Openssl --RSA加密算法的使用。

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

其主要数据结构以及函数
RSA数据结构,其中包含公钥信息,如果只有n和e则表明是公钥信息
struct rsa_st
{
/* The first parameter is used to pickup errors where
* this is passed instead of aEVP_PKEY, it is set to 0 */
int pad;
long version;
const RSA_METHOD *meth;//OpenSSL默认的RSA加解密算法
/* functional reference if 'meth' is ENGINE-provided */
ENGINE *engine;
BIGNUM *n;//模数n
BIGNUM *e;//公钥指数e,通常为RSA_3(3)或RSA_F4(65537)
BIGNUM *d;//私钥指数d
BIGNUM *p;//大素数p
BIGNUM *q;//大素数q
BIGNUM *dmp1; //d mod (p-1)
BIGNUM *dmq1; //d mod (q-1)
BIGNUM *iqmp;//(inverse of q) mod p
/* be careful using this if the RSA structure is shared */
CRYPTO_EX_DATA ex_data;
int references;
int flags;


/* Used to cache montgomery values */
BN_MONT_CTX *_method_mod_n;
BN_MONT_CTX *_method_mod_p;
BN_MONT_CTX *_method_mod_q;


/* all BIGNUM values are actually in the following data, if it is not
* NULL */
char *bignum_data;
BN_BLINDING *blinding;
BN_BLINDING *mt_blinding;
};


BIGNUM:openssl定义的数据类型 抽象表示1个大数


RSA* RSA_new(void):生成一个RSA结构,并采用默认的rsa_pkcs1_eay_meth RSA_METHOD算法。成功返回1,失败返回-1


int BN_new():生成一个BIGNUM结构,成功返回1,失败返回-1


int BN_num_bytes(const BIGNUM *a);返回a的位数,大量使用


int BN_hex2bn(BIGNUM **a, const char *str);转化为10进制字符串


void BN_init(BIGNUM *);    初始化所有项均为0,一般为BN_init(&c) 


void BN_free(BIGNUM *a);   释放一个BIGNUM结构,释放完后a=NULL; 


void BN_clear(BIGNUM *a);  将a中所有项均赋值为0,但是内存并没有释放 


void BN_clear_free(BIGNUM *a); 相当与将BN_free和BN_clear综合,要不就赋值0,要不就释放空间。


int BN_set_word(BIGNUM *a, BN_ULONG w);将BIGNUM结构的值置为unsigned long int类型证书的值。成功返回1,失败返回-1


int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);参数分别为:RSA结构,bits为生成密钥的长度,公开指数e,一般为NULL。(密钥产生函数)作用:根据密钥长度和公钥指数生成密钥。


void RSA_FREE(RSA *rsa);释放RSA结构。


int RSA_print_fp(FILE *fp, const RSA *r,int offset);参数分别为:文件指针*fp,RSA结构,打印偏移量offset是为了调整输出格式的,随意一个数都可以(例如2,12,16。。)。
作用:将生成的密钥输出到文件


int RSA_check_key(const RSA *);检查RSA结构中n,e,d,p,q是否满足条件。满足返回1,否则返回-1;


int RSA_size(const RSA *);返回RSA结构中密钥的长度


加解密函数:
int RSA_public_encrypt(intflen, const unsigned char *from, unsigned char *to, RSA *rsa,int padding); 


int RSA_private_encrypt(intflen, const unsigned char *from, unsigned char *to, RSA *rsa,int padding); 


int RSA_public_decrypt(intflen, const unsigned char *from, unsigned char *to, RSA *rsa,int padding); 


int RSA_private_decrypt(intflen, const unsigned char *from, unsigned char *to, RSA *rsa,int padding);


下面是一个例子,这个例子利用已有的密钥来对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;
   }
   /*以上可以这样
      RSA* pRSAPublicKey = RSA_new();
      if(PEM_read_RSA_PUBKEY(file, &pRSAPublicKey, 0, 0) == NULL)
      {
        assert(false);
        return "";
      }
     不用以后 记得释放
     RSA_free(pRSAPublicKey);
   */


   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;
}


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于使用C语言实现AES加密算法RSA加密算法,你可以按照以下步骤进行操作: 1. AES加密算法的实现: - 首先,你需要导入相关的文件,如openssl/aes.h。 - 接下来,你可以选择使用AES-128、AES-192或AES-256等不同的密钥长度。选择一个适合的密钥长度后,你可以生成一个随机的密钥,并设置初始向量(IV)。 - 然后,你可以使用AES加密函数,如AES_set_encrypt_key()和AES_encrypt(),将明文加密为密文。 - 最后,记得释放相关的资源,并清理内存。 2. RSA加密算法的实现: - 首先,你需要导入相关的文件,如openssl/rsa.h。 - 接下来,你可以使用RSA_generate_key函数生成RSA的公钥和私钥。 - 然后,你可以使用RSA公钥对明文进行加密,使用RSA私钥对密文进行解密。 - 除此之外,你还可以使用RSA私钥对明文进行签名,使用RSA公钥对签名进行验证。 下面是一个示例代码,演示了如何使用C语言实现AES和RSA加密算法: ```c #include <stdio.h> #include <openssl/aes.h> #include <openssl/rsa.h> #include <openssl/pem.h> #include <openssl/err.h> // AES加密函数 void aes_encrypt(const unsigned char *plain_text, unsigned char *cipher_text, const unsigned char *key, const unsigned char *iv) { AES_KEY aes_key; AES_set_encrypt_key(key, 128, &aes_key); AES_cbc_encrypt(plain_text, cipher_text, 128, &aes_key, iv, AES_ENCRYPT); } // RSA加密函数 int rsa_encrypt(const unsigned char *plain_text, int plain_text_len, unsigned char *cipher_text, RSA *rsa) { int rsa_len = RSA_size(rsa); int result = RSA_public_encrypt(plain_text_len, plain_text, cipher_text, rsa, RSA_PKCS1_PADDING); return result; } int main() { // AES加密示例 unsigned char aes_key[16]; // 128位密钥 unsigned char iv[16]; // 初始向量 unsigned char plain_text[] = "Hello, AES!"; unsigned char cipher_text[128]; // 生成随机的密钥和初始向量 // ... // 使用AES加密算法加密明文 aes_encrypt(plain_text, cipher_text, aes_key, iv); printf("AES Encrypted Text: %s\n", cipher_text); // RSA加密示例 RSA *rsa = RSA_generate_key(2048, RSA_F4, NULL, NULL); unsigned char rsa_plain_text[] = "Hello, RSA!"; unsigned char rsa_cipher_text[256]; // 使用RSA公钥加密明文 rsa_encrypt(rsa_plain_text, sizeof(rsa_plain_text), rsa_cipher_text, rsa); printf("RSA Encrypted Text: %s\n", rsa_cipher_text); RSA_free(rsa); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值