test_openssl

#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>

int main(int argc, char** argv)
{
BIO *bpub;
BIO *bpri;
RSA *pRSA;
RSA *rsa_rpu;
FILE *fp = NULL;
unsigned char *encryptedString = NULL;
unsigned char *plainText= NULL;
 
bpub = BIO_new_file("public.rsa", "w");
if (!bpub)
   printf("%s","failed to create public bio file\n");

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

if (!bpub || !bpri)
        return -1;

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

  /*
   int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
                                   unsigned char *kstr, int klen,
                                   pem_password_cb *cb, void *u);
           */

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

  if (bpub)
    BIO_free(bpub);
  if (bpri)
    BIO_free(bpri);
  if (pRSA)
          free(pRSA);
  
  printf("done.\n");
   
 
  bpri = BIO_new_file("private.rsa", "r");
  if(bpri==NULL)
  {
   printf("%s\n", "open private.rsa error");       
   return -1;       
          }
  pRSA = PEM_read_bio_RSAPrivateKey(bpri, NULL, NULL, NULL);
  if (pRSA==NULL){
    printf("%s\n","Reading of private key failed");
   }else{
      printf("%s\n","Reading of private key successful");
    }
 
  bpub = BIO_new_file("public.rsa", "r");
  if(bpub==NULL)
  {
   printf("%s\n", "open public.rsa error");       
   return -1;       
          }
  rsa_rpu = PEM_read_bio_RSA_PUBKEY(bpub,NULL, NULL, NULL);
//printf("rsa_rpu is: %s",rsa_rpu);
if (rsa_rpu==NULL){
     printf("rsa_rpu is:%s\n","Reading of public key failed");
  }
else{
     printf("rsa_rpu is:%s\n","Reading of public key successful");
}

  encryptedString=(unsigned char *)malloc(RSA_size(rsa_rpu));
  RSA_blinding_off(rsa_rpu);
  if (RSA_public_encrypt(strlen("TrialString")+1,(unsigned char*)"TrialString",(unsigned char*)encryptedString,rsa_rpu,RSA_PKCS1_PADDING)==-1){
     printf("%s\n","encryption failed ");
   }
  else{
     printf("%s\n", "Encryption success");
  }
 
plainText=(unsigned char *)malloc(RSA_size(pRSA));
if (RSA_private_decrypt(RSA_size(pRSA),encryptedString,(unsigned char*)plainText,pRSA,RSA_PKCS1_PADDING)==-1){
     printf("%s\n","Decryption failed ");
}
else{
     printf("%s\n","Decryption success");
}

printf("Plain text:%s\n",plainText);
 
    return 0;
}

生成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,整个过程就是可以的....

 

#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>

int main(int argc, char** argv)
{
        int i=0;
        unsigned char *encryptedString = NULL;
        BIO *bpub;
        RSA *pRSA;
        bpub = BIO_new_file("public.rsa", "r");
        if (!bpub)
        {
                printf("failed to create public bio file\n");
        }
       
        pRSA = PEM_read_bio_RSAPublicKey(bpub,NULL,NULL,NULL);
        if(pRSA == NULL)
        {
                printf("failed to get public key\n");
        }               
        else
        {
                printf("succeed to get public key\n");
        //        printf("pRSA->d %ulld\n",pRSA->d->d);
        }
        RSA_print_fp(stdout, pRSA, 0);
        encryptedString=(unsigned char *)malloc(2048);
        memset(encryptedString,0,2048);
        if (RSA_public_encrypt(strlen("123456")+1,(unsigned char*)"123456",(unsigned char*)encryptedString,pRSA,RSA_PKCS1_PADDING)==-1)
        {
                printf("%s\n","encryption failed ");
        }
        else{
                printf("%s\n", "Encryption success");
                for(i=0;i<strlen(encryptedString);i++)
                {
                        if(i%16 == 0)
                                printf("\n");
                        printf("%02x ",encryptedString[i]);
                }
                printf("\n");
        }

}

public.rsa内容为

-----BEGIN RSA PUBLIC KEY-----
MIGHAoGBAOn3yXJdq5zWlqpdy1IC3lf9sjPGfvhaEqY4tGbL5mpuvnyETw1zAsap
nB5kaNg8jeSyhBLfLsb4T9Ru8PXcjXsVdnEjnCy0FH+su7jWlJII5YcUUsTORM8q
1w+PFReNME6+Kradxu0l8799uqPvAk4EAgd8xDTdpLiWVk5S32O5AgER
-----END RSA PUBLIC KEY-----

 

每次RSA_print打印出的内容是一样的!
使用了随机数作为pading填充,rsa有很多种填充方式如pkcs等,解密肯定能得到明文,这样做是为了防止别人通过截获相同的密文数据进行分析破解rsa密钥

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值