https://blog.csdn.net/RUN32875094/article/details/79306256
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<openssl/rsa.h>
#include<openssl/pem.h>
#include<openssl/err.h>
#define PRIKEY "prikey.pem"
#define PUBKEY "pubkey.pem"
#define BUFFSIZE 4096
/************************************************************************
* RSA加密解密函数
*
* file: test_rsa_encdec.c
* gcc -Wall -O2 -o test_rsa_encdec test_rsa_encdec.c -lcrypto -lssl
*
* author: tonglulin@gmail.com by www.qmailer.net
************************************************************************/
char *my_encrypt(char *str, char *pubkey_path)
{
RSA *rsa = NULL;
FILE *fp = NULL;
char *en = NULL;
int len = 0;
int rsa_len = 0;
if ((fp = fopen(pubkey_path, "r")) == NULL) {
return NULL;
}
/* 读取公钥PEM,PUBKEY格式PEM使用PEM_read_RSA_PUBKEY函数 */
if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) == NULL) {
return NULL;
}
RSA_print_fp(stdout, rsa, 0);
len = strlen(str);
rsa_len = RSA_size(rsa);
en = (char *)malloc(rsa_len + 1);
memset(en, 0, rsa_len + 1);
if (RSA_public_encrypt(rsa_len, (unsigned char *)str, (unsigned char*)en, rsa, RSA_NO_PADDING) < 0) {
return NULL;
}
RSA_free(rsa);
fclose(fp);
return en;
}
char *my_decrypt(char *str, char *prikey_path)
{
RSA *rsa = NULL;
FILE *fp = NULL;
char *de = NULL;
int rsa_len = 0;
if ((fp = fopen(prikey_path, "r")) == NULL) {
return NULL;
}
if ((rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL)) == NULL) {
return NULL;
}
RSA_print_fp(stdout, rsa, 0);
rsa_len = RSA_size(rsa);
de = (char *)malloc(rsa_len + 1);
memset(de, 0, rsa_len + 1);
if (RSA_private_decrypt(rsa_len, (unsigned char *)str, (unsigned char*)de, rsa, RSA_NO_PADDING) < 0) {
return NULL;
}
RSA_free(rsa);
fclose(fp);
return de;
}
int main(int argc, char *argv[])
{
char *src = "hello, world!";
char *en = NULL;
char *de = NULL;
printf("src is: %s\n", src);
en = my_encrypt(src, PUBKEY);
printf("enc is: %s\n", en);
de= my_decrypt(en, PRIKEY);
printf("dec is: %s\n", de);
if (en != NULL) {
free(en);
}
if (de != NULL) {
free(de);
}
return 0;
}
离线
感谢分享例子
离线
libssl在单片机上跑估计够呛,但是上linux系统好使了,只要私钥不泄密,别人就解不了。
离线
vnc支持libssl吗?vnc应用用libssl这种加密合适吗?
离线
搭车问下银行的u盾原理是不是RSA私钥箱?公钥存银行服务器,用来保护通讯?
最近编辑记录 有梦的地方 (2020-04-07 09:21:19)
离线