歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 利用OpenSSL進行RSA加密解密

利用OpenSSL進行RSA加密解密

日期:2017/2/28 14:23:36   编辑:Linux教程

OpenSSL是一個功能強大的工具包,它集成了眾多密碼算法及實用工具。我們即可以利用它提供的命令台工具生成密鑰、證書來加密解密文件,也可以在利用其提供的API接口在代碼中對傳輸信息進行加密。

RSA是一個非對稱加密算法。簡單說來,非對稱加密算法就是說加密解密一個文件需要有兩個密鑰,一個用來加密,為公鑰,一個用來解密,為私鑰。證書可以用來授權公鑰的使用。

今天小研究了下OpenSSL的RSA加密,其中主要涉及利用公鑰和密鑰加解密文件,沒有涉及對證書的操作。想要集體了解的可以去:

http://www.OpenSSL.org/

http://www.linuxidc.com/Linux/2015-01/112072.htm

http://www.linuxidc.com/Linux/2015-01/112071.htm

-----------------------------------------------

首先介紹下命令台下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方法會一直失敗。

估計是文件格式的問題~

通過OpenSSL提供FTP+SSL/TLS認證功能,並實現安全數據傳輸 http://www.linuxidc.com/Linux/2013-05/84986.htm

利用OpenSSL簽署多域名證書 http://www.linuxidc.com/Linux/2014-10/108222.htm

OpenSSL 的詳細介紹:請點這裡
OpenSSL 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved