openssl使用rsa加解密,C++封装,支持设置公钥、私钥,公钥加解密,私钥加解密。
使用方法很简单:
CRSAEncoder mRSAEncoderPC;
//设置公钥
mRSAEncoderPC.SetPublicKey("-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQE..........XApYoMnPE3g4xU4NceOBTFZtR5fp+w/MswIDAQAB\n-----END PUBLIC KEY-----");
//公钥解密
std::string strTest = mRSAEncoderPC.PublicDecrypt("......");
//公钥加密
strTest = mRSAEncoderPC.PublicEncrypt("......");
//另外一种
unsigned char d[] = {
0x38, 0x60, 0x63, 0x56, 0xBC, 0x54, 0x52, 0xBC, 0xA6, 0xB6, 0xDB, 0x47, 0x49, 0x08, 0xE5, 0xB8,
0x0B, 0xAA, 0x44, 0xEC, 0x49, 0x2B, 0x35, 0x5F, 0xBB, 0x1A, 0xB2, 0x29, 0x2D, 0x0F, 0x2D, 0xE2,
0x93, 0x70, 0x4D, 0x8F, 0x6F, 0x01, 0x64, 0xE4, 0xC9, 0x0C, 0x03, 0x4C, 0x02, 0x08, 0xE6, 0xB7,
0xAF, 0xBB, 0x8A, 0x0A, 0xFF, 0x84, 0xB4, 0xBA, 0x9E, 0x4B, 0x1A, 0xB2, 0x0A, 0x75, 0xDA, 0xFD,
0x0E, 0xBE, 0x73, 0xCA, 0x5C, 0xFC, 0xA2, 0x4D, 0xCF, 0x56, 0xA8, 0xAD, 0x9D, 0xC3, 0x60, 0x86,
0xF5, 0xA8, 0xA0, 0xD0, 0xCD, 0x7E, 0x21, 0x8A, 0xCE, 0x4C, 0xCD, 0x03, 0xDE, 0x76, 0xF6, 0xA5,
0x95, 0xA9, 0x77, 0x77, 0xFF, 0xF9, 0xBA, 0x3B, 0x0F, 0xD9, 0xFF, 0x50, 0x63, 0x6E, 0xDD, 0x49,
0xFA, 0x31, 0x7D, 0xE0, 0xC5, 0x81, 0xC5, 0x75, 0x79, 0xCE, 0x1C, 0x78, 0x1A, 0x94, 0xD6, 0x7A,
0xA1, 0xFD, 0x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34, 0x04,
0x6D, 0xD1, 0x6E, 0xF7, 0x2D, 0x96, 0xB3, 0xD3, 0xAE, 0x43, 0xC3, 0x4D, 0x26, 0x23, 0x5E, 0x7F
};
unsigned char e[] = { 0x01, 0x00, 0x01 };
CRSAEncoder mRSAEncoder;
//设置私钥
mRSAEncoder.SetPrivateKey(n, sizeof(n), e, sizeof(e), d, sizeof(d));
//设置私钥另外一个姿势也可以的
//mRSAEncoder.SetPrivateKey("-----BEGIN PRIVATE KEY-----\nMIICeAIBA..............DANBQltd+11\n-----END PRIVATE KEY-----");
//私钥加密
FString strEnBuf = mRSAEncoder.PrivateEncrypt("......");
//私钥解密
strEnBuf = mRSAEncoder.PrivateDecrypt("......");
RSAEncoder.h(header only):
#pragma once
#include <string>
#include <memory>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
class CRSAEncoder
{
public:
CRSAEncoder(void)
{
m_pRSAPrivate = NULL;
m_pRSAPublic = NULL;
m_nPaddingType = RSA_PKCS1_PADDING;
m_nPaddingLen = RSA_PKCS1_PADDING_SIZE;
}
~CRSAEncoder(void)
{
if (m_pRSAPrivate)
RSA_free(m_pRSAPrivate);
if (m_pRSAPublic)
RSA_free(m_pRSAPublic);
}
//设置padding类型,缺省为RSA_PKCS1_PADDING
void SetPaddingType(int nPadding)
{
m_nPaddingType = nPadding;
}
//设置padding长度,缺省为RSA_PKCS1_PADDING_SIZE
void SetPaddingLength(int nPaddingLen)
{
m_nPaddingLen = nPaddingLen;
}
//设置公钥,-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----
bool SetPublicKey(const char* szKeyData)
{
if (m_pRSAPublic)
{
RSA_free(m_pRSAPublic);
m_pRSAPublic = NULL;
}
BIO* pKEY = NULL;
try
{
pKEY = _GetBIOFromKeyString(szKeyData);
if (pKEY == NULL)
throw ERR_get_error();
m_pRSAPublic = PEM_read_bio_RSA_PUBKEY(pKEY, NULL, NULL, NULL);
BIO_free_all(pKEY);
if (m_pRSAPublic == NULL)
throw ERR_get_error();
}
catch (unsigned long e)
{
printf("rsa set public key err %d.\n", e);
return false;
}
return true;
}
//设置公钥,n,e,n和e为原始内存数据
void SetPublicKey(const void* n, int nlen, const void* e, int elen)
{
if (m_pRSAPublic)
RSA_free(m_pRSAPublic);
m_pRSAPublic = RSA_new();
BIGNUM* bn = BN_new();
BIGNUM* be = BN_new();
BN_bin2bn((const unsigned char*)n, nlen, bn);
BN_bin2bn((const unsigned char*)e, elen, be);
RSA_set0_key(m_pRSAPublic, bn, be, NULL);
}
void SetPublicKey(const std::string& n, const std::string& e)
{
SetPublicKey(n.c_str(), (int)n.length(), e.c_str(), (int)e.length());
}
//设置私钥,-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----
bool SetPrivateKey(const char* szKeyData)
{
if (m_pRSAPrivate)
{
RSA_free(m_pRSAPrivate);
m_pRSAPrivate = NULL;
}
BIO* pKEY = NULL;
try
{
pKEY = _GetBIOFromKeyString(szKeyData);
if (pKEY == NULL)
throw ERR_get_error();
m_pRSAPrivate = PEM_read_bio_RSAPrivateKey(pKEY, NULL, NULL, NULL);
BIO_free_all(pKEY);
if (m_pRSAPrivate == NULL)
throw ERR_get_error();
}
catch (unsigned long e)
{
printf("rsa set private key err %d.\n", e);
return false;
}
return true;
}
//设置私钥,n,e,d
void SetPrivateKey(const void* n, int nlen, const void* e, int elen, const void* d, int dlen)
{
if (m_pRSAPrivate)
RSA_free(m_pRSAPrivate);
m_pRSAPrivate = RSA_new();
BIGNUM* bn = BN_new();
BIGNUM* be = BN_new();
BIGNUM* bd = BN_new();
BN_bin2bn((const unsigned char*)n, nlen, bn);
BN_bin2bn((const unsigned char*)e, elen, be);
BN_bin2bn((const unsigned char*)d, dlen, bd);
RSA_set0_key(m_pRSAPrivate, bn, be, bd);
}
void SetPrivateKey(const std::string& n, const std::string& e, const std::string& d)
{
SetPrivateKey(n.c_str(), (int)n.length(), e.c_str(), (int)e.length(), d.c_str(), (int)d.length());
}
//私钥加密
std::string PrivateEncrypt(const void* data, int data_len)
{
std::string strRet;
if (m_pRSAPrivate == NULL)
{
printf("private key is empyt.\n");
return strRet;
}
int nRsaLen = 0;
int nBlockLen = 0;
int nBlockCount = 0;
int nPartResult = 0;
try
{
nRsaLen = RSA_size(m_pRSAPrivate);
if (nRsaLen <= 0)
throw ERR_get_error();
nBlockLen = nRsaLen - m_nPaddingLen;
nBlockCount = data_len / nBlockLen;
if (data_len % nBlockLen)
nBlockCount++;
//分块加密
std::unique_ptr <unsigned char[]> buff(new unsigned char[nRsaLen]);
for (int i = 0; i < nBlockCount; i++)
{
int nDatalen = (std::min)(nBlockLen, data_len - (i * nBlockLen));
nPartResult = RSA_private_encrypt(nDatalen, (unsigned char*)data + (i * nBlockLen), buff.get(), m_pRSAPrivate, m_nPaddingType);
if (nPartResult < 0)
throw ERR_get_error();
strRet.append((char*)buff.get(), nPartResult);
}
}
catch (unsigned long e)
{
printf("rsa private encrypt err %d.\n", e);
strRet.clear();
}
return strRet;
}
std::string PrivateEncrypt(const std::string& strData)
{
return PrivateEncrypt(strData.c_str(), (int)strData.length());
}
//公钥加密
std::string PublicEncrypt(const void* data, int data_len)
{
std::string strRet;
if (m_pRSAPublic == NULL)
{
printf("public key is empyt.\n");
return strRet;
}
int nRsaLen = 0;
int nBlockLen = 0;
int nBlockCount = 0;
int nPartResult = 0;
try
{
nRsaLen = RSA_size(m_pRSAPublic);
if (nRsaLen <= 0)
throw ERR_get_error();
nBlockLen = nRsaLen - m_nPaddingLen;
nBlockCount = data_len / nBlockLen;
if (data_len % nBlockLen)
nBlockCount++;
//分块加密
std::unique_ptr <unsigned char[]> buff(new unsigned char[nRsaLen]);
for (int i = 0; i < nBlockCount; i++)
{
int nDatalen = (std::min)(nBlockLen, data_len - (i * nBlockLen));
nPartResult = RSA_public_encrypt(nDatalen, (unsigned char*)data + (i * nBlockLen), buff.get(), m_pRSAPublic, m_nPaddingType);
if (nPartResult < 0)
throw ERR_get_error();
strRet.append((char*)buff.get(), nPartResult);
}
}
catch (unsigned long e)
{
printf("rsa public encrypt err %d.\n", e);
strRet.clear();
}
return strRet;
}
std::string PublicEncrypt(const std::string& strData)
{
return PublicEncrypt(strData.c_str(), (int)strData.length());
}
//私钥解密
std::string PrivateDecrypt(const void* data, int data_len)
{
std::string strRet;
if (m_pRSAPrivate == NULL)
{
printf("private key is empyt.\n");
return strRet;
}
int nRsaLen = 0;
int nBlockCount = 0;
int nPartResult = 0;
try
{
nRsaLen = RSA_size(m_pRSAPrivate);
if (nRsaLen <= 0)
throw ERR_get_error();
if (data_len % nRsaLen)
throw 0;
nBlockCount = data_len / nRsaLen;
//分块解密
std::unique_ptr <unsigned char[]> buff(new unsigned char[nRsaLen]);
for (int i = 0; i < nBlockCount; i++)
{
//
nPartResult = RSA_private_decrypt(nRsaLen, (unsigned char*)data + (i * nRsaLen), buff.get(), m_pRSAPrivate, m_nPaddingType);
if (nPartResult < 0)
throw ERR_get_error();
strRet.append((char*)buff.get(), nPartResult);
}
}
catch (unsigned long e)
{
char errbuf[256];
printf("rsa private decrypt err %s(%d).\n", ERR_error_string(e, errbuf), e);
strRet.clear();
}
return strRet;
}
std::string PrivateDecrypt(const std::string& strData)
{
return PrivateDecrypt(strData.c_str(), (int)strData.length());
}
//公钥解密
std::string PublicDecrypt(const void* data, int data_len)
{
std::string strRet;
if (m_pRSAPublic == NULL)
{
printf("public key is empyt.\n");
return strRet;
}
int nRsaLen = 0;
int nBlockCount = 0;
int nPartResult = 0;
try
{
nRsaLen = RSA_size(m_pRSAPublic);
if (nRsaLen <= 0)
throw ERR_get_error();
if (data_len % nRsaLen)
throw 0;
nBlockCount = data_len / nRsaLen;
//分块解密
std::unique_ptr <unsigned char[]> buff(new unsigned char[nRsaLen]);
for (int i = 0; i < nBlockCount; i++)
{
//
nPartResult = RSA_public_decrypt(nRsaLen, (unsigned char*)data + (i * nRsaLen), buff.get(), m_pRSAPublic, m_nPaddingType);
if (nPartResult < 0)
throw ERR_get_error();
strRet.append((char*)buff.get(), nPartResult);
}
}
catch (unsigned long e)
{
printf("rsa public decrypt err %d.\n", e);
strRet.clear();
}
return strRet;
}
std::string PublicDecrypt(const std::string& strData)
{
return PublicDecrypt(strData.c_str(), (int)strData.length());
}
protected:
RSA *m_pRSAPrivate;
RSA *m_pRSAPublic;
int m_nPaddingType;
int m_nPaddingLen;
BIO* _GetBIOFromKeyString(const std::string strKey)
{
BIO* pKEY = NULL;
try
{
pKEY = BIO_new(BIO_s_mem());
if (pKEY == NULL)
throw ERR_get_error();
int nResult = BIO_write(pKEY, strKey.c_str(), (int)strKey.length());
if (nResult <= 0)
throw ERR_get_error();
}
catch (unsigned long e)
{
printf("rsa bio key err %d.\n", e);
if (pKEY)
BIO_free_all(pKEY);
return NULL;
}
return pKEY;
}
};
!
2024/11/19 | Tags:rsa,openssl,C | C/C++代码 | 查看评论(0)