OpenSSL 编程 - RSA 加密解密

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[] = {0x98, 0x79, 0xb2, 0x76};
     unsigned char *out;

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

     RSA *r = RSA_new();
     r-&gt;n = bnn;
     r-&gt;e = 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), in, out, 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;
}

解密:

/*
gcc -o rsa-decrypt rsa-decrypt.c -lcrypto
*/
#include &lt;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[] = {0x98, 0x79, 0xb2, 0x76};
     unsigned char *out;

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

     RSA *r = RSA_new();
     r-&gt;n = bnn;
     r-&gt;e = 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), in, out, r, RSA_NO_PADDING);
if (ret &lt; 0)
{
         printf("Decrypt failed! ");
return 1;
     }

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

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

 

你可能感兴趣的:(rsa,解密,职场,OpenSSL,休闲)