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 { /** * 初始 ...
随机推荐
- python智能提取省、市、区地址
工具原文 https://github.com/DQinYuan/chinese_province_city_area_mapper 说明: https://blog.csdn.net/qq_3325 ...
- javascript_17-基本类型和引用类型
基本类型 直接存储值 Number . String .Boolean undefined.null 引用类型 存储引用 -Object.Array.Date.函数 包装基本类型--引用类型 func ...
- mysql:[Err] 1068 - Multiple primary key defined
添加主键时,出现错误:[Err] 1068 - Multiple primary key defined #增加主键 ) not null; ; alter table my_test add pri ...
- 剑指offer:二维数组的查找
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- 51nod 2485 小b重排字符串
小b有一个字符串S,现在她希望重排列S,使得S中相邻字符不同. 请你判断小b是否可能成功. 样例解释:将"aab"重排为"aba"即可. 收起 输入 输入一 ...
- 《hello-world》第九次团队作业:【Beta】Scrum meeting 3
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十三 团队作业9:Beta冲刺与团队项目验收 团队名称 <hello--wor ...
- 三.protobuf3标量值类型
Protobuf3 标量值类型 标量消息字段可以具有以下类型之一——该表显示了.proto文件中指定的类型,以及自动生成的类中的相应类型: .proto类型 说明 C++ 类型 Java 类型 Pyt ...
- 通过类型断言获取error类型,获得更详细的信息
package main import ( "fmt" "os" ) func main() { f, err := os.Open("/test.t ...
- shiro授权+注解式开发
shiro授权和注解式开发 1.shiro授权角色.权限 2.Shiro的注解式开发 ShiroUserMapper.xml <select id="getRolesByUserId& ...
- 获取当前主题颜色 Flutter
通过context获取当前主题颜色 Theme.of(context).accentColor