#include "openssl/rsa.h"
#include "openssl/err.h"
#include "openssl/pem.h"
一 产生私钥文件 和 公钥文件
int GenerateKey(char* publicKeyFile, char* privateKeyFile)
{
RSA *pRsa = RSA_generate_key(RSA_KEY_LENGTH, RSA_F4, NULL, NULL);
if (pRsa == NULL) {
hloge("RSA_generate_key Error");
return -1;
}
BIO *priBio = BIO_new_file(privateKeyFile, "w");
if (PEM_write_bio_RSAPrivateKey(priBio, pRsa, NULL, NULL, 0, NULL, NULL) <= 0) {
hloge("Save to private key file error");
return -2;
}
BIO *pubBio = BIO_new_file(publicKeyFile, "w");
if (PEM_write_bio_RSAPublicKey(pubBio, pRsa) <= 0) {
hloge("Save to private key file error");
return -3;
}
BIO_free(priBio);
BIO_free(pubBio);
RSA_free(pRsa);
CRYPTO_cleanup_all_ex_data();
return 0;
}
二 公钥加密
int PublicKeyEncrypt(char* publicKeyFile, unsigned char* pRawData, int rawDataLen, unsigned char* pOutData, int& outDataLen)
{
if(publicKeyFile == NULL || pRawData == NULL || pOutData == NULL || rawDataLen == 0)
{
hloge("Encrypt para Error");
return -1;
}
FILE* hPubKeyFile = fopen(publicKeyFile, "rb");
if( hPubKeyFile == NULL )
{
hloge("fopen publicKeyFile fail");
return -2;
}
RSA* pRSAPublicKey = RSA_new();
if(PEM_read_RSAPublicKey(hPubKeyFile, &pRSAPublicKey, 0, 0) == NULL)
{
hloge("PEM_read_RSA_PUBKEY Error");
return -3;
}
outDataLen = RSA_public_encrypt(rawDataLen, (const unsigned char*)pRawData, pOutData, pRSAPublicKey, RSA_PKCS1_PADDING);
RSA_free(pRSAPublicKey);
fclose(hPubKeyFile);
CRYPTO_cleanup_all_ex_data();
return 0;
}
三 公钥解密
int PublicKeyDecrypt(char* publicKeyFile, unsigned char* pEncryptData, int encryptDataLen, unsigned char* pOutData, int& outDataLen)
{
if(publicKeyFile == NULL || pEncryptData == NULL || pOutData == NULL || encryptDataLen == 0)
{
hloge("Encrypt para Error");
return -1;
}
FILE* hPubKeyFile = fopen(publicKeyFile, "rb");
if( hPubKeyFile == NULL )
{
hloge("fopen publicKeyFile fail");
return -2;
}
RSA* pRSAPublicKey = RSA_new();
if(PEM_read_RSAPublicKey(hPubKeyFile, &pRSAPublicKey, 0, 0) == NULL)
{
hloge("PEM_read_RSA_PUBKEY Error");
return -3;
}
outDataLen = RSA_public_decrypt(encryptDataLen, (const unsigned char*)pEncryptData, pOutData, pRSAPublicKey, RSA_PKCS1_PADDING);
RSA_free(pRSAPublicKey);
fclose(hPubKeyFile);
CRYPTO_cleanup_all_ex_data();
return 0;
}
四 私钥加密
int PrivateKeyEncrypt(char* privateKeyFile, unsigned char* pRawData, int rawDataLen, unsigned char* pOutData, int& outDataLen)
{
if(privateKeyFile == NULL || pRawData == NULL || pOutData == NULL || rawDataLen == 0)
{
hloge("Decrypt para Error");
return -1;
}
FILE* hPriKeyFile = fopen(privateKeyFile, "rb");
if( hPriKeyFile == NULL )
{
hloge("fopen privateKeyFile fail");
return -2;
}
RSA* pRSAPriKey = RSA_new();
if(PEM_read_RSAPrivateKey(hPriKeyFile, &pRSAPriKey, 0, 0) == NULL)
{
hloge("PEM_read_RSAPrivateKey Error");
return -3;
}
outDataLen = RSA_private_encrypt(rawDataLen, (const unsigned char*)pRawData, pOutData, pRSAPriKey, RSA_PKCS1_PADDING);
RSA_free(pRSAPriKey);
fclose(hPriKeyFile);
CRYPTO_cleanup_all_ex_data();
return 0;
}
五 私钥解密
int PublicKeyDecrypt(char* publicKeyFile, unsigned char* pEncryptData, int encryptDataLen, unsigned char* pOutData, int& outDataLen)
{
if(publicKeyFile == NULL || pEncryptData == NULL || pOutData == NULL || encryptDataLen == 0)
{
hloge("Encrypt para Error");
return -1;
}
FILE* hPubKeyFile = fopen(publicKeyFile, "rb");
if( hPubKeyFile == NULL )
{
hloge("fopen publicKeyFile fail");
return -2;
}
RSA* pRSAPublicKey = RSA_new();
if(PEM_read_RSAPublicKey(hPubKeyFile, &pRSAPublicKey, 0, 0) == NULL)
{
hloge("PEM_read_RSA_PUBKEY Error");
return -3;
}
outDataLen = RSA_public_decrypt(encryptDataLen, (const unsigned char*)pEncryptData, pOutData, pRSAPublicKey, RSA_PKCS1_PADDING);
RSA_free(pRSAPublicKey);
fclose(hPubKeyFile);
CRYPTO_cleanup_all_ex_data();
return 0;
}