RSA加解密源码

源码:

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
#include<assert.h>
#include<openssl/rsa.h> 
#include<openssl/pem.h> 
#include<openssl/err.h>

 
 typedef struct{
 	unsigned char encData[128];
 	unsigned char encData2[128];
 }ENC_DATA;
 
 
static  char *MODULUS="9EC7D9A2DC5B095F8E5F90295121F41262FAEFBE9AF57B772A71F1F9D9635F8769CB78DA2BCFE9B27FC1F3AD4A3D178F8C61981225EF5DEACBDC5665F12E691AA13DDD321A59CFCF376F002036612FF3C5E057A3007FF675AFA3EDE34DC23A1A2637294870EBE823F76B5CE21E25F3FA5137F5DE12437DE0118245B927B28221";  
static char *PRIVATE="8B26E30ECA6E8F3668F6FA78B0C55FB75A4A3FAD0667B152933A4991D7A815D1498F5E1EF44ACEF6CDF252E56F367DED5BA024DF6B267B7E36BD35552DFA0A4CC1E9D0A4BC8E7C76F98D4971441D6693745A0A76E175571BD160E4B1536A6EFF5A08EDA45236E96E7A4748CF4D031CA8B2F4CCE9F2E1286F432DE6495A535E43";    
/**************************************
功能:获取RSA结构指针
输入:无
输出:无
返回:RSA结构指针
**************************************/
static RSA *get_key()
{
	  RSA *r; 
	  int ret; 
    BIGNUM *bne,*bnn,*bnd;  
  
    //使用的密匙数据      
    unsigned long e = 75011;      
    //构建RSA数据结构     
    bne = BN_new();  
    bnd = BN_new();  
    bnn = BN_new();  
    ret = BN_set_word(bne, e);  
    BN_hex2bn(&bnd, PRIVATE);  
    BN_hex2bn(&bnn, MODULUS);  
  
    r = RSA_new();  
    r->e=bne;  
    r->d=bnd;  
    r->n=bnn;  
    RSA_print_fp(stdout, r, 5);  
  
     return r; 
}

/**************************************
功能:加密
输入:r: RSA结构指针
      in: 输入需要加密的字符串
输出:无
返回:加密后的字符串
**************************************/
static unsigned char *RSA_encrypt(RSA *r, unsigned char *in)
{  

    int ret, flen, i;    
    
    unsigned  char *encData, *tmpData;//加密后的数据/临时指针   
 
  
    //准备输出的加密数据结构      
    flen =  RSA_size(r);  
    encData =  (unsigned char *)malloc(flen);  
    bzero(encData, flen);  
    printf("Begin RSA_private_encrypt ...\n");  
    ret =  RSA_private_encrypt(flen, in, encData, r,  RSA_NO_PADDING);  
    if(ret < 0)
    {  
      printf("Encrypt failed!\n");  
      return;     
    }  
    printf("Size:%d\n", ret);     
    printf("ClearText:%s\n", in);      
    printf("CipherText(Hex):\n");     
    tmpData=encData;  
    for  (i=0; i<ret; i++)
    {          
    	printf("0x%02x, ", *tmpData);        
    	tmpData++;  
    } 
     
    return encData;
 }
 /**************************************
功能:解密
输入:r: RSA结构指针
      in: 输入需要解密的字符串
输出:无
返回:解密后的字符串
**************************************/
 static unsigned char *RSA_decrypt(RSA *r, unsigned char *in)
 {    
    int ret, flen;    
    unsigned  char *decData;  
     
    flen =  RSA_size(r);  
    decData =  (unsigned char *)malloc(flen); 
    bzero(decData, flen); 
       
    printf("Begin RSA_public_decrypt ...\n"); 
    ret =  RSA_public_decrypt(flen, in, decData, r,  RSA_NO_PADDING);  
    if(ret < 0)
    {  
    	  printf("RSA_public_decrypt failed!\n");  
    	  return;  
    }  
    
 
    printf("Size:%d\n", ret); 
    printf("ClearText:%s\n", decData);   

    
    
    return decData;
  
}  


int main(void) {  
	  int i = 0;
	  
	  unsigned char *in1 = "test2", *in2="test3";
	  
	  unsigned char *enc1 = NULL, *enc2 = NULL;
	  unsigned char *dec1 = NULL, *dec2 = NULL;
	  
	  RSA *r = get_key();
	  
    FILE *tmpfp = fopen("a.bin", "wb+");
    ENC_DATA tmp_Data;
    memset(&tmp_Data, 0, sizeof(ENC_DATA));
    //加密
    enc1 = RSA_encrypt(r, in1);
    enc2 = RSA_encrypt(r, in2);
    
    
    unsigned char *tmpData = enc1;
    for  (i=0; i<128; i++)
    {          
    	tmp_Data.encData[i] = *tmpData;     
    	tmpData++;  
    } 
    tmpData = enc2;
    for  (i=0; i<128; i++)
    {          
    	tmp_Data.encData2[i] = *tmpData;     
    	tmpData++;  
    } 
    fwrite(&tmp_Data,1, sizeof(ENC_DATA), tmpfp);
    free(enc1);
    enc1 = NULL;
    free(enc2);
    enc2 = NULL;
    fclose(tmpfp);
    RSA_free(r);
    
    //解密
    r = get_key();  
    tmpfp = fopen("a.bin", "rb");
    memset(&tmp_Data, 0, sizeof(ENC_DATA));
    fread(&tmp_Data,1, sizeof(ENC_DATA), tmpfp);
    dec1 = RSA_decrypt(r, tmp_Data.encData);
    dec2 = RSA_decrypt(r, tmp_Data.encData2);
     free(dec1);
    dec1 = NULL;
    free(dec2);
    dec2 = NULL;
    fclose(tmpfp);  
  
    RSA_free(r);  
    
    return EXIT_SUCCESS;  
 }  


编译:

 gcc -o test test_rsa.c -I/home/openssl/include/ -L/home/openssl/lib -lcrypto -lssl -ldl


 

你可能感兴趣的:(数据结构,加密,struct,解密,null,hex)