65.9K
CodeProject 正在变化。 阅读更多。
Home

C++ 11 S/MIME:一个具有安全功能的简单 MIME 解析器和构建器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.90/5 (18投票s)

2016年7月24日

CPOL
viewsIcon

43604

downloadIcon

560

易于使用的简单解析器,支持 S/MIME。版本 2。

引言

这是我的简单 MIME 解析器和构建器的版本 2,它使用 C++ 11 元素和 Win32 API 来实现快速操作。

对于 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。您必须 #define MIME_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");

玩得开心!

历史

  • 2018-09-03:版本 2,更简洁的接口和通过 AdES 使用 S/MIME
  • 2016-08-02:为所有类型的操作添加了二进制支持
  • 2016-07-30:添加了时间戳支持和底层函数,用于分离签名的构建器
  • 2016-07-23:首次发布
© . All rights reserved.