Linux C语言 使用Openssl对数据进行RSA加密的实现

该代码示例展示了如何使用RSA算法进行数据加密和解密。首先读取RSA的公钥和私钥文件,然后使用公钥加密原始数据,最后用私钥解密加密后的数据。加密和解密过程中采用了PKCS1_OAEP_PADDING填充方式。
摘要由CSDN通过智能技术生成
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/sha.h> 
#include <openssl/crypto.h> 



#define PUBLIC_KEY_PATH  ("./rsa_public_key.pem")
#define PRIVATE_KEY_PATH ("./rsa_private_key.pem")

#define isUseSha256    (1)

#if isUseSha256
#define SHA_WHICH        NID_sha256
#define WHICH_DIGEST_LENGTH    SHA256_DIGEST_LENGTH
#else
#define SHA_WHICH        NID_sha512
#define WHICH_DIGEST_LENGTH    SHA512_DIGEST_LENGTH
#endif


void printHex(unsigned char *md, int len)
{

    int i = 0;
    for (i = 0; i < len; i++)
    {
        printf("%02x", md[i]);
    }

    printf("\n");
}

/*读取私钥*/
RSA* ReadPrivateKey(char* p_KeyPath)
{   
    FILE *fp = NULL; 
    RSA  *priRsa = NULL;

    printf("PrivateKeyPath[%s] \n", p_KeyPath);

    /*  打开密钥文件 */
    if(NULL == (fp = fopen(p_KeyPath, "r")))
    {
        printf( "fopen[%s] failed \n", p_KeyPath);
        return NULL;
    }
    /*  获取私钥 */
    priRsa = PEM_read_RSAPrivateKey(fp, NULL, NULL,NULL);
    if(NULL == priRsa)
    {
 //       ERR_print_errors_fp(stdout);
        printf( "PEM_read_RSAPrivateKey\n");
        fclose(fp);
        return NULL;
    }
    fclose(fp);

    return priRsa;
}

/*读取公匙*/
RSA* ReadPublicKey(char* p_KeyPath)
{   
    FILE *fp = NULL; 
    RSA *pubRsa = NULL;

    printf("PublicKeyPath[%s]\n", p_KeyPath);

    /*  打开密钥文件 */
    if(NULL == (fp = fopen(p_KeyPath, "r")))
    {
        printf( "fopen[%s] \n", p_KeyPath);
        return NULL;
    }
    /*  获取公钥 */
    if(NULL == (pubRsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL,NULL)))
    {
        printf( "PEM_read_RSAPrivateKey error\n");
        fclose(fp);
        return NULL;
    }
    fclose(fp);

    return pubRsa;
}


void RSA_encrypt_decrypt()
{
     unsigned  char *data = "china";
    unsigned char buf[128] = {0};
    RSA *pubKey = NULL;
    RSA *privKey = NULL;
    int nOutLen = sizeof(buf);
    int nRet = 0;

  
    // 2. 读取私钥
    privKey = ReadPrivateKey(PRIVATE_KEY_PATH);
    if (!privKey) 
    {  
   //     ERR_print_errors_fp (stderr);    
        return -1;  
    }

    // 3. 读取公匙
    pubKey = ReadPublicKey(PUBLIC_KEY_PATH);  
    if(!pubKey)
    {
        RSA_free(privKey);   
        printf("Error: can't load public key");
        return -1;
    }

    printf("before encrypt str is %s\n",data);
    nRet=RSA_public_encrypt(strlen(data),data,buf,pubKey,RSA_PKCS1_OAEP_PADDING);
    if(-1==nRet)
    {
        RSA_free(privKey);
        RSA_free(pubKey);
        printf("encrypt error\n");
        return;
    }

    printf("encrypt length is %d\n",nRet);
    printHex(buf, nOutLen);
   
    char *out[128]={0};
    nRet=RSA_private_decrypt(128,buf,out,privKey,RSA_PKCS1_OAEP_PADDING);
     if(-1==nRet)
    {
     RSA_free(privKey);
    RSA_free(pubKey);

    printf("encrypt error\n");
        return;
    }
    printf("after decrypt str is %s\n",out);
    printf("decrypt success");

    RSA_free(privKey);
    RSA_free(pubKey);

    return 0;

}




int main(int argc, char *argv[])
{
    RSA_encrypt_decrypt();
    return 0;
}

执行结果:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值