OpenSSL 编程 - RSA 加密解密

这篇博客记录了作者使用OpenSSL进行RSA加密解密的实践经验,分享了一段试验代码,供读者参考。
摘要由CSDN通过智能技术生成

这几天做这方面的东西,网上资料很少,贴一个自己试验写的代码,做个记录。

加密:

/*
gcc -o rsa-encrypt rsa-encrypt.c -lcrypto
*/

#include 
< openssl / rsa.h >
#include 
< openssl / err.h >

#define  MODULUS "C8FBCF21"
#define  PUBLIC_EXPONENT RSA_F4
#define  PRIVATE_EXPONENT "97B55D7D"

int  main()
{
    
int ret, flen;
    BIGNUM 
*bnn, *bne, *bnd;
    unsigned 
char *in = "abc";
    unsigned 
char *out;

    bnn 
= BN_new();
    bne 
= BN_new();
    bnd 
= BN_new();
    BN_hex2bn(
&bnn, MODULUS);
    BN_set_word(bne, PUBLIC_EXPONENT);
    BN_hex2bn(
&bnd, PRIVATE_EXPONENT);

    RSA 
*= RSA_new();
    r
->= bnn;
    r
->= bne;
    r
->= bnd;
    RSA_print_fp(stdout, r, 
5);

    flen 
= RSA_size(r);// - 11;
    out = (char *)malloc(flen);
    bzero(
out, flen);
    
//memset(out, 0, flen);

    printf(
"Begin encrypt... ");
    ret 
= RSA_private_encrypt(flen, inout, r,  RSA_NO_PADDING);
    
if (ret < 0)
    
{
        printf(
"Encrypt failed! ");
        
return 1;
    }


    printf(
"Size:%d ", ret);
    printf(
"ClearText:%s "in);
    printf(
"CipherText(Hex):");
    
int i;
    
for (i=0; i<ret; i++)
    
{
        printf(
"0x%02x, "*out);
        
out++;
    }

    printf(
" ");

    
//free(out);
    RSA_free(r);
    
return 0;
}


 

解密:

 

/*
gcc -o rsa-decrypt rsa-decrypt.c -lcrypto
*/

#include 
< openssl / rsa.h >

#define  MODULUS "C8FBCF21"
#define  PUBLIC_EXPONENT RSA_F4
#define  PRIVATE_EXPONENT "97B55D7D"

int  main()
{
    
int ret, flen;
    BIGNUM 
*bnn, *bne;
    unsigned 
char in[] = {0x980x790xb20x76};
    unsigned 
char *out;

    bnn 
= BN_new();
    bne 
= BN_new();
    BN_hex2bn(
&bnn, MODULUS);
    BN_set_word(bne, PUBLIC_EXPONENT);

    RSA 
*= RSA_new();
    r
->= bnn;
    r
->= bne;
    RSA_print_fp(stdout, r, 
5);

    flen 
= RSA_size(r);
    
out = (unsigned char *)malloc(flen);
    bzero(
out, flen);

    printf(
"Begin decrypt... ");
    ret 
= RSA_public_decrypt(sizeof(in), inout, r, RSA_NO_PADDING);
    
if (ret < 0)
    
{
        printf(
"Decrypt failed! ");
        
return 1;
    }


    printf(
"Size:%d ", ret);
    printf(
"ClearText:%s "out);

    free(
out);
    RSA_free(r);
    
return 0;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
OpenSSL是一个广泛使用的开放源代码工具包,提供加密解密功能。它支持DES和RSA加密算法,可以用来加密解密数据。DES是一种对称加密算法,使用相同的密钥进行加密解密RSA是一种非对称加密算法,使用公钥进行加密,私钥进行解密。 使用OpenSSL进行DES加密解密,首先需要生成一个DES密钥,然后使用该密钥进行加密解密操作。可以使用以下命令生成DES密钥: ``` openssl rand -out des.key 8 ``` 生成的密钥保存在des.key文件中。然后使用该密钥进行加密解密: ``` openssl enc -des -in plaintext.txt -out encrypted.des -kfile des.key openssl enc -d -des -in encrypted.des -out decrypted.txt -kfile des.key ``` 上述命令分别用指定的DES密钥对明文文件plaintext.txt进行加密,然后将加密结果保存到encrypted.des文件中。解密操作则相反,使用相同的DES密钥对加密后的文件进行解密,得到明文文件decrypted.txt。 而要使用RSA算法进行加密解密,首先需要生成RSA密钥对(公钥和私钥),然后使用公钥进行加密,私钥进行解密。可以使用以下命令生成RSA密钥对: ``` openssl genrsa -out private.pem 1024 openssl rsa -in private.pem -pubout -out public.pem ``` 生成的私钥保存在private.pem文件中,公钥保存在public.pem文件中。然后使用公钥进行加密,私钥进行解密: ``` openssl rsautl -encrypt -in plaintext.txt -out encrypted.rsa -inkey public.pem openssl rsautl -decrypt -in encrypted.rsa -out decrypted.txt -inkey private.pem ``` 上述命令分别使用指定的公钥对明文文件plaintext.txt进行加密,然后将加密结果保存到encrypted.rsa文件中。解密操作则相反,使用相同的私钥对加密后的文件进行解密,得到明文文件decrypted.txt。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值