免费注册 查看新帖 |

Chinaunix

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

openssl求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-25 13:12 |只看该作者 |倒序浏览
20可用积分
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <openssl/bio.h>
  4. #include <openssl/rsa.h>
  5. #include <openssl/pem.h>

  6. int main(int argc, char** argv)
  7. {
  8. BIO *bpub;
  9. BIO *bpri;
  10. RSA *pRSA;
  11. RSA *rsa_rpu;
  12. FILE *fp = NULL;
  13. unsigned char *encryptedString = NULL;
  14. unsigned char *plainText= NULL;
  15.   
  16. bpub = BIO_new_file("public.rsa", "w");
  17. if (!bpub)
  18.    printf("%s","failed to create public bio file\n");

  19. bpri = BIO_new_file("private.rsa", "w");
  20. if (!bpri)
  21.    printf("%s","failed to create private bio file\n");

  22. if (!bpub || !bpri)
  23.         return -1;

  24. pRSA = RSA_generate_key( 1024, RSA_F4, NULL, NULL);
  25. if (pRSA != NULL) {
  26.    if (!PEM_write_bio_RSAPublicKey(bpub, pRSA) )
  27.             printf("%s","PEM_write_bio_RSAPublicKey: failed\n");

  28.   /*
  29.    int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
  30.                                    unsigned char *kstr, int klen,
  31.                                    pem_password_cb *cb, void *u);
  32.            */

  33.   if (!PEM_write_bio_RSAPrivateKey(bpri, pRSA, EVP_des_ede3_cbc(), NULL, 0, 0, "Private"))
  34.             printf("%s","PEM_write_bio_PrivateKey: failed\n");
  35. }

  36.   if (bpub)
  37.     BIO_free(bpub);
  38.   if (bpri)
  39.     BIO_free(bpri);
  40.   if (pRSA)
  41.           free(pRSA);
  42.    
  43.   printf("done.\n");
  44.    
  45.   
  46.   bpri = BIO_new_file("private.rsa", "r");
  47.   if(bpri==NULL)
  48.   {
  49.    printf("%s\n", "open private.rsa error");       
  50.    return -1;       
  51.           }
  52.   pRSA = PEM_read_bio_RSAPrivateKey(bpri, NULL, NULL, NULL);
  53.   if (pRSA==NULL){
  54.     printf("%s\n","Reading of private key failed");
  55.    }else{
  56.       printf("%s\n","Reading of private key successful");
  57.     }
  58.   
  59.   bpub = BIO_new_file("public.rsa", "r");
  60.   if(bpub==NULL)
  61.   {
  62.    printf("%s\n", "open public.rsa error");       
  63.    return -1;       
  64.           }
  65.   rsa_rpu = PEM_read_bio_RSA_PUBKEY(bpub,NULL, NULL, NULL);
  66. //printf("rsa_rpu is: %s",rsa_rpu);
  67. if (rsa_rpu==NULL){
  68.      printf("rsa_rpu is:%s\n","Reading of public key failed");
  69.   }
  70. else{
  71.      printf("rsa_rpu is:%s\n","Reading of public key successful");
  72. }

  73.   encryptedString=(unsigned char *)malloc(RSA_size(rsa_rpu));
  74.   RSA_blinding_off(rsa_rpu);
  75.   if (RSA_public_encrypt(strlen("TrialString")+1,(unsigned char*)"TrialString",(unsigned char*)encryptedString,rsa_rpu,RSA_PKCS1_PADDING)==-1){
  76.      printf("%s\n","encryption failed ");
  77.    }
  78.   else{
  79.      printf("%s\n", "Encryption success");
  80.   }
  81.   
  82. plainText=(unsigned char *)malloc(RSA_size(pRSA));
  83. if (RSA_private_decrypt(RSA_size(pRSA),encryptedString,(unsigned char*)plainText,pRSA,RSA_PKCS1_PADDING)==-1){
  84.      printf("%s\n","Decryption failed ");
  85. }
  86. else{
  87.      printf("%s\n","Decryption success");
  88. }

  89. printf("Plain text:%s\n",plainText);
  90.   
  91.     return 0;
  92. }

复制代码
生成public.rsa和private.rsa后,将public.rsa发给B,自己留private.rsa!!再加解密
为什么从保持有private key的private.rsa中读取RSA会失败呢?
PEM_read_bio_RSAPrivateKey和PEM_read_RSAPrivateKey都尝试过了...

public key 和public.rsa也是一样失败...
因为实际通过过程中肯定是把public.rsa发给用户的...


而我write file的时候使用PEM_write_RSA_PUBKEY和PEM_write_RSAPrivateKey,整个过程就是可以的....

论坛徽章:
0
2 [报告]
发表于 2010-04-25 21:38 |只看该作者
问题更新下:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <openssl/bio.h>
  4. #include <openssl/rsa.h>
  5. #include <openssl/pem.h>

  6. int main(int argc, char** argv)
  7. {
  8.         int i=0;
  9.         unsigned char *encryptedString = NULL;
  10.         BIO *bpub;
  11.         RSA *pRSA;
  12.         bpub = BIO_new_file("public.rsa", "r");
  13.         if (!bpub)
  14.         {
  15.                 printf("failed to create public bio file\n");
  16.         }
  17.        
  18.         pRSA = PEM_read_bio_RSAPublicKey(bpub,NULL,NULL,NULL);
  19.         if(pRSA == NULL)
  20.         {
  21.                 printf("failed to get public key\n");
  22.         }               
  23.         else
  24.         {
  25.                 printf("succeed to get public key\n");
  26.         //        printf("pRSA->d %ulld\n",pRSA->d->d);
  27.         }
  28.         RSA_print_fp(stdout, pRSA, 0);
  29.         encryptedString=(unsigned char *)malloc(2048);
  30.         memset(encryptedString,0,2048);
  31.         if (RSA_public_encrypt(strlen("123456")+1,(unsigned char*)"123456",(unsigned char*)encryptedString,pRSA,RSA_PKCS1_PADDING)==-1)
  32.         {
  33.                 printf("%s\n","encryption failed ");
  34.         }
  35.         else{
  36.                 printf("%s\n", "Encryption success");
  37.                 for(i=0;i<strlen(encryptedString);i++)
  38.                 {
  39.                         if(i%16 == 0)
  40.                                 printf("\n");
  41.                         printf("%02x ",encryptedString[i]);
  42.                 }
  43.                 printf("\n");
  44.         }

  45. }
复制代码
public.rsa内容为
  1. -----BEGIN RSA PUBLIC KEY-----
  2. MIGHAoGBAOn3yXJdq5zWlqpdy1IC3lf9sjPGfvhaEqY4tGbL5mpuvnyETw1zAsap
  3. nB5kaNg8jeSyhBLfLsb4T9Ru8PXcjXsVdnEjnCy0FH+su7jWlJII5YcUUsTORM8q
  4. 1w+PFReNME6+Kradxu0l8799uqPvAk4EAgd8xDTdpLiWVk5S32O5AgER
  5. -----END RSA PUBLIC KEY-----
复制代码
每次RSA_print打印出的内容是一样的!
奇迹这个时候发生了,每次运行这个程序打印出的加密的值不一样

还真没遇到过

论坛徽章:
0
3 [报告]
发表于 2010-04-25 22:13 |只看该作者
弄清楚了
使用了随机数作为pading填充,rsa有很多种填充方式如pkcs等,解密肯定能得到明文,这样做是为了防止别人通过截获相同的密文数据进行分析破解rsa密钥
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP