天天看點

使用OpenSSL進行RSA加密解密_F_hawk189_新浪部落格

生成一個密鑰:

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來生成密鑰檔案)。

//下面本來都是尖括号,但是新浪部落格尖括号沒法顯示,又懶得寫代碼,就換成引号了
#include “stdio.h”
 #include “stdlib.h”
 #include “openssl/rsa.h”
 #include “openssl/pem.h”
 #include “openssl/err.h”
 #include “string.h”
 #define OPENSSLKEY "test.key"
 #define PUBLICKEY "test_pub.key"
 #define BUFFSIZE 1024
 char* my_encrypt(char *str,char *path_key);//加密
 char* my_decrypt(char *str,char *path_key);//解密
 int main(void){
     char *source="i like dancing !";
     char *ptr_en,*ptr_de;
     printf("source is    :%s\n",source);
     ptr_en=my_encrypt(source,PUBLICKEY);
     printf("after encrypt:%s\n",ptr_en);
     ptr_de=my_decrypt(ptr_en,OPENSSLKEY);
     printf("after decrypt:%s\n",ptr_de);
     if(ptr_en!=NULL){
         free(ptr_en);
     }  
     if(ptr_de!=NULL){
         free(ptr_de);
     }  
     return 0;
 }
 char *my_encrypt(char *str,char *path_key){
     char *p_en;
     RSA *p_rsa;
     FILE *file;
     int flen,rsa_len;
     if((file=fopen(path_key,"r"))==NULL){
         perror("open key file error");
         return NULL;   
     }  
     if((p_rsa=PEM_read_RSA_PUBKEY(file,NULL,NULL,NULL))==NULL){
     //if((p_rsa=PEM_read_RSAPublicKey(file,NULL,NULL,NULL))==NULL){   換成這句死活通不過,無論是否将公鑰分離源檔案
         ERR_print_errors_fp(stdout);
         return NULL;
     }  
     flen=strlen(str);
     rsa_len=RSA_size(p_rsa);
     p_en=(unsigned char *)malloc(rsa_len+1);
     memset(p_en,0,rsa_len+1);
     if(RSA_public_encrypt(rsa_len,(unsigned char *)str,(unsigned char*)p_en,p_rsa,RSA_NO_PADDING)<0){
         return NULL;
     }
     RSA_free(p_rsa);
     fclose(file);
     return p_en;
 }
 char *my_decrypt(char *str,char *path_key){
     char *p_de;
     RSA *p_rsa;
     FILE *file;
     int rsa_len;
     if((file=fopen(path_key,"r"))==NULL){
         perror("open key file error");
         return NULL;
     }
     if((p_rsa=PEM_read_RSAPrivateKey(file,NULL,NULL,NULL))==NULL){
         ERR_print_errors_fp(stdout);
         return NULL;
     }
     rsa_len=RSA_size(p_rsa);
     p_de=(unsigned char *)malloc(rsa_len+1);
     memset(p_de,0,rsa_len+1);
     if(RSA_private_decrypt(rsa_len,(unsigned char *)str,(unsigned char*)p_de,p_rsa,RSA_NO_PADDING)<0){
         return NULL;
     }
     RSA_free(p_rsa);
     fclose(file);
     return p_de;
 }      
gcc test.c -o test -lcrypto -lssl