首页 > 代码库 > openssl C语言编码实现rsa加密

openssl C语言编码实现rsa加密


 1 CC=gcc
 2 CPPFLAGS= -I /home/yyx/02/openssl-1.0.1t/include/
 3 CFLAGS=-Wall -g
 4 LIBPATH = -L /usr/lib
 5 LIBS= -lssl -lcrypto -lhiredis -lm
 6 
 7 
 8 #找到当前目录下所有的.c文件
 9 src = http://www.mamicode.com/$(wildcard ./src/*.c)
10 
11 #将当前目录下所有的.c  转换成.o给obj
12 obj = $(patsubst %.c, %.o, $(src))
13 
14 rsa = test_rsa
15 
16 target = $(rsa)
17 
18 ALL:$(target)
19     
20 #生成所有的.o文件
21 $(obj):%.o:%.c
22     $(CC) -c $< -o $@ $(CPPFLAGS) $(LIBPATH) $(LIBS) $(CFLAGS) 
23     
24 #test_rsa程序
25 $(rsa):./src/test.o  
26     $(CC) $^ -o $@ $(CPPFLAGS)  $(LIBPATH) $(LIBS) $(CFLAGS)    
27  
28  
29 #clean指令
30 
31 clean:
32     -rm -rf $(obj) $(target) ./test/*.o
33 
34 #将clean目标 改成一个虚拟符号
  1 //  RSA 加密 ///
  2 
  3 #include <stdio.h>
  4 #include <stdlib.h>
  5 #include <string.h>
  6 #include <errno.h>
  7 #include <openssl/rsa.h>
  8 #include <openssl/pem.h>
  9 #include <openssl/err.h>
 10 #include <openssl/ssl.h>
 11 
 12 
 13 #define OPENSSLKEY "test.key"
 14 #define PUBLICKEY  "test_pub.key"
 15 #define BUFFSIZE   1024
 16 
 17 char *my_encrypt(char *str, char *path_key);    //加密
 18 char *my_decrypt(char *str, char *path_key);        //解密
 19 
 20 int main(void)
 21 {
 22     char *source = "i like dancing !!!";
 23     
 24     char *ptf_en, *ptf_de;
 25     
 26     printf("source is   :%s\n", source);
 27     
 28     //1.加密
 29     ptf_en = my_encrypt(source, PUBLICKEY);
 30     printf("ptf_en is   :%s\n", ptf_en);
 31     
 32     //2.解密
 33     ptf_de = my_decrypt(ptf_en, OPENSSLKEY);
 34     printf("ptf_de is   :%s\n", ptf_de);
 35     
 36     if(ptf_en)            free(ptf_en);
 37     if(ptf_de)            free(ptf_de);
 38         
 39     return 0;
 40     
 41 }
 42 
 43 //加密
 44 char *my_encrypt(char *str, char *path_key)
 45 {
 46     char *p_en = NULL;
 47     RSA  *p_rsa = NULL;
 48     FILE *file = NULL;
 49     
 50     int  rsa_len = 0;    //flen为源文件长度, rsa_len为秘钥长度
 51     
 52     //1.打开秘钥文件
 53     if((file = fopen(path_key, "r")) == NULL)
 54     {
 55         perror("fopen() error ");
 56         goto End;
 57     }        
 58     
 59     //2.从公钥中获取 加密的秘钥
 60     if((p_rsa = PEM_read_RSA_PUBKEY(file, NULL,NULL,NULL )) == NULL)
 61     {
 62         ERR_print_errors_fp(stdout);
 63         goto End;
 64     }
 65     
 66     //3.获取秘钥的长度
 67     rsa_len = RSA_size(p_rsa);
 68     
 69     //4.为加密后的内容 申请空间(根据秘钥的长度+1)
 70     p_en = (char *)malloc(rsa_len + 1);
 71     if(!p_en)
 72     {
 73         perror("malloc() error ");
 74         goto End;
 75     }    
 76     memset(p_en, 0, rsa_len + 1);
 77     
 78     //5.对内容进行加密
 79     if(RSA_public_encrypt(rsa_len, (unsigned char*)str, (unsigned char*)p_en, p_rsa, RSA_NO_PADDING) < 0)
 80     {
 81         perror("RSA_public_encrypt() error ");
 82         goto End;
 83     }
 84 
 85 End:
 86     
 87     //6.释放秘钥空间, 关闭文件
 88     if(p_rsa)    RSA_free(p_rsa);
 89     if(file)     fclose(file);
 90         
 91     return p_en;
 92 }   
 93 
 94 //解密
 95 char *my_decrypt(char *str, char *path_key)
 96 {
 97     char *p_de = NULL;
 98     RSA  *p_rsa = NULL;
 99     FILE *file = NULL;
100     int   rsa_len = 0;
101     
102     
103     //1.打开秘钥文件
104     file = fopen(path_key, "r");
105     {
106         perror("fopen() error ");
107         goto End;
108     }        
109     
110     //2.从私钥中获取 解密的秘钥
111     if((p_rsa = PEM_read_RSAPrivateKey(file, NULL,NULL,NULL )) == NULL)
112     {
113         ERR_print_errors_fp(stdout);
114         goto End;
115     }
116     
117     //3.获取秘钥的长度,
118     rsa_len = RSA_size(p_rsa);
119     
120     //4.为加密后的内容 申请空间(根据秘钥的长度+1)
121     p_de = (char *)malloc(rsa_len + 1);
122     if(!p_de)
123     {
124         perror("malloc() error ");
125         goto End;
126     }    
127     memset(p_de, 0, rsa_len + 1);
128     
129     //5.对内容进行加密
130     if(RSA_private_decrypt(rsa_len, (unsigned char*)str, (unsigned char*)p_de, p_rsa, RSA_NO_PADDING) < 0)
131     {
132         perror("RSA_public_encrypt() error ");
133         goto End;
134     }
135             
136 End:
137     //6.释放秘钥空间, 关闭文件
138     if(p_rsa)    RSA_free(p_rsa);
139     if(file)     fclose(file);
140         
141     return p_de;
142 }    

 

35 .PHONY: clean ALL

 

openssl C语言编码实现rsa加密