rsa 加密 pkcs#1格式秘钥的格式化

C++调用openssl库生成的秘钥对,经过传输传出来的只有秘钥的内容,没有秘钥的格式。而咱们在调用openssl库加密解密时,传入的秘钥是须要包含格式的。C++调用openssl库须要的格式为pkcs#1, java默认的格式为pkcs#8。html

下面的代码,仅仅是添加收尾标识,并不是对密匙内容作转换。java

//pkcs#1格式的私钥 64位分行 + 首尾标志
std::string formatPrivateKeyPKCS1(std::string priKey )
{
    std::string strPrivateKey = priKey;
    {
    //语句块做用:读取内存里生成的秘钥对,再从内存生成rsa
    int nPrivateKeyLen = strPrivateKey.size();
    for(int i = 64; i < nPrivateKeyLen; i+=64)
    {
        if(strPrivateKey[i] != '\n')
        {
            strPrivateKey.insert(i, "\n");
        }
        i++;
    }
    strPrivateKey.insert(0, "-----BEGIN RSA PRIVATE KEY-----\n");
    strPrivateKey.append("\n-----END RSA PRIVATE KEY-----\n");
    }
    return strPrivateKey;
}

//pkcs#1格式的公钥 64位分行 + 首尾标志
std::string formatPublicKeyPKCS1(std::string pubKey )
{
    std::string strPublicKey = pubKey;
    {
        //语句块做用:读取内存里生成的秘钥对,再从内存生成rsa
        int nPublicKeyLen = strPublicKey.size();
        for(int i = 64; i < nPublicKeyLen; i+=64)
        {
            if(strPublicKey[i] != '\n')
            {
                strPublicKey.insert(i, "\n");
            }
            i++;
        }
        strPublicKey.insert(0, "-----BEGIN RSA PUBLIC KEY-----\n");
        strPublicKey.append("\n-----END RSA PUBLIC KEY-----\n");
    }
    return strPublicKey;
}

 

附1:rsa密匙对生成web

附 2:rsa秘钥对在线格式转换app

附3:DES加解密 cbc模式 的简单讲解 && C++用openssl库来实现的注意事项加密

附4:C++ 使用openssl库实现 DES 加密——CBC模式 && RSA加密——公加私解——私加公解spa

相关文章
相关标签/搜索