linux下Openssl RSA加密解密实例

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1、生成秘钥文件
openssl genrsa -out secret.key 1024
openssl genrsa是生成密钥的命令,-out是指定所生成的密钥文件,secret.key这个文件里包含了公钥和密钥两部分,就是说这个文件即可用来加密也可以用来解密,如果想分开也可以用下面的命令将公钥导出。

命令中的1024是指生成的密钥的长度。

2、将公钥导出
openssl rsa -in secret.key -pubout -out secret_pub.key
将公钥从secret.key中导出,-in指定输入文件,-out指定提取生成公钥的文件名。

这样我们就有了一个公钥和一个私钥(包含公钥)。

下面我们就可以用公钥来加密文件了。

3.
下面是一个用C实现的OpenSSL RSA加密的程序,程序实现的是公钥加密,私钥解密的过程,当然也可以实现私钥加密,公钥解密,大家可以根据程序后面的函数解释来进行各种更改。

下面将通过第一部分生成的加密文件中的公钥secret_pub.key来实现对字符串的加密,用密钥文件secret.key来实现字符串的解密,程序已经经过编译运行,可以直接实验、运行。

myRSA.h:
1 #ifndef _MY_RSA_H_
2 #define _MY_RSA_H_
3
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <openssl/rsa.h>
8 #include <openssl/pem.h>
9 #include <openssl/err.h>
10
11 #define BUFFSIZE 1024
12 #define PUBLICKEY “secret_pub.key”
13 #define OPENSSLKEY “secret.key”
14
15 char* my_EncryptFunc(char *str,char *path_key); //加密函数
16 char* my_DecryptFunc(char *str,char *path_key); //解密函数
17
18
19 #endif
myRSA.c:
1 #include “myRSA.h”
2
3
4 int main(void)
5 {
6 char *p_Src = (char *)”This is my test !”;
7 char *p_EnStr,*p_DeStr;
8
9 printf(“Source is : [%s]\n”,p_Src);
10
11 p_EnStr = my_EncryptFunc(p_Src, PUBLICKEY);
12 printf(“Encryption : [%s]\n”,p_EnStr);
13
14 p_DeStr = my_DecryptFunc(p_EnStr, OPENSSLKEY);
15 printf(“Decryption : [%s]\n”,p_DeStr);
16
17 if(p_EnStr!=NULL)
18 {
19 free(p_EnStr);
20 }
21 if(p_DeStr!=NULL)
22 {
23 free(p_DeStr);
24 }
25
26 return 0;
27 }
28
29
30 char *my_EncryptFunc(char *str,char *path_key)
31 {
32 RSA *p_Rsa;
33 char *p_En;
34 FILE *file;
35 int rsa_len;
36
37 if((file=fop en(path_key,”r”))==NULL)
38 {
39 perror(“open key file error”);
40 return NULL;
41 }
42
43
44 #ifndef RSA_NEW
45
46 if((p_Rsa=PEM_read_RSA_PUBKEY(file,NULL,NULL,NULL))==N ULL)
47 {
48 ERR_print_errors_fp(stdout);
49 return NULL;
50 }
51 else
52 {
53 printf(“PEM read success!\n”);
54 }
55
56 #else
57
58 p_Rsa = RSA_new();
59 if(PEM_read_RSA_PUBKEY(file, &p_Rsa, 0, 0) == NULL)
60 {
61 return NULL;
62 }
63 else
64 {
65 printf(“PEM read new success\n”);
66 }
67
68 #endif
69
70 rsa_len=RSA_size(p_Rsa);
71
72 p_En = (char *)malloc(rsa_len+1);
73 memset(p_En,0,rsa_len+1);
74
75 if(RSA_public_encrypt(rsa_len, (unsigned char *)str, (unsigned char*)p_En, p_Rsa, RSA_NO_PADDING) < 0)
76 {
77 return NULL;
78 }
79
80 RSA_free(p_Rsa);
81 fclose(file);
82
83 return p_En;
84
85 }
86
87 char *my_DecryptFunc(char *str,char *path_key)
88 {
89 RSA *p_Rsa;
90 char *p_De;
91 FILE *file;
92 int rsa_len;
93
94 if((file=fopen(path_key,”r”))==NU LL){
95 perror(“open key file error”);
96 return NULL;
97 }
98
99 if((p_Rsa=PEM_read_RSAPrivateKey(file,NULL,NULL,NULL))==N ULL){
100 ERR_print_errors_fp(stdout);
101 return NULL;
102 }
103
104 rsa_len=RSA_size(p_Rsa);
105 p_De=(char *)malloc(rsa_len+1);
106 memset(p_De,0,rsa_len+1);
107
108 if(RSA_private_decrypt(rsa_len,(unsigned char *)str,(unsigned char*)p_De,p_Rsa,RSA_NO_PADDING)<0){
109 return NULL;
110 }
111
112 RSA_free(p_Rsa);
113 fclose(file);
114
115 return p_De;
116 }
4. linux下编译
gcc myRSA.c -o exe -lssl
1)如果出现如openssl/rsa.h : no such file or direction,则需要安装
openssl-devel
sudo yum install openssl-devel
2) 如果出现:
undefined reference to symbol 'RSA_size@@libcrypto.so.10'
/usr/lib64/libcrypto.so.10: error adding symbols: DSO missing f rom command line
使用新命令:gcc myRSA.c -o exe -lssl -lcrypto
5.运行:./exe
6.结果;。

相关文档
最新文档