Openssl --RSA加密算法的使用。

来源:互联网 发布:手机屏幕画笔软件 编辑:程序博客网 时间:2024/05/19 11:48

        RSA是一个非对称加密算法。简单说来,非对称加密算法就是说加密解密一个文件需要有两个密钥,一个用来加密,为公钥,一个用来解密,为私钥。证书可以用来授权公钥的使用。

其主要数据结构以及函数
RSA数据结构,其中包含公钥信息,如果只有n和e则表明是公钥信息
struct rsa_st
{
/* The first parameter is used to pickup errors where
* this is passed instead of aEVP_PKEY, it is set to 0 */
int pad;
long version;
const RSA_METHOD *meth;//OpenSSL默认的RSA加解密算法
/* functional reference if 'meth' is ENGINE-provided */
ENGINE *engine;
BIGNUM *n;//模数n
BIGNUM *e;//公钥指数e,通常为RSA_3(3)或RSA_F4(65537)
BIGNUM *d;//私钥指数d
BIGNUM *p;//大素数p
BIGNUM *q;//大素数q
BIGNUM *dmp1; //d mod (p-1)
BIGNUM *dmq1; //d mod (q-1)
BIGNUM *iqmp;//(inverse of q) mod p
/* be careful using this if the RSA structure is shared */
CRYPTO_EX_DATA ex_data;
int references;
int flags;


/* Used to cache montgomery values */
BN_MONT_CTX *_method_mod_n;
BN_MONT_CTX *_method_mod_p;
BN_MONT_CTX *_method_mod_q;


/* all BIGNUM values are actually in the following data, if it is not
* NULL */
char *bignum_data;
BN_BLINDING *blinding;
BN_BLINDING *mt_blinding;
};


BIGNUM:openssl定义的数据类型 抽象表示1个大数


RSA* RSA_new(void):生成一个RSA结构,并采用默认的rsa_pkcs1_eay_meth RSA_METHOD算法。成功返回1,失败返回-1


int BN_new():生成一个BIGNUM结构,成功返回1,失败返回-1


int BN_num_bytes(const BIGNUM *a);返回a的位数,大量使用


int BN_hex2bn(BIGNUM **a, const char *str);转化为10进制字符串


void BN_init(BIGNUM *);    初始化所有项均为0,一般为BN_init(&c) 


void BN_free(BIGNUM *a);   释放一个BIGNUM结构,释放完后a=NULL; 


void BN_clear(BIGNUM *a);  将a中所有项均赋值为0,但是内存并没有释放 


void BN_clear_free(BIGNUM *a); 相当与将BN_free和BN_clear综合,要不就赋值0,要不就释放空间。


int BN_set_word(BIGNUM *a, BN_ULONG w);将BIGNUM结构的值置为unsigned long int类型证书的值。成功返回1,失败返回-1


int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);参数分别为:RSA结构,bits为生成密钥的长度,公开指数e,一般为NULL。(密钥产生函数)作用:根据密钥长度和公钥指数生成密钥。


void RSA_FREE(RSA *rsa);释放RSA结构。


int RSA_print_fp(FILE *fp, const RSA *r,int offset);参数分别为:文件指针*fp,RSA结构,打印偏移量offset是为了调整输出格式的,随意一个数都可以(例如2,12,16。。)。
作用:将生成的密钥输出到文件


int RSA_check_key(const RSA *);检查RSA结构中n,e,d,p,q是否满足条件。满足返回1,否则返回-1;


int RSA_size(const RSA *);返回RSA结构中密钥的长度


加解密函数:
int RSA_public_encrypt(intflen, const unsigned char *from, unsigned char *to, RSA *rsa,int padding); 


int RSA_private_encrypt(intflen, const unsigned char *from, unsigned char *to, RSA *rsa,int padding); 


int RSA_public_decrypt(intflen, const unsigned char *from, unsigned char *to, RSA *rsa,int padding); 


int RSA_private_decrypt(intflen, const unsigned char *from, unsigned char *to, RSA *rsa,int padding);


下面是一个例子,这个例子利用已有的密钥来对source字符串进行加密与解密:


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


#define OPENSSLKEY "test.key"
#define PUBLICKEY "test_pub.key"
#define BUFFSIZE 1024
char* my_encrypt(char *str,char *path_key);//加密
char* my_decrypt(char *str,char *path_key);//解密


int main(void){
   char *source="i like dancing !";
   char *ptr_en,*ptr_de;
   printf("source is    :%s\n",source);
   ptr_en=my_encrypt(source,PUBLICKEY);
   printf("after encrypt:%s\n",ptr_en);
   ptr_de=my_decrypt(ptr_en,OPENSSLKEY);
   printf("after decrypt:%s\n",ptr_de);
   if(ptr_en!=NULL){
      free(ptr_en);
   }   
   if(ptr_de!=NULL){
      free(ptr_de);
   }   
   return 0;
}


char *my_encrypt(char *str,char *path_key){
   char *p_en;
   RSA *p_rsa;
   FILE *file;
   int flen,rsa_len;
   if((file=fopen(path_key,"r"))==NULL){
       perror("open key file error");
       return NULL;    
   }   
   if((p_rsa=PEM_read_RSA_PUBKEY(file,NULL,NULL,NULL))==NULL){
   //if((p_rsa=PEM_read_RSAPublicKey(file,NULL,NULL,NULL))==NULL){   换成这句死活通不过,无论是否将公钥分离源文件
       ERR_print_errors_fp(stdout);
       return NULL;
   }
   /*以上可以这样
      RSA* pRSAPublicKey = RSA_new();
      if(PEM_read_RSA_PUBKEY(file, &pRSAPublicKey, 0, 0) == NULL)
      {
        assert(false);
        return "";
      }
     不用以后 记得释放
     RSA_free(pRSAPublicKey);
   */


   flen=strlen(str);
   rsa_len=RSA_size(p_rsa);
   p_en=(unsigned char *)malloc(rsa_len+1);
   memset(p_en,0,rsa_len+1);
   if(RSA_public_encrypt(rsa_len,(unsigned char *)str,(unsigned char*)p_en,p_rsa,RSA_NO_PADDING)<0){
       return NULL;
   }
   RSA_free(p_rsa);
   fclose(file);
   return p_en;
}
char *my_decrypt(char *str,char *path_key){
   char *p_de;
   RSA *p_rsa;
   FILE *file;
   int rsa_len;
   if((file=fopen(path_key,"r"))==NULL){
       perror("open key file error");
       return NULL;
   }
   if((p_rsa=PEM_read_RSAPrivateKey(file,NULL,NULL,NULL))==NULL){
       ERR_print_errors_fp(stdout);
       return NULL;
   }
   rsa_len=RSA_size(p_rsa);
   p_de=(unsigned char *)malloc(rsa_len+1);
   memset(p_de,0,rsa_len+1);
   if(RSA_private_decrypt(rsa_len,(unsigned char *)str,(unsigned char*)p_de,p_rsa,RSA_NO_PADDING)<0){
       return NULL;
   }
   RSA_free(p_rsa);
   fclose(file);
   return p_de;
}


0 0