简体   繁体   English

在Windows CE 3上将PEM转换为DER

[英]Translate PEM to DER on Windows CE 3

I have a RSA public key crypto wrapper which works quite well in desktop Windows and Windows Embedded/POSReady. 我有一个RSA公钥加密包装器,它在桌面Windows和Windows Embedded / POSReady中运行良好。 I need to port this system to both Windows CE 5 and Windows CE 3. In part of this system, I allow developers to import various crypto objects, such as certificates and keys, in several encodings. 我需要将此系统移植到Windows CE 5和Windows CE 3.在此系统的一部分中,我允许开发人员以多种编码方式导入各种加密对象,例如证书和密钥。 The most commonly used encoding is Base64 encoded PEM. 最常用的编码是Base64编码的PEM。 On most versions of Windows, it is easy to convert encodings to the binary (DER) format that Windows needs for CryptDecodeObjectEx calls: 在大多数Windows版本中,很容易将编码转换为Windows对CryptDecodeObjectEx调用所需的二进制(DER)格式:

bool MyClass::externalToBinary( const DATA_BLOB &in, DATA_BLOB &outDER )
{
    DWORD flags;

    // This call determines the format and how much memory is needed in outDER
    if ( ::CryptStringToBinaryA( reinterpret_cast<char *>(in.pbData), in.cbData, CRYPT_STRING_ANY,     NULL, &outDER.cbData, NULL, &flags ) == false &&
         ::CryptStringToBinaryA( reinterpret_cast<char *>(in.pbData), in.cbData, CRYPT_STRING_HEX_ANY, NULL, &outDER.cbData, NULL, &flags ) == false )
    {
        // Log errors here using ::GetLastError();
        return false;
    }

    if ( ( outDER.pbData = new unsigned char[outDER.cbData] ) == NULL )
    {
        // Log errors here using ::GetLastError();
        return false;
    }
    return ( ::CryptStringToBinaryA( reinterpret_cast<char *>(in.pbData), in.cbData, flags, outDER.pbData, &outDER.cbData, NULL, NULL ) != FALSE );
} // end externalToBinary

Unfortunately, CryptStringToBinary doesn't exist in Windows CE 3's version of CryptoAPI. 不幸的是, CryptStringToBinary在Windows CE 3的CryptoAPI版本中不存在。 While I can do away with support for less popular encodings (such as hex), I really don't want to remove support for PEM encoding in the CE 3 version of the API. 虽然我可以取消对不太流行的编码(例如hex)的支持,但我真的不想在CE 3版本的API中删除对PEM编码的支持。

Does anyone have a CryptStringToBinary alternative that would work on Windows CE 3? 有没有人有一个可以在Windows CE 3上运行的CryptStringToBinary替代品? The developers using this API don't currently have OpenSSL as a dependency, so I would prefer not to add it just for this. 使用此API的开发人员目前没有将OpenSSL作为依赖项,因此我不想仅为此添加它。

Well - PEM is simply base64 encoded DER. 嗯 - PEM只是base64编码的DER。 So if this is the only place you need to convert; 所以如果这是你需要转换的唯一地方; I'd just forego the API niceties; 我只是放弃了API的细节; strip off any empty line or line starting with a '-'; 剥去任何以' - '开头的空行或行; and base64 decode the anything in between. 和base64解码之间的任何东西。 The result is the DER you're after. 结果是你追求的DER。 WindowsCE3 has a Encode/Decodeter type for Base64; WindowsCE3具有Base64的编码/解码器类型; If for some reason you do not/cannot want to use that - How do I base64 encode (decode) in C? 如果由于某种原因你不能/不想使用它 - 我如何在C中进行base64编码(解码)? or http://www.adp-gmbh.ch/cpp/common/base64.html are fairly trivial 'raw' versions. http://www.adp-gmbh.ch/cpp/common/base64.html是相当简单的“原始”版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM