openssl 进行RSA加解密

来源:互联网 发布:阿里云收费 编辑:程序博客网 时间:2024/05/17 04:23

十分感谢http://www.cnblogs.com/aLittleBitCool/archive/2011/09/22/2185418.html对我的帮助,看了这篇文章后,对我的问题解决十分有帮助

一、利用openssl进行RSA加密解密

openssl是一个功能强大的工具包,它集成了众多密码算法及实用工具。我们即可以利用它提供的命令台工具生成密钥、证书来加密解密文件,也可以在利用其提供的API接口在代码中对传输信息进行加密。

RSA是一个非对称加密算法。简单说来,非对称加密算法就是说加密解密一个文件需要有两个密钥,一个用来加密,为公钥,一个用来解密,为私钥。证书可以用来授权公钥的使用。

今天小研究了下openssl的rsa加密,其中主要涉及利用公钥和密钥加解密文件,没有涉及对证书的操作。想要集体了解的可以去:

http://www.openssl.org/

http://blog.csdn.net/jiangsq12345/article/details/6066275

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

首先介绍下命令台下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字符串进行加密与解密:#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<openssl/rsa.h>

#include<openssl/pem.h>

#include<openssl/err.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;

}


二:使用openssl 工具生成私钥后,可以从私钥中提取公钥,上面提到一种生成公钥方式:

openssl rsautl -encrypt -in hello -inkey test_pub.key -pubin -out hello.en 
生成的公钥是以-----BEGIN PUBLIC KEY-----开头的,
还有另外一种生成公钥的方式:
openssl rsa -in key.pen -RSAPublicKey_out -out pubkey.pem
key.pen:私钥
pubkey.pem:公钥
生成的公钥是以-----BEGIN RSA PUBLIC KEY-----开头的
需要使用以下函数来进行加解密
/*****************************************************************
函数     : openssl_rsa_dec
概述     : openssl rsa 算法使用私钥解密函数,out字符串长度建议为1024
入参说明 :encrypted: 解密字符串;inLen:解密字符串长度
    dencrypted:解密后输出结果;outLen:解密字符串长度
*****************************************************************/
int openssl_rsa_dec(char *encrypted, int inLen, char *dencrypted, int* outLen)
{
// -------------------------------------------------------  
// 利用私钥解密密文的过程  
// -------------------------------------------------------  
// 打开私钥文件  
    BIO *pBio = BIO_new_file(PRIV_KEY,"r");
    int len = 0;
    RSA *pRsa = PEM_read_bio_RSAPrivateKey(pBio,NULL,NULL,NULL);
    BIO_free_all(pBio);
    len = RSA_private_decrypt(inLen, encrypted, dencrypted, pRsa, RSA_PKCS1_PADDING);
    RSA_free(pRsa);
    *outLen = len;
    if(len >= 0)
    {
        return 0;
    }
     return -1;
}
/*****************************************************************
函数     : openssl_rsa_enc
概述     : openssl rsa 算法使用公钥进行加密
入参说明 :plain 加密字符串; inLen:加密字符串长度
    encrypted:加密后的字符串,建议长度1024;  outLen:加密字符串长度
*****************************************************************/
int openssl_rsa_enc(char *plain, int inLen, char *encrypted, int* outLen)
{
// -------------------------------------------------------  
// 利用公钥加密明文的过程 
// -------------------------------------------------------  
// 打开公钥文件  
int len=0;
BIO *pBio = BIO_new_file(PUBLISH_KEY,"r");
// 从文件中读取公钥
RSA *pRsa = PEM_read_bio_RSAPublicKey(pBio,NULL,NULL,NULL); 
if(NULL == pRsa)
{  
printf("unable to read public key!\n");
return -1;   
}  
  
BIO_free_all(pBio); 
// 用公钥加密  
len=RSA_public_encrypt((strlen(pRsa)-11)>inLen?inLen:strlen(pRsa)-11,(const unsigned char*)plain, (unsigned char*)encrypted, pRsa, RSA_PKCS1_PADDING);  
if(len==-1 )
{
printf("failed to encrypt\n");
return -1;
}  
*outLen = len;
return 0;
}
#if 0
int main()  
{  
// 原始明文  
char *plain="sys";  
// 用来存放密文
char encrypted[1024] = {0};  
// 用来存放解密后的明文  
  
char decrypted[1024] = {0};  
int nDecLen = 0;
int len = 0;
openssl_rsa_enc(plain,s(plain), encrypted, &len);
  openssl_rsa_dec(encrypted, len, decrypted, &nDecLen);
// 输出解密后的明文  
decrypted[len]=0;  
printf("%s\n",decrypted);
} 
#endif


0 0
原创粉丝点击