RSA加解密 私钥加密公钥解密 私加公解 && C++ 调用openssl库 的代码实例

前提:秘钥长度=1024

==============================================

    对一片(117字节)明文加密  私加

==============================================

// 私钥加密
std::string rsa_pri_encrypt(const std::string &clearText,  std::string &pubKey)  
{  
    std::string strRet;  
    BIO *keybio = BIO_new_mem_buf((unsigned char *)pubKey.c_str(), -1);  
    // 此处有三种方法  
    // 1, 读取内存里生成的密钥对,再从内存生成rsa  
    // 2, 读取磁盘里生成的密钥对文本文件,在从内存生成rsa  
    // 3,直接从读取文件指针生成rsa  
    //RSA* pRSAPublicKey = RSA_new();  
    RSA* rsa = RSA_new();
    rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
    if (!rsa)
    {
            BIO_free_all(keybio);
            return std::string("");
    }

    int len = RSA_size(rsa);  
    //int len = 1028;
    char *encryptedText = (char *)malloc(len + 1);  
    memset(encryptedText, 0, len + 1);  
  
    // 加密  
    int ret = RSA_private_encrypt(clearText.length(), (const unsigned char*)clearText.c_str(), (unsigned char*)encryptedText, rsa, RSA_PKCS1_PADDING);  
    if (ret >= 0)  
        strRet = std::string(encryptedText, ret);  
  
    // 释放内存  
    free(encryptedText);  
    BIO_free_all(keybio);  
    RSA_free(rsa);  
  
    return strRet;  
}  

==============================================

    对一片(128字节)密文解密  公解

==============================================

// 公钥解密    
std::string rsa_pub_decrypt(const std::string &clearText,  std::string &pubKey)  
{  
    std::string strRet;  
    BIO *keybio = BIO_new_mem_buf((unsigned char *)pubKey.c_str(), -1);  
    //keybio = BIO_new_mem_buf((unsigned char *)strPublicKey.c_str(), -1);  
    // 此处有三种方法  
    // 1, 读取内存里生成的密钥对,再从内存生成rsa  
    // 2, 读取磁盘里生成的密钥对文本文件,在从内存生成rsa  
    // 3,直接从读取文件指针生成rsa  
    //RSA* pRSAPublicKey = RSA_new();  
    RSA* rsa = RSA_new();
    rsa = PEM_read_bio_RSAPublicKey(keybio, &rsa, NULL, NULL);
    if (!rsa)
    {
            BIO_free_all(keybio);
            return std::string("");
    }

    int len = RSA_size(rsa);  
    //int len = 1028;
    char *encryptedText = (char *)malloc(len + 1);  
    memset(encryptedText, 0, len + 1);  
  
    //解密
    int ret = RSA_public_decrypt(clearText.length(), (const unsigned char*)clearText.c_str(), (unsigned char*)encryptedText, rsa, RSA_PKCS1_PADDING);  
    if (ret >= 0)  
        strRet = std::string(encryptedText, ret);  
  
    // 释放内存  
    free(encryptedText);  
    BIO_free_all(keybio);  
    RSA_free(rsa);  
  
    return strRet;  
}

==============================================

    对整体   明文加密  私加

==============================================

//私钥加密 + 分片
std::string rsa_pri_split117_encrypt(const std::string &clearText,  std::string &pubKey)
{
    std::string result;
    std::string input;
    result.clear();
    for(int i = 0 ; i < clearText.length()/117; i++)
    {
           input.clear();
           input.assign(clearText.begin() + i*117, clearText.begin() + i*117 + 117);
           result = result + rsa_pri_encrypt(input, pubKey);
    }
    if( clearText.length()%117 != 0)
    {
        int tem1 = clearText.length()/117*117;
        int tem2 = clearText.length() - tem1;
        input.clear();
        input.assign(clearText.begin()+ tem1, clearText.end());
        result = result + rsa_pri_encrypt(input, pubKey);
    }
    return result;
}

==============================================

    对整体   密文解密  公解

==============================================

//公钥解密 + 分片
std::string rsa_pub_split128_decrypt(const std::string &clearText,  std::string &pubKey)
{
    //Base64 *base = new Base64();
    std::string result;
    std::string input;
    result.clear();
    for(int i = 0 ; i< clearText.length()/128; i++)
    {
        input.clear();
        input.assign(clearText.begin() + i*128, clearText.begin() + i*128 + 128);

        result = result + rsa_pub_decrypt(input, pubKey);
    }
    if(clearText.length()%128 != 0)
    {
        int tem1 = clearText.length()/128 * 128;
        int tem2 = clearText.length() - tem1;
        input.clear();
        input.assign(clearText.begin()+ tem1, clearText.end());
        result = result + rsa_pri_encrypt(input, pubKey);
    }
    return result;
}

附1:rsa 公加私解

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

原文地址:https://www.cnblogs.com/azbane/p/10180464.html