用openssl库RSA加密解密
#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h> //加密
int my_encrypt(const char *input, int input_len, char *output, int *output_len, const char *pri_key_fn)
{
RSA *p_rsa = NULL;
FILE *file = NULL;
int ret = ; if((file = fopen(pri_key_fn, "rb")) == NULL)
{
ret = -;
goto End;
} if((p_rsa = PEM_read_RSAPrivateKey(file, NULL,NULL,NULL )) == NULL)
{
ret = -;
goto End;
} if((*output_len = RSA_private_encrypt(input_len, (unsigned char*)input, (unsigned char*)output, p_rsa, RSA_PKCS1_PADDING)) < )
{
ret = -;
goto End;
} End:
if(p_rsa != NULL)
RSA_free(p_rsa);
if(file != NULL)
fclose(file); return ret;
} //解密
int my_decrypt(const char *input, int input_len, char *output, int *output_len, const char *pri_key_fn)
{
RSA *p_rsa = NULL;
FILE *file = NULL;
int ret = ; file = fopen(pri_key_fn, "rb");
if(!file)
{
ret = -;
goto End;
} if((p_rsa = PEM_read_RSA_PUBKEY(file, NULL,NULL,NULL )) == NULL)
{
ret = -;
goto End;
} if((*output_len=RSA_public_decrypt(input_len, (unsigned char*)input, (unsigned char*)output, p_rsa, RSA_PKCS1_PADDING)) < )
{
ret = -;
goto End;
}
End:
if(p_rsa != NULL)
RSA_free(p_rsa);
if(file != NULL)
fclose(file); return ret;
} int main(int argc, char**argv)
{
char src[];
char dst[];
int src_len;
int dst_len;
int ret;
FILE *f; src_len = fread(src, , , stdin); if(argv[][] == 'e') {
ret = my_encrypt(src, src_len, dst, &dst_len, argv[]);
}else {
ret = my_decrypt(src, src_len, dst, &dst_len, argv[]);
} if(ret) {
fprintf(stderr, "Error\n");
}
fwrite(dst,,dst_len,stdout);
return ret;
}
以上是一个示例,测试了私钥加密(签名)/公钥解密(验证),main函数是一个测试
测试一下,先生成2048位公钥、私钥对
colin@colin-VirtualBox:/tmp$ openssl genrsa -out pri2048.pem
Generating RSA private key, bit long modulus
................................+++
.............+++
e is (0x10001)
colin@colin-VirtualBox:/tmp$ openssl rsa -in pri2048.pem -pubout -out pub2048.pem
writing RSA key
编译、文件测试
colin@colin-VirtualBox:/tmp$ gcc t.c -lssl -lcrypto -lm
colin@colin-VirtualBox:/tmp$ ./a.out en pri2048.pem <data >data.en
colin@colin-VirtualBox:/tmp$ ./a.out enc pri2048.pem <data >data.en
colin@colin-VirtualBox:/tmp$ ./a.out dec pub2048.pem <data.en >data2
colin@colin-VirtualBox:/tmp$ openssl rsautl -verify -in data.en -inkey pub2048.pem -pubin -out data3
对比一下
colin@colin-VirtualBox:/tmp$ cmp data data2
colin@colin-VirtualBox:/tmp$ cmp data data3
colin@colin-VirtualBox:/tmp$ md5sum data data2 data3
7a71146998ad521bab336a49f65c90c4 data
7a71146998ad521bab336a49f65c90c4 data2
7a71146998ad521bab336a49f65c90c4 data3
公钥加密、私钥解密就不写了,对着看就会很明白了。
int RSA_public_encrypt(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding);
int RSA_private_encrypt(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding);
int RSA_public_decrypt(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding);
int RSA_private_decrypt(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding);
用openssl库RSA加密解密的更多相关文章
- openssl evp RSA 加密解密
openssl evp RSA 加密解密 可以直接使用RSA.h 提供的接口 如下测试使用EVP提供的RSA接口 1. EVP提供的RSA 加密解密 主要接口: int EVP_PKEY_encryp ...
- OpenSSL 中 RSA 加密解密实现源代码分析
1.RSA 公钥和私钥的组成.以及加密和解密的公式: 2.模指数运算: 先做指数运算,再做模运算.如 5^3 mod 7 = 125 mod 7 = 6 3.RSA加密算法流程: 选择一对不同的.而且 ...
- 利用openssl进行RSA加密解密
openssl是一个功能强大的工具包,它集成了众多密码算法及实用工具.我们即可以利用它提供的命令台工具生成密钥.证书来加密解密文件,也可以在利用其提供的API接口在代码中对传输信息进行加密. RSA是 ...
- jsencrypt代码分析——openssl的rsa加密解密在js的实现
在js上做rsa,感觉jsencrypt这个是封装的比较好的,但用起来还是遇到了些坑,所以踩进代码里填填坑- 项目在这里 https://github.com/travist/jsencrypt [r ...
- Cryptopp iOS 使用 RSA加密解密和签名验证签名
Cryptopp 是一个c++写的功能完善的密码学工具,类似于openssl 官网:https://www.cryptopp.com 以下主要演示Cryptopp 在iOS上的RSA加密解密签名与验证 ...
- python下RSA加密解密以及跨平台问题
Reference: http://www.cnblogs.com/luchanghong/archive/2012/07/18/2596886.html 项目合作需要,和其他网站通信,消息内容采用 ...
- RSA 加密解密使用实例
http://www.dtmao.cc/news_show_692109.shtml 本文不讨论RSA加密解密本身,只记录使用方法及遇到的坑,RSA原理及注意事项可在网上查找. 背景:公司的一个需求, ...
- 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输
Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...
- iOS使用Security.framework进行RSA 加密解密签名和验证签名
iOS 上 Security.framework为我们提供了安全方面相关的api: Security框架提供的RSA在iOS上使用的一些小结 支持的RSA keySize 大小有:512,768,10 ...
随机推荐
- sql操作一般函数
sql操作一般函数 函数一般语法:SELECT function(列) FROM 表 函数的基本类型是: Aggregate 合计函数:函数的操作面向一系列的值,并返回一个单一的值. Scalar 函 ...
- Let's Encrypt与DNS轮循
本文由网络安全研究员.securityheaders.io和report-uri.io创始人Scott Helme发布在其个人博客中.描述了如何使用Let's Encrypt的同时兼容DNS轮循. 早 ...
- Ubuntu16.04配置Mac主题
作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7072878.html 觉得有帮助?欢迎来打赏 Ubuntu配置Mac主题 下 ...
- yii2 获取从前台传过来的post数据
第一次使用yii写接口的时候,直接用了$_POST获取post数据,发现会报400错误,根本无法获取到post数据,用$_GET却能获取get数据. 纠结了很久,然后查资料,发现原来yii中默认的开启 ...
- Eclipse、maven项目常见问题
阿里云maven仓库地址: <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> &l ...
- acdsee-pro-8 英文版
目前中文版acdsee pro 8不支持破解,只能选择英文版,然后汉化. 但是英文版并不好找,我在这里分享一下. 下载地址:http://pan.baidu.com/s/1boNZxQf 同时附上注册 ...
- 在drawRect:方法中绘制图片,文字以及Core Graphics 框架的了解
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...
- NYOJ--21--bfs--三个水杯
/* 输入 第一行一个整数N(0<N<50)表示N组测试数据 接下来每组测试数据有两行,第一行给出三个整数V1 V2 V3 (V1>V2>V3 V1<100 V3> ...
- 一步一步学MySQL-日志文件
错误日志 错误日志不用多说,记录了mysql运行过程中的错误信息,当出现问题时,我们可以通过错误日志查找线索. 慢查询日志 可以通过参数long_query_time来设置时间,当sql语句执行超过指 ...
- 正则表达式(即:匹配模式Pattern)
通过学习<正则表达式-Regular-Expression-Tutorial.pdf>这份文档,对正则表达式有了很好的了解,原因只有1个--这份文档写得太好了. 它的下载地址是:http: ...