Openssl RSA加密演算法的使用。

2021-07-07 11:00:12 字數 3147 閱讀 9480

rsa是乙個非對稱加密演算法。簡單說來,非對稱加密演算法就是說加密解密乙個檔案需要有兩個金鑰,乙個用來加密,為公鑰,乙個用來解密,為私鑰。證書可以用來授權公鑰的使用。

其主要資料結構以及函式

rsa資料結構,其中包含公鑰資訊,如果只有n和e則表明是公鑰資訊

struct rsa_st

;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

#include

#include

#include

#include

#include

#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)   

if(ptr_de!=null)   

return 0;

}char *my_encrypt(char *str,char *path_key)   

if((p_rsa=pem_read_rsa_pubkey(file,null,null,null))==null)

/*以上可以這樣

rsa* prsapublickey = rsa_new();

if(pem_read_rsa_pubkey(file, &prsapublickey, 0, 0) == null)

不用以後 記得釋放

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)

rsa_free(p_rsa);

fclose(file);

return p_en;

}char *my_decrypt(char *str,char *path_key)

if((p_rsa=pem_read_rsaprivatekey(file,null,null,null))==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)

rsa_free(p_rsa);

fclose(file);

return p_de;

}

對稱加密演算法 DES加密演算法

一 對稱加密演算法 對稱加密也稱為常規加密 私鑰或單鑰加密。乙個對稱加密由5部分組成 明文 plaintext 這是原始資訊或資料,作為演算法的輸入。加密演算法 encryption algorithm 加密演算法對明文進行各種替換和轉換。金鑰 secret key 金鑰也是演算法的輸入。演算法進行...

gentry同態加密演算法 同態加密演算法

本文對同態加密演算法進行學習。參考文章同態加密演算法。定義同態加密演算法保證對聯合密文的解密結果等價於聯合明文。若存在同態加密演算法f,針對明文a和b,加密後分別得到a f a b f b 將其和a b 解密後得到a b,則同態加密演算法f被成為加法同態加密演算法。加法同態演算法的加密和解密分別用e...

對稱加密演算法 非對稱加密演算法

對稱加密演算法 對稱加密演算法是應用較早的加密演算法,技術成熟。在對稱加密演算法中,資料發信方將明文 原始資料 和加密金鑰一起經過特殊加密演算法處理後,使其變成複雜的加密密文傳送出去。收信方收到密文後,若想解讀原文,則需要使用加密用過的金鑰及相同演算法的逆演算法對密文進行解密,才能使其恢復成可讀明文...