免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6254 | 回复: 2
打印 上一主题 下一主题

分享:RSA非对称数据加密算法使用示例 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-19 14:59 |只看该作者 |倒序浏览
如题!

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <openssl/rsa.h>
  4. #include <openssl/applink.c>

  5. void print_buf_hex( unsigned char *buf, int len )
  6. {
  7.     int i;

  8.     for( i = 0; i < len; i++ )
  9.     {
  10.         printf( "%02x", buf[i] );
  11.     }
  12. }

  13. int main( int argc, char *argv[] )
  14. {
  15.     RSA           *rsa;
  16.     int            n;
  17.     unsigned char  text[128], cipher[128];
  18.     unsigned char  rsa_n[128], rsa_d[128];

  19.     // 生成密钥
  20.     rsa = RSA_generate_key( 1024, RSA_F4, NULL, NULL );
  21.     if( NULL == rsa )
  22.     {
  23.         printf( "main.RSA_generate_key\n" );
  24.         return -1;
  25.     }
  26.     n = RSA_size( rsa );
  27.     BN_bn2bin( rsa->n, rsa_n ); // 保存公钥
  28.     BN_bn2bin( rsa->d, rsa_d ); // 保存私钥
  29.     RSA_free( rsa );
  30.     printf( "RSA_size: %d\n\npublic modulus:\n", n );
  31.     print_buf_hex( rsa_n, sizeof(rsa_n) );
  32.     printf( "\n\nprivate exponent:\n" );
  33.     print_buf_hex( rsa_d, sizeof(rsa_d) );
  34.     printf( "\n\n" );

  35.     // 加密的过程
  36.     rsa = RSA_new();
  37.     if( NULL == rsa )
  38.     {
  39.         printf( "main.RSA_new for encrypt\n" );
  40.         return -1;
  41.     }
  42.     // 设置公钥
  43.     rsa->n = BN_bin2bn( rsa_n, sizeof(rsa_n), rsa->n );
  44.     rsa->e = BN_bin2bn( "\x01\x00\x01", 3, rsa->e );
  45.     // 设置明文并加密
  46.     for( n = 0; n < sizeof(text); n++ )
  47.     {
  48.         text[n] = n;
  49.     }
  50.     n = RSA_public_encrypt( sizeof(text), text, cipher, rsa, RSA_NO_PADDING );
  51.     RSA_free( rsa );
  52.     printf( "RSA_public_encrypt: %d\ncipher:\n", n );
  53.     print_buf_hex( cipher, sizeof(cipher) );
  54.     printf( "\n\n" );

  55.     // 解密的过程
  56.     rsa = RSA_new();
  57.     if( NULL == rsa )
  58.     {
  59.         printf( "main.RSA_new for decrypt\n" );
  60.         return -1;
  61.     }
  62.     // 设置公钥及私钥
  63.     rsa->n = BN_bin2bn( rsa_n, sizeof(rsa_n), rsa->n );
  64.     rsa->e = BN_bin2bn( "\x01\x00\x01", 3, rsa->e );
  65.     rsa->d = BN_bin2bn( rsa_d, sizeof(rsa_d), rsa->d );
  66.     // 解密数据
  67.     n = RSA_private_decrypt( sizeof(cipher), cipher, text, rsa,
  68.                              RSA_NO_PADDING );
  69.     RSA_free( rsa );
  70.     printf( "RSA_private_decrypt: %d\ntext:\n", n );
  71.     print_buf_hex( text, sizeof(text) );
  72.     putchar( '\n' );

  73.     return 0;
  74. }
复制代码

[ 本帖最后由 guoruimin 于 2009-5-12 09:32 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-12-19 15:00 |只看该作者
不是不全,是你没找到,openssl的文档水木上就有,相当于man手册。

论坛徽章:
0
3 [报告]
发表于 2008-12-19 15:03 |只看该作者
原帖由 cugb_cat 于 2008-12-19 15:00 发表
不是不全,是你没找到,openssl的文档水木上就有,相当于man手册。

惭愧了,多谢指点!望大家多多指正,相互交流。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP