技术文档

基于openssl的RSA加解密的解决方案

一、前言

基于openssl的RSA加解密的解决方案

openssl是一套第三方的关于数据完整性的安全协议,有一些常用的密码算法,数字证书,数字签名等等方面的一些应用。由于项目需求需要用到openssl的包来实现RSA算法的加解密。

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

二、openssl的安装及错误解决方案

第一步,首先需要在openssl官网下载openssl包http://www.openssl.org/source/;第二步,按照下载的openssl的readme文档,需要阅读install.win32文档,从读文档可知,还需要另外几个软件。

perl开发包,http://www./activeperl/downloads/ ,然后安装;

第三步,还需要下载masm,在微软的官方网站下载,需要visual c++ 2005,其实不用,网上有人说直接将下载的文件解压

 

三、基于openssl的RSA加解密实现

http://www.openssl.org/

 

———————————————————————————————————————

 

首先介绍下命令台下openssl工具的简单使用:

 

生成一个密钥:

 

openssl genrsa -out test.key 1024

这里-out指定生成文件的。需要注意的是这个文件包含了公钥和密钥两部分,也就是说这个文件即可用来加密也可以用来解密。后面的1024是生成密钥的长度。

 

openssl可以将这个文件中的公钥提取出来:

 

openssl rsa -in test.key -pubout -out test_pub.key

-in指定输入文件,-out指定提取生成公钥的文件名。至此,我们手上就有了一个公钥,一个私钥(包含公钥)。现在可以将用公钥来加密文件了。

 

我在目录中创建一个hello的文本文件,然后利用此前生成的公钥加密文件:

 

openssl rsautl -encrypt -in hello -inkey test_pub.key -pubin -out hello.en

-in指定要加密的文件,-inkey指定密钥,-pubin表明是用纯公钥文件加密,-out为加密后的文件。

 

解密文件:

 

openssl rsautl -decrypt -in hello.en -inkey test.key -out hello.de

-in指定被加密的文件,-inkey指定私钥文件,-out为解密后的文件。

 

至此,一次加密解密的过程告终。在实际使用中还可能包括证书,这个以后有机会再说~

 

——————————————————————————————————————-

下来介绍下在程序如何利用之前生成的test.key和test_pub.key来进行信息的加密与解密(当然也可以直接利用openssl的API来生成密钥文件)。

 

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

 

复制代码

1 #include<stdio.h>

2 #include<stdlib.h>

3 #include<string.h>

4 #include<openssl/rsa.h>

5 #include<openssl/pem.h>

6 #include<openssl/err.h>

7 #define OPENSSLKEY “test.key”

8 #define PUBLICKEY “test_pub.key”

9 #define BUFFSIZE 1024

10 char* my_encrypt(char *str,char *path_key);//加密

11 char* my_decrypt(char *str,char *path_key);//解密

12 int main(void){

13 char *source=”i like dancing !”;

14 char *ptr_en,*ptr_de;

15 printf(“source is :%s\n”,source);

16 ptr_en=my_encrypt(source,PUBLICKEY);

17 printf(“after encrypt:%s\n”,ptr_en);

18 ptr_de=my_decrypt(ptr_en,OPENSSLKEY);

19 printf(“after decrypt:%s\n”,ptr_de);

20 if(ptr_en!=NULL){

21 free(ptr_en);

22 }

23 if(ptr_de!=NULL){

24 free(ptr_de);

25 }

26 return 0;

27 }

28 char *my_encrypt(char *str,char *path_key){

29 char *p_en;

30 RSA *p_rsa;

31 FILE *file;

32 int flen,rsa_len;

33 if((file=fopen(path_key,”r”))==NULL){

34 perror(“open key file error”);

35 return NULL;

36 }

37 if((p_rsa=PEM_read_RSA_PUBKEY(file,NULL,NULL,NULL))==NULL){

38 //if((p_rsa=PEM_read_RSAPublicKey(file,NULL,NULL,NULL))==NULL){ 换成这句死活通不过,无论是否将公钥分离源文件

39 ERR_print_errors_fp(stdout);

40 return NULL;

41 }

42 flen=strlen(str);

43 rsa_len=RSA_size(p_rsa);

44 p_en=(unsigned char *)malloc(rsa_len+1);

45 memset(p_en,0,rsa_len+1);

46 if(RSA_public_encrypt(rsa_len,(unsigned char *)str,(unsigned char*)p_en,p_rsa,RSA_NO_PADDING)<0){

47 return NULL;

48 }

49 RSA_free(p_rsa);

50 fclose(file);

51 return p_en;

52 }

53 char *my_decrypt(char *str,char *path_key){

54 char *p_de;

55 RSA *p_rsa;

56 FILE *file;

57 int rsa_len;

58 if((file=fopen(path_key,”r”))==NULL){

59 perror(“open key file error”);

60 return NULL;

61 }

62 if((p_rsa=PEM_read_RSAPrivateKey(file,NULL,NULL,NULL))==NULL){

63 ERR_print_errors_fp(stdout);

64 return NULL;

65 }

66 rsa_len=RSA_size(p_rsa);

67 p_de=(unsigned char *)malloc(rsa_len+1);

68 memset(p_de,0,rsa_len+1);

69 if(RSA_private_decrypt(rsa_len,(unsigned char *)str,(unsigned char*)p_de,p_rsa,RSA_NO_PADDING)<0){

70 return NULL;

71 }

72 RSA_free(p_rsa);

73 fclose(file);

74 return p_de;

75 }

复制代码

一个比较奇怪的问题:

37、38行中为从文件中获取密钥,发现如果使用openssl提供的PEM_read_RSAPublicKey方法会一直失败。

估计是文件格式的问题~

 

©2020-2024   万云SSL证书  (www.sslssl.com.cn)  万云科技   豫ICP备2020034358号-10