用OpenSSL实现RSA算法加密(转)

 1: #include <openssl/err.h>
 2:  
 3: #define MODULUS "C8FBCF21"
 4: #define PUBLIC_EXPONENT RSA_F4
 5: #define PRIVATE_EXPONENT "97B55D7D"
 6:  
 7: int main()
 8: {
  
 9:  int ret, flen;
 10: BIGNUM *bnn, *bne, *bnd;
 11: unsigned char *in = "abc";
 12: unsigned char *out;
 13:  
 14: bnn = BN_new();
 15: bne = BN_new();
 16: bnd = BN_new();
 17: BN_hex2bn(&bnn, MODULUS);
 18: BN_set_word(bne, PUBLIC_EXPONENT);
 19: BN_hex2bn(&bnd, PRIVATE_EXPONENT);
 20:  
 21: RSA *r = RSA_new();
 22: r->n = bnn;
 23: r->e = bne;
 24: r->d = bnd;
 25: RSA_print_fp(stdout, r, 5);
 26:  
 27: flen = RSA_size(r);// - 11;
 28:  out = (char *)malloc(flen);
 29: bzero(out, flen);
 30:  //memset(out, 0, flen);
 31:  
 32: printf("Begin encrypt... ");
 33: ret = RSA_private_encrypt(flen, in, out, r, RSA_NO_PADDING);
 34:  if (ret < 0)
 35: {
  
 36: printf("Encrypt failed! ");
 37:  return 1;
 38: }
 39:  
 40: printf("Size:%d ", ret);
 41: printf("ClearText:%s ", in);
 42: printf("CipherText(Hex):");
 43:  int i;
 44:  for (i=0; i<ret; i++)
 45: {
  
 46: printf("0x%02x, ", *out);
 47:  out++;
 48: }
 49: printf(" ");
 50:  
 51:  //free(out);
 52: RSA_free(r);
 53:  return 0;
 54: }
 1: #include <openssl/err.h>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值