非原創, 引用自:
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 = $(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目標 改成一個虛擬符號
35 .PHONY: clean ALL
1.上述makefile; 用來下面編譯的 加密程序。
2.首先介紹下命令台下openssl工具的簡單使用:
1)生成一個密鑰:
openssl genrsa -out test.key 1024
這里-out指定生成文件的。需要注意的是這個文件包含了公鑰和密鑰兩部分,也就是說這個文件即可用來加密也可以用來解密。后面的1024是生成密鑰的長度。
2)openssl可以將這個文件中的公鑰提取出來:
openssl rsa -in test.key -pubout -out test_pub.key
-in指定輸入文件,-out指定提取生成公鑰的文件名。至此,我們手上就有了一個公鑰,一個私鑰(包含公鑰)。現在可以將用公鑰來加密文件了。
3)在目錄中創建一個hello的文本文件,然后利用此前生成的公鑰加密文件:
openssl rsautl -encrypt -in hello -inkey test_pub.key -pubin -out hello.en
-in指定要加密的文件,-inkey指定密鑰,-pubin表明是用純公鑰文件加密,-out為加密后的文件。
4)解密文件:
openssl rsautl -decrypt -in hello.en -inkey test.key -out hello.de
-in指定被加密的文件,-inkey指定私鑰文件,-out為解密后的文件。
至此,一次加密解密的過程告終。
3. 采用 API 進行加密
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 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, "rb")) == NULL) 54 { 55 perror("fopen() error 111111111 "); 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 2222222222"); 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 2222222222"); 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, "rb"); 105 if(!file) 106 { 107 perror("fopen() error 22222222222"); 108 goto End; 109 } 110 111 //2.從私鑰中獲取 解密的秘鑰 112 if((p_rsa = PEM_read_RSAPrivateKey(file, NULL,NULL,NULL )) == NULL) 113 { 114 ERR_print_errors_fp(stdout); 115 goto End; 116 } 117 118 //3.獲取秘鑰的長度, 119 rsa_len = RSA_size(p_rsa); 120 121 //4.為加密后的內容 申請空間(根據秘鑰的長度+1) 122 p_de = (char *)malloc(rsa_len + 1); 123 if(!p_de) 124 { 125 perror("malloc() error "); 126 goto End; 127 } 128 memset(p_de, 0, rsa_len + 1); 129 130 //5.對內容進行加密 131 if(RSA_private_decrypt(rsa_len, (unsigned char*)str, (unsigned char*)p_de, p_rsa, RSA_NO_PADDING) < 0) 132 { 133 perror("RSA_public_encrypt() error "); 134 goto End; 135 } 136 137 End: 138 //6.釋放秘鑰空間, 關閉文件 139 if(p_rsa) RSA_free(p_rsa); 140 if(file) fclose(file); 141 142 return p_de; 143 }