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 { /** * 初始 ...
随机推荐
- AutoMapper 初次使用心得
本例以asp.net webform为例: 结构: 主要代码:AutoMapperConfig 类 public class AutoMapperConfig { public static void ...
- Cephfs 部署 创建 metadata 池 data池
上一次部署了ceph分布式存储,接下来我们部署ceph的文件系统.Ceph文件系统至少需要两个RADOS池,一个用于数据,一个用于元数据. 创建metadata 池 后面数字表示 PG 和pgp数 c ...
- c# Path类
- c# IComparable与IComparer接口
- array_pop
array_pop() 函数删除数组中的最后一个元素,返回数组的最后一个值.如果数组是空的,或者非数组,将返回 NULL. <?php$a=["red","gree ...
- prometheus operator 部署
prometheus operator 部署自定义记录 环境: k8s 1.11集群版本,kubeadm部署 docker 17.3.2版本 Centos 7系统 阿里云服务器 operator 源码 ...
- 小顶堆第二弹-----堆降序排序(C语言非递归)
现在po一下C语言版本的,留作以后接口使用. 1 #include <stdio.h> #include <stdlib.h> #define HEAP_SIZE 100 #d ...
- WEB前端-搜索引擎工作原理与SEO优化
一.搜索引擎工作原理 搜索引擎的工作分为三个阶段,即爬行,索引和检索 1.爬行 搜索引擎具有网络爬虫或蜘蛛来执行爬网,每次抓取工具访问网页时,它都会复制该网页并将其网址添加到索引中. 在“蜘蛛”抓取 ...
- test20190815 NOIP2019 模拟题
100+60+40=200,被后面两个题卡着我很不爽. 立方数 [问题描述] 作为 XX 战队的狂热粉丝,MdZzZZ 看到了自己心仪的队伍在半决赛落败,顿时 心灰意冷.看着自己手中的从黄牛那里抢来的 ...
- Django示例演示--?
引用自:https://blog.csdn.net/weixin_42950386/article/details/83143293 定义模型类 模型类被定义在"应用/models.py&q ...