C++利用openssl进行公钥解密
私钥加密的部分内容,需要用公钥解密下面的实例代码,由于私钥加密后的字符串有不可打印字符,所以程序里面进行了base64,要用的时候先解dec base64
再传递给函数 进行解密
#include <stdio.h> #include <stdlib.h> #ifdef WIN32 #include <windows.h> #endif #include "openssl/rsa.h" #include "openssl/pem.h" #include "Base64.h" #ifdef WIN32 #pragma comment(lib,"User32.lib") #pragma comment(lib,"Advapi32.lib") #pragma comment(lib,"Gdi32.lib") #pragma comment(lib,"libeay32.lib") #pragma comment(lib,"ssleay32.lib") #endif
// 私钥解密
std::string rsa_pri_decrypt(const std::string &cipherText, const std::string &priKey)
{
std::string strRet;
RSA *rsa = RSA_new();
BIO *keybio;
keybio = BIO_new_mem_buf((unsigned char *)priKey.c_str(), -1);
// 此处有三种方法
// 1, 读取内存里生成的密钥对,再从内存生成rsa
// 2, 读取磁盘里生成的密钥对文本文件,在从内存生成rsa
// 3,直接从读取文件指针生成rsa
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
int len = RSA_size(rsa);
char *decryptedText = (char *)malloc(len + 1);
memset(decryptedText, 0, len + 1);
// 解密函数
int ret = RSA_private_decrypt(cipherText.length(), (const unsigned char*)cipherText.c_str(), (unsigned char*)decryptedText, rsa, RSA_PKCS1_PADDING);
if (ret >= 0)
strRet = std::string(decryptedText, ret);
// 释放内存
free(decryptedText);
BIO_free_all(keybio);
RSA_free(rsa);
return strRet;
}
// 公钥解密 std::string rsa_pub_decrypt(const std::string &cipherText, const std::string &pubKey) { std::string strRet; RSA *rsa = RSA_new(); BIO *keybio= BIO_new_mem_buf((unsigned char *)pubKey.c_str(), -1); rsa=PEM_read_bio_RSA_PUBKEY(keybio,&rsa,NULL,NULL); int len = RSA_size(rsa); char *decryptedText = (char *)malloc(len + 1); memset(decryptedText, 0, len + 1); // 解密函数 int ret = RSA_public_decrypt(cipherText.length(), (const unsigned char*)cipherText.c_str(), (unsigned char*)decryptedText, rsa, RSA_PKCS1_PADDING); if (ret >= 0) strRet = std::string(decryptedText, ret); // 释放内存 free(decryptedText); BIO_free_all(keybio); RSA_free(rsa); return strRet; } int main(int narg,char** args) { string pubKey="\ -----BEGIN PUBLIC KEY-----\n\ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAptoreKuW1O+Bb1u+O0LQ\n\ NAiaj5i0gbqi5uHCfcTxPIScoeidR/FaPFWpG3mHUCd7SfPKlSQ4H4FGwqkPrAJ8\n\ tb6vfNAY7UPlStaWwCASBBBeHRIFySDIFQnIeXTcZqKgvCAkM3kudyKGMGfZyU6v\n\ SUCMUJbvtIKrSBpPFOaPF1EjfsTpcRjRwNUxjInvxyOmegdLSgg/NNV9AxUSpGX4\n\ Fs5j3W4u4bfgoH6WiI7QLeVWpw+ZcynndtihPa2Y5rKgTDq1IYnniaYy+bTlWMPC\n\ us9AjL0qaM0oXKzUByIeA8cWQR99ewSLsEaa7mhpUZ1jg+fDQEqYfgvenfcOe5bn\n\ NQIDAQAB\n\ -----END PUBLIC KEY-----\n"; string encStr="TgaNSiG6yPkKql3pIW9310IsRpcppl4SIt12kdP3ZwR0wWj65P3I3YCaSrb7+lmJ9N\ mpTmHHzwgvTQN3zM+IRpjQr1iAJZ0C/+2uM0WkPwbZhvlzcr4iZFOfhe6CbPGibu1\ +pRYdkkxgvXT3/0D4hrtzOxMXVYzBLjfd7xXRfVTXGI7FlBGfq6/W+bg3DE3BQbdS\ 07eBBRKQ1Jbl/LPIQP40FK5qdvTDeWTG0m/mLRfpavbIw4pMKnc7845xU9ZZVmxcv\ 14O7AJYmJat72mGzDVpafx+65aanLMIydSY+dly62XeSRd5ghd1fSaTHlVN4h59YY\ qNPXGxFKTrMvPUDg=="; Base64 base; string encStr2=base.Decode(encStr.c_str(),encStr.length()); string decStr=rsa_pub_decrypt(encStr2,pubKey); printf("---%s---\n",decStr.c_str()); system("pause"); return 0; }
下面的私钥和公钥 以及加密字符串
秘钥位数2048 秘钥格式PKCD#8 -----BEGIN PUB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WoNP ssMf9XFnddjPvHIkHG9zNdjvmbgyuj4puzB3tBKtd8pZlzQj4A7uSOWRVkr9/kFZ CLMG7W6B3XCtnutAF16pGQxwgqjkLH73RSqTvJVdZQKBgQDXv8qeDRROPRxmU6u+ Zz+wK32iyocd+eBwhuc/TnEOBqlzF/HySVSqPIEzIXs48qmMSYGI8Ab/Qy1hB4Hl DZqglx4aQ3K31QCIzzINulDxj/msYNRp+hFV2UCrZN/ScNYymdj+kdIt8brM1RRl hZjVcWwH1Iv8MZ12ALaEclkjtwKBgQDF+w2m4oWsPApq1vs4Cc+61gh2W9xP6Cij FShyXU7Ms/qQ2F9C+0DC6ssGNC0+YV4aK6xSE6vhexGpJYf08fT/RrDW3JLTGdFC HvX8QTEf9LoCV5y7mdoNirKV3eVcYMCUkVFEyQfXUDz2bKt+VU36ep395UUzCTRZ IYD9kOsEcwKBgENZIXzZBF+v/++JavFS9bSevudUZX1cBU3bLtC6QdxpA/hjnylM 7tr9bC9z4i1Sws6CG6eNlgGVDNNfLpM7/erngYQdTfBI05PhafSTFrVxhjTjPSuf 3k14NhHKuXvNsW+2SuCc0Il7Dh7m3Skb3Suz7k87cD9XlZnCezc5Is5dAoGAAVde FE9ckHonp+mk8hA+9AkXLZYZvADapaAzEfNH4WMmw2xz2MRS//8POQ+Jbu7B8HNz S2sOswxeZkL21NX+zuvvrsT3ya5XGJeVGwxmBM1npTRqv2qfa5AQ/arvLt0I8Pi7 EDg8cVz9zL2Xu9dgG95Qy7ON8HaTgDcV5KHitbkCgYEA1a/mgASIJRP38FUcl1OB CebYFOMicyjw0LbxfGZSThpHnSxPCI93iw6XbOrwrcsDe67GPbkT/mknNKGWXiof goLgLkVS5TXSxVO786YqNwWE2pB8+phhcFtb1wYyTnMXIo8a+KsZ0tWTO4pvlIsz /4e1njkFYpOoRHjczHhA1Ag= -----END PRIVATE KEY----- 加密前字符串 73a90acaae2b1ccc0e969709665bc62f 加密后字符串 TgaNSiG6yPkKql3pIW9310IsRpcppl4SIt12kdP3ZwR0wWj65P3I3YCaSrb7+lmJ9NmpTmHHzwgvTQN3zM+IRpjQr1iAJZ0C/+2uM0WkPwbZhvlzcr4iZFOfhe6CbPGibu1+pRYdkkxgvXT3/0D4hrtzOxMXVYzBLjfd7xXRfVTXGI7FlBGfq6/W+bg3DE3BQbdS07eBBRKQ1Jbl/LPIQP40FK5qdvTDeWTG0m/mLRfpavbIw4pMKnc7845xU9ZZVmxcv14O7AJYmJat72mGzDVpafx+65aanLMIydSY+dly62XeSRd5ghd1fSaTHlVN4h59YYqNPXGxFKTrMvPUDg==