C++11S/MIME:具有安全功能的简单MIME解析器和生成器

开课吧开课吧锤锤2021-05-12 16:17

    这是我的简单MIME解析器和生成器的版本2,该版本使用C++11元素和Win32API进行快速处理。

    对于S/MIME,库现在使用我的AdES。

    从此处包括QP解码器。

    单个消息生成器

// Single Message
MIME2::CONTENT c;

c["MIME-Version"] = "1.0";
c["Content-Type"] = "text/plain";
c.SetData("Hello");

auto str = c.SerializeToVector();

    结果:

MIME-Version: 1.0
Content-Type: text/plain

Hello

    一些二进制消息

MIME2::CONTENT c;
c["MIME-Version"] = "1.0";
c["Content-Type"] = "application/octet-stream";
c["Content-Transfer-Encoding"] = "base64";
c["Content-Disposition"] = "attachment; filename=\"hello.txt\"";
string out = MIME2::Char2Base64("Hello", 5);
c.SetData(out.c_str());

auto str = c.SerializeToVector();

    结果:

MIME-Version: 1.0
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="hello.txt"

SGVsbG8=

    多部分生成器

MIME2::CONTENTBUILDER cb;
MIME2::CONTENT e1;
MIME2::CONTENT e2;
e1["Content-Type"] = "text/plain";
e1.SetData("Hello\r\n\r\n");
e2["Content-Type"] = "text/html";
e2.SetData("<b>Hello</b>");

cb.Add(e1);
cb.Add(e2);

MIME2::CONTENT cc;
cb.Build(cc, "multipart/alternative");

auto str = cc.SerializeToVector();

    结果:

MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}"

--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}
Content-Type: text/plain

Hello

--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}
Content-Type: text/html

<b>Hello</b>

--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}--

简单解析

string str = 
R"(MIME-Version: 1.0
Content-Type: text/plain

Hello)";

    MIME2::CONTENT c;
    if (c.Parse(str.c_str()) != MIME2::MIMEERR::OK)
        return;

    auto a1 = c.hval("Content-Type"); // a1 = "text/plain"
    auto a2 = c.GetData(); // vector<char> with the data

    解析多个

string str = "MIME-Version: 1.0\r\n\
Content-Type: multipart/alternative; boundary=\"{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}\"\r\n\r\n\
\r\n\
--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}\r\n\
Content-Type: text/plain\r\n\
\r\n\
Hello\r\n\
\r\n\
--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}\r\n\
Content-Type: text/html\r\n\
\r\n\
<b>Hello</b>\r\n\
\r\n\
--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}--";

    MIME2::CONTENT c;
    if (c.Parse(str.c_str()) != MIME2::MIMEERR::OK)
        return;

    auto a1 = c.hval("Content-Type","boundary"); // a1 = the boundary
    if (a1.empty())
        return;

    vector<MIME2::CONTENT> Contents;
    MIMELIB::ParseMultipleContent(str.c_str(), a1.c_str(), Contents);

    // Should have 2
    vector<char> d;
    Contents[1].DecodeData(d); // Decodes from Base64 or Quoted-Printable
    // d = "<b>Hello</b>"

    S/MIME

    对于S/MIME,该库现在使用AdES。您必须#defineMIME_CMS使用S/MIME。

MIMEERR Encrypt(CONTENT& c, std::vector<PCCERT_CONTEXT> certs, bool BinaryOutput = false);
MIMEERR Decrypt(CONTENT& c);
MIMEERR Sign(CONTENT& co, std::vector<PCCERT_CONTEXT> certs,
      std::vector<PCCERT_CONTEXT> addcerts, const wchar_t* TimeStampServer = 0,
      bool Attached = true, bool BinaryOutput = false);
MIMEERR Verify(vector<PCCERT_CONTEXT>* Certs = 0, AdES::CLEVEL* plev = 0);

    HTTP支持

vector<char> data = "HTTP 1/1 200 OK\r\n...."
c.Parse(data.data(),true); // true indicates a possible HTTP header
auto mh = c1.httphdr();    // Gets the header

    或建立:

c.AddHTTPHeader("HTTP 1/1 200 OK");

    以上就是开课吧广场小编为大家整理发布的“C++11S/MIME:具有安全功能的简单MIME解析器和生成器”一文,更多Web教程相关内容尽在开课吧广场Web教程频道!

有用
分享
全部评论快来秀出你的观点
登录 后可发表观点…
发表
暂无评论,快来抢沙发!
云开发实战特训营