polarssl rsa & aes 加密与解密<转>
上周折腾加密与解密,用了openssl, crypto++, polarssl, cyassl, 说起真的让人很沮丧,只有openssl & polarssl两个库的RSA & AES 加密和解密,我用起来了,crypto++各种模板,各种多继承,看的头大,而且对各种常用的加密算法也不了解,所以这个库我在折腾了一天之后就放弃了;cyassl这个库现在没什么印象了;openssl没什么好说的,用起来很方便,尤其是使用win32openssl,都不用自己编译,下载下来安装好了就能用,着实方便;但是我是要在移动终端使用RSA & AES,研究了半天怎么只使用openssl的源代码,发现还真是麻烦;总之呢,现在我决定使用polarssl,接口简单易用,而且使用源代码进行编译,都是C文件,肯定是跨平台的了,很小,很精悍,下面帖出使用polarssl实现的RSA & AES加密和解密的过程,便于日后直接使用
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string> #include "polarssl/entropy.h"
#include "polarssl/ctr_drbg.h"
#include "polarssl/rsa.h"
#include "polarssl/aes.h" const unsigned int RSA_KEY_SIZE = ; // RSA 公钥的位数
const unsigned int AES_KEY_SIZE = ;
const unsigned int EXPONENT = ;
const unsigned int BUFFER_SIZE = ; class rsa
{
public:
rsa()
{
memset(rsa_n, , BUFFER_SIZE);
memset(rsa_e, , BUFFER_SIZE);
memset(rsa_d, , BUFFER_SIZE);
memset(rsa_p, , BUFFER_SIZE);
memset(rsa_q, , BUFFER_SIZE);
memset(rsa_dp, , BUFFER_SIZE);
memset(rsa_dq, , BUFFER_SIZE);
memset(rsa_qp, , BUFFER_SIZE);
} unsigned char rsa_n[BUFFER_SIZE];
unsigned char rsa_e[BUFFER_SIZE];
unsigned char rsa_d[BUFFER_SIZE];
unsigned char rsa_p[BUFFER_SIZE];
unsigned char rsa_q[BUFFER_SIZE];
unsigned char rsa_dp[BUFFER_SIZE];
unsigned char rsa_dq[BUFFER_SIZE];
unsigned char rsa_qp[BUFFER_SIZE]; unsigned int n_len = BUFFER_SIZE;
unsigned int e_len = BUFFER_SIZE;
unsigned int d_len = BUFFER_SIZE;
unsigned int p_len = BUFFER_SIZE;
unsigned int q_len = BUFFER_SIZE;
unsigned int dp_len = BUFFER_SIZE;
unsigned int dq_len = BUFFER_SIZE;
unsigned int qp_len = BUFFER_SIZE;
}; void generate_rsa(rsa& r)
{
// 生成RSA密钥对
rsa_context rsa;
entropy_context entropy;
ctr_drbg_context ctr_drbg; entropy_init(&entropy); assert(ctr_drbg_init(&ctr_drbg, entropy_func, &entropy, nullptr, ) == ); rsa_init(&rsa, RSA_PKCS_V15, ); assert(rsa_gen_key(&rsa, ctr_drbg_random, &ctr_drbg, RSA_KEY_SIZE, EXPONENT) == ); assert(mpi_write_binary(&rsa.N, r.rsa_n, BUFFER_SIZE) == );
assert(mpi_write_binary(&rsa.E, r.rsa_e, BUFFER_SIZE) == );
assert(mpi_write_binary(&rsa.D, r.rsa_d, BUFFER_SIZE) == );
assert(mpi_write_binary(&rsa.P, r.rsa_p, BUFFER_SIZE) == );
assert(mpi_write_binary(&rsa.Q, r.rsa_q, BUFFER_SIZE) == );
assert(mpi_write_binary(&rsa.DP, r.rsa_dp, BUFFER_SIZE) == );
assert(mpi_write_binary(&rsa.DQ, r.rsa_dq, BUFFER_SIZE) == );
assert(mpi_write_binary(&rsa.QP, r.rsa_qp, BUFFER_SIZE) == ); //puts(r.rsa_n);
//puts(r.rsa_e);
} // 加密
void encrypt(
const rsa &r,
const unsigned char* plaintext,
unsigned int plaintext_size,
unsigned char *ciphertext,
unsigned int &ciphertext_size)
{
rsa_context rsa;
entropy_context entropy;
ctr_drbg_context ctr_drbg; entropy_init(&entropy);
assert(ctr_drbg_init(&ctr_drbg, entropy_func, &entropy, nullptr, ) == ); rsa_init(&rsa, RSA_PKCS_V15, ); assert(mpi_read_binary(&rsa.N, r.rsa_n, BUFFER_SIZE) == );
assert(mpi_read_binary(&rsa.E, r.rsa_e, BUFFER_SIZE) == ); rsa.len = (mpi_msb(&rsa.N) + ) >> ; assert(rsa_pkcs1_encrypt(&rsa, ctr_drbg_random, &ctr_drbg, RSA_PUBLIC, plaintext_size, plaintext, ciphertext) == );
} // 解密
void decrypt(
const rsa &r,
const unsigned char* ciphertext,
unsigned int ciphertext_size,
unsigned char *plaintext,
unsigned int &plaintext_size)
{
rsa_context rsa;
entropy_context entropy;
ctr_drbg_context ctr_drbg; entropy_init(&entropy);
assert(ctr_drbg_init(&ctr_drbg, entropy_func, &entropy, nullptr, ) == ); rsa_init(&rsa, RSA_PKCS_V15, ); assert(mpi_read_binary(&rsa.N, r.rsa_n, BUFFER_SIZE) == );
assert(mpi_read_binary(&rsa.E, r.rsa_e, BUFFER_SIZE) == );
assert(mpi_read_binary(&rsa.D, r.rsa_d, BUFFER_SIZE) == );
assert(mpi_read_binary(&rsa.P, r.rsa_p, BUFFER_SIZE) == );
assert(mpi_read_binary(&rsa.Q, r.rsa_q, BUFFER_SIZE) == );
assert(mpi_read_binary(&rsa.DP, r.rsa_dp, BUFFER_SIZE) == );
assert(mpi_read_binary(&rsa.DQ, r.rsa_dq, BUFFER_SIZE) == );
assert(mpi_read_binary(&rsa.QP, r.rsa_qp, BUFFER_SIZE) == ); rsa.len = (mpi_msb(&rsa.N) + ) >> ; assert(rsa_pkcs1_decrypt(&rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE, &plaintext_size, ciphertext, plaintext, plaintext_size) == );
} void test_aes()
{
// 产生随机的AES key buffer
ctr_drbg_context ctr_drbg;
entropy_context entropy;
unsigned char aes_key_buf[AES_KEY_SIZE] = { }; entropy_init(&entropy);
assert(ctr_drbg_init(&ctr_drbg, entropy_func, &entropy, nullptr, ) == );
ctr_drbg_set_prediction_resistance(&ctr_drbg, CTR_DRBG_PR_OFF);
ctr_drbg_random(&ctr_drbg, aes_key_buf, AES_KEY_SIZE); // 生成AES
aes_context aes_enc, aes_dec;
aes_init(&aes_enc);
aes_init(&aes_dec); assert(aes_setkey_enc(&aes_enc, aes_key_buf, AES_KEY_SIZE) == );
assert(aes_setkey_dec(&aes_dec, aes_key_buf, AES_KEY_SIZE) == ); // 加密 & 解密. 明文与密文的长度是固定的, 都是16bytes
/*
const unsigned int DATA_SIZE = 16;
unsigned char plaintext[DATA_SIZE] = { 0 };
unsigned char ciphertext[DATA_SIZE] = { 0 };
sprintf((char*)plaintext, "%s", "moyakukudi"); assert(aes_crypt_ecb(&aes_enc, AES_ENCRYPT, plaintext, ciphertext) == 0);
memset(plaintext, 0, DATA_SIZE);
assert(aes_crypt_ecb(&aes_dec, AES_DECRYPT, ciphertext, plaintext) == 0);
*/ // 加密 & 解密. 明文与密文的长度是不固定的, 但必须是16bytes的倍数
const unsigned int DATA_SIZE = ;
unsigned char plaintext[DATA_SIZE] = { };
unsigned char ciphertext[DATA_SIZE] = { };
sprintf((char*)plaintext, "%s", "return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH, assert(aes_crypt_ecb(&aes_dec, AES_DECRYPT, ciphertext, plaintext) == 0);"); const unsigned int IV_SIZE = ;
unsigned char iv[IV_SIZE] = { };
//unsigned char iv2[IV_SIZE] = { 0 };
//ctr_drbg_random(&ctr_drbg, iv, IV_SIZE);
//strcpy((char*)iv2, (const char*)iv); assert(aes_crypt_cbc(&aes_enc, AES_ENCRYPT, DATA_SIZE, iv, plaintext, ciphertext) == );
memset(plaintext, , DATA_SIZE);
memset(iv, , IV_SIZE);
assert(aes_crypt_cbc(&aes_dec, AES_DECRYPT, DATA_SIZE, iv, ciphertext, plaintext) == ); puts("over");
} int main()
{
goto AES; // RSA
RSA:
rsa r;
generate_rsa(r); unsigned char plaintext[] = "moyakukudi";
unsigned char ciphertext[BUFFER_SIZE] = { };
unsigned int ciphertext_len = BUFFER_SIZE;
encrypt(r, plaintext, sizeof(plaintext), ciphertext, ciphertext_len); unsigned char output[BUFFER_SIZE] = { };
unsigned int output_len = BUFFER_SIZE;
decrypt(r, ciphertext, ciphertext_len, output, output_len); // AES
AES: test_aes(); system("pause");
return ;
}
https://www.cnblogs.com/emyueguang/p/4072906.html
polarssl rsa & aes 加密与解密<转>的更多相关文章
- polarssl rsa & aes 加密与解密
上周折腾加密与解密,用了openssl, crypto++, polarssl, cyassl, 说起真的让人很沮丧,只有openssl & polarssl两个库的RSA & AES ...
- 探讨数据进行AES加密和解密以及.NET Core对加密和解密为我们提供了什么?
前言 对于数据加密和解密每次我都是从网上拷贝一份,无需有太多了解,由于在.net core中对加密和解密目前全部是统一了接口,只是做具体的实现,由于遇到过问题,所以将打算基本了解下其原理,知其然足矣, ...
- 探讨.NET Core中实现AES加密和解密以及.NET Core为我们提供了什么方便!
前言 对于数据加密和解密每次我都是从网上拷贝一份,无需有太多了解,由于在.net core中对加密和解密目前全部是统一了接口,只是做具体的实现,由于遇到过问题,所以将打算基本了解下其原理,知其然足矣, ...
- RSA前台加密后台解密的应用
写在前面 项目安全测试需要将登录功能修改, AES加密不符合要求, 现改为RSA非对称加密.(将登录密码加密后传给后台, 后台解密后再进行一系列的校验) .期间遇到了前台js加密但是后台解密失败的问题 ...
- Php AES加密、解密与Java互操作的问题
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- 密码疑云 (3)——详解RSA的加密与解密
上一篇文章介绍了RSA涉及的数学知识,本章将应用这些知识详解RSA的加密与解密. RSA算法的密钥生成过程 密钥的生成是RSA算法的核心,它的密钥对生成过程如下: 1. 选择两个不相等的大素数p和q, ...
- Oracle的AES加密与解密用法
Oracle的AES加密与解密用法2013年12月11日 11:50:35 iteye_751 阅读数:428--加密字符串create or replace function des3_enc( i ...
- java独立小程序实现AES加密和解密
一.需求: web项目中配置文件配置的密码是明文的, 现在需要修改成密文, 加密方式采用AES, 于是写了个工具类用于加密和解密. 又因为这个密码是由客户来最终确定, 所以为了部署时方便起见, 写了个 ...
- php的AES加密、解密类
<?php /** * php.ios.Android 通用的AES加密.解密方法 */ namespace Common\Business; class AESCrypt { /** * 初始 ...
随机推荐
- VsCode使用setting sync 同步自己的插件和设置等
直接再 Vscode中安装就可以,然后: 1. 可以点看setting sync插件在vscode 这个时候可以按照提示进行设置(也可以参考下:https://www.cnblogs.com/kenz ...
- elementUI——主题定制
需求: 设计三套主题色+部分图标更换: 实现方式汇总: 1.传统做法,生成多套css主题包,切换link引入路径切换href实现,参考网站:http://jui.org/: <link id=& ...
- django使用admin站点上传图片
Django有提供文件系统支持,在Admin站点中可以轻松上传图片.使用Admin站点保存图片,需要安装Python的图片操作包 pip install Pillow 1 配置 默认情况下,Dja ...
- 【MySQL】SQL语句基础
一.操作数据库 1.1 创建数据库 1.2 查看数据库 1.3 修改数据库 1.4 删除数据库 1.5 选择数据库 二.操作表 2.1 创建表 2.2 查看表 2.3 修改表 2.4 删除表 三.操作 ...
- javascript_18-Array 数组
数组 数组-引用类型,JavaScript中的内置对象 Array对象的属性 length 获取数组的长度(元素个数) 检测数组 instanceof Array.isArray() //h5新增 常 ...
- 腾讯云服务器搭建WampServer环境
软件环境Windows Server 2008 R2 企业版 SP1 64位 刚刚进入 Windows Server ,你会看到以下界面: 列出了服务器的基础信息和常用配置下载 XAMPP https ...
- obj + mtl 格式说明
OBJ(或 .OBJ)是一种开放的几何定义文件格式,最初由Wavefront Technologies公司开发,用以描述其Advanced Visualizer动画包.该格式已被其他3D图形应用供应商 ...
- Qt错误: 程序数据库管理器不匹配 请检查安装
错误提示: C1902: 程序数据库管理器不匹配:请检查安装解决 解决方法: 到D:\VisualStudio2015\VC\bin目录下面拷贝mspdbsrv.exe.mspdb140.dll.ms ...
- Please provide compiled classes of your project with sonar.java.binaries property
是因为一个jar包版本的原因,sonar-java-plugin-5.1.0.13090.jar 需要降级 https://repo.maven.apache.org/maven2/org/sonar ...
- 开发一个代码的自动生成器,使用Jfinal4.3+Swagger+Sql
-- 所有表名select column_name 列名, data_type 字段类型, column_comment 字段注释 from information_schema.columns ...