openssl rsa 使用

本文详细介绍了如何使用OpenSSL库进行RSA密钥生成、公钥加密与解密、私钥加密与解密的过程。提供了完整的C语言代码示例,涵盖了从创建密钥文件到数据加密解密的整个流程。

// 头文件 
#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值