这几天做这方面的东西,网上资料很少,贴一个自己试验写的代码,做个记录。
加密:
/**/
/*
gcc -o rsa-encrypt rsa-encrypt.c -lcrypto
*/
#include
<
openssl
/
rsa.h
>
#include
<
openssl
/
err.h
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#define
MODULUS "C8FBCF21"
#define
PUBLIC_EXPONENT RSA_F4
#define
PRIVATE_EXPONENT "97B55D7D"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
int
main()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
int ret, flen;
BIGNUM *bnn, *bne, *bnd;
unsigned char *in = "abc";
unsigned char *out;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
bnn = BN_new();
bne = BN_new();
bnd = BN_new();
BN_hex2bn(&bnn, MODULUS);
BN_set_word(bne, PUBLIC_EXPONENT);
BN_hex2bn(&bnd, PRIVATE_EXPONENT);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
RSA *r = RSA_new();
r->n = bnn;
r->e = bne;
r->d = bnd;
RSA_print_fp(stdout, r, 5);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
flen = RSA_size(r);// - 11;
out = (char *)malloc(flen);
bzero(out, flen);
//memset(out, 0, flen);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
printf("Begin encrypt... ");
ret = RSA_private_encrypt(flen, in, out, r, RSA_NO_PADDING);
if (ret < 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
printf("Encrypt failed! ");
return 1;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
printf("Size:%d ", ret);
printf("ClearText:%s ", in);
printf("CipherText(Hex):");
int i;
for (i=0; i<ret; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
printf("0x%02x, ", *out);
out++;
}
printf(" ");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//free(out);
RSA_free(r);
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
解密:
/**/
/*
gcc -o rsa-decrypt rsa-decrypt.c -lcrypto
*/
#include
<
openssl
/
rsa.h
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#define
MODULUS "C8FBCF21"
#define
PUBLIC_EXPONENT RSA_F4
#define
PRIVATE_EXPONENT "97B55D7D"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
int
main()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
int ret, flen;
BIGNUM *bnn, *bne;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
unsigned char in[] = ...{0x98, 0x79, 0xb2, 0x76};
unsigned char *out;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
bnn = BN_new();
bne = BN_new();
BN_hex2bn(&bnn, MODULUS);
BN_set_word(bne, PUBLIC_EXPONENT);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
RSA *r = RSA_new();
r->n = bnn;
r->e = bne;
RSA_print_fp(stdout, r, 5);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
flen = RSA_size(r);
out = (unsigned char *)malloc(flen);
bzero(out, flen);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
printf("Begin decrypt... ");
ret = RSA_public_decrypt(sizeof(in), in, out, r, RSA_NO_PADDING);
if (ret < 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
printf("Decrypt failed! ");
return 1;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
printf("Size:%d ", ret);
printf("ClearText:%s ", out);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
free(out);
RSA_free(r);
return 0;
}