售前咨询
技术支持
渠道合作

openssl进行RSA加密解密

openssl是什么

OpenSSL 是一个安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

OpenSSL被曝出现严重安全漏洞后,发现多数通过SSL协议加密的网站使用名为OpenSSL的开源软件包。OpenSSL漏洞不仅影响以https开头的网站,黑客还可利用此漏洞直接对个人电脑发起“心脏出血”(Heartbleed)攻击。据分析,Windows上有大量软件使用了存在漏洞的OpenSSL代码库,可能被黑客攻击抓取用户电脑上的内存数据。

利用openssl进行RSA加密解密

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

下面介绍openssl的rsa加密,其中主要涉及利用公钥和密钥加解密文件,没有涉及对证书的操作。

首先介绍下命令台下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 }


GDCA一直以“构建网络信任体系,服务现代数字生活”的宗旨,致力于提供全球化的数字证书认证服务。其自主品牌——信鉴易®TrustAUTH® SSL证书系列,为涉足互联网的企业打造更安全的生态环境,建立更具公信力的企业网站形象。

上一篇:

下一篇:

相关文章