上周折腾加密与解密,用了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 加密与解密<转>的更多相关文章

  1. polarssl rsa & aes 加密与解密

    上周折腾加密与解密,用了openssl, crypto++, polarssl, cyassl, 说起真的让人很沮丧,只有openssl & polarssl两个库的RSA & AES ...

  2. 探讨数据进行AES加密和解密以及.NET Core对加密和解密为我们提供了什么?

    前言 对于数据加密和解密每次我都是从网上拷贝一份,无需有太多了解,由于在.net core中对加密和解密目前全部是统一了接口,只是做具体的实现,由于遇到过问题,所以将打算基本了解下其原理,知其然足矣, ...

  3. 探讨.NET Core中实现AES加密和解密以及.NET Core为我们提供了什么方便!

    前言 对于数据加密和解密每次我都是从网上拷贝一份,无需有太多了解,由于在.net core中对加密和解密目前全部是统一了接口,只是做具体的实现,由于遇到过问题,所以将打算基本了解下其原理,知其然足矣, ...

  4. RSA前台加密后台解密的应用

    写在前面 项目安全测试需要将登录功能修改, AES加密不符合要求, 现改为RSA非对称加密.(将登录密码加密后传给后台, 后台解密后再进行一系列的校验) .期间遇到了前台js加密但是后台解密失败的问题 ...

  5. Php AES加密、解密与Java互操作的问题

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  6. 密码疑云 (3)——详解RSA的加密与解密

    上一篇文章介绍了RSA涉及的数学知识,本章将应用这些知识详解RSA的加密与解密. RSA算法的密钥生成过程 密钥的生成是RSA算法的核心,它的密钥对生成过程如下: 1. 选择两个不相等的大素数p和q, ...

  7. Oracle的AES加密与解密用法

    Oracle的AES加密与解密用法2013年12月11日 11:50:35 iteye_751 阅读数:428--加密字符串create or replace function des3_enc( i ...

  8. java独立小程序实现AES加密和解密

    一.需求: web项目中配置文件配置的密码是明文的, 现在需要修改成密文, 加密方式采用AES, 于是写了个工具类用于加密和解密. 又因为这个密码是由客户来最终确定, 所以为了部署时方便起见, 写了个 ...

  9. php的AES加密、解密类

    <?php /** * php.ios.Android 通用的AES加密.解密方法 */ namespace Common\Business; class AESCrypt { /** * 初始 ...

随机推荐

  1. 用D3.js画树状图

    做项目遇到一个需求,将具有层级关系的词语用树状图的形式展示它们之间的关系,像这样: 或者是这样: 上面的图片只是样例,跟我下面的代码里面用的数据不同 网上有很多这种数据可视化展示的js控件,我这里选择 ...

  2. postgressql启动与关闭

    POSTGRESSQL启动和停止数据库可以通过service方式 .POSTGRESSQL的命令行工具进行启动与停止. 1.使有SERVICE方式 启动数据库服务的命令如下·: #service po ...

  3. django获取数据queryset中的filter选项

    2.条件选取querySet的时候,filter表示=,exclude表示!=. querySet.distinct() 去重复__exact 精确等于 like 'aaa' __iexact 精确等 ...

  4. 微博MySQL优化之路

    数据库是所有架构中不可缺少的一环,一旦数据库出现性能问题,那对整个系统都回来带灾难性的后果.并且数据库一旦出现问题,由于数据库天生有状态(分主从)带数据(一般还不小),所以出问题之后的恢复时间一般不太 ...

  5. jquery 下拉一屏

  6. 《BUG创造队》作业8:软件测试与Alpha冲刺(第三天)

    项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 BUG创造队 作业学习目标 (1)掌握软件测试基础技术.(2)学习 ...

  7. 运行Vue项目,没办法自动打开浏览器,提示“Unable to open browser. If you are running in a headless environment, please do not use the open flag.”

    留坑,待解决 Unable to open browser. If you are running in a headless environment, please do not use the o ...

  8. 十五.ProtoBuf3的基础总结

    转自: https://blog.csdn.net/u011518120/article/details/54604615 定义一个消息类型 指定字段类型 分配标识号 指定字段规则 添加更多消息类型 ...

  9. 如何修改host

    因不可抗拒的原因,有些网站会被q,但只是比较恶心的域名DNS污染,并不需要tiizi,修改hosts文件即可. 以 www.youneed.win 为例: 首先,进入目录:C:\Windows\Sys ...

  10. 移动端videojs视频插件使用直播流rtmp、hls、http-flv的注意事项

    可以访问:https://videojs.com/ 下载对应的脚本包 特别注意的是 移动端videojs一般应用的直播流协议为HLS, RTMP协议一般是PC上使用,需要flash支持. HLS直播源 ...