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 ;
}
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 { /** * 初始 ...
随机推荐
- 关于qt QWebKit/QWebview 使用心得
当前项目为c/s客户端,采用qt4.8.7,需要使用仪表盘.折线图.柱状图等,曾经使用过qwt和自定义的图形控件,但是都不尽如人意.最近发现ECharts控件不错.为此就要在qt端使用web的技术.为 ...
- Ubuntu下qemu环境搭建
在查找资料过程中,发现自己搭建虚拟的arm环境的话,有一个比较好的软件就是qemu了,当然还有其他的,大家各投所好就好. 接下来说一下qemu环境搭建过程. 其实搭建很简单,作为小白,我还是捣鼓了两三 ...
- 两个有序单链表合并成一个有序单链表的java实现
仅作为备注, 便于自己回顾. import java.util.Arrays; public class MergeSort { public static class LinkedNode<V ...
- 手机web——自适应网页设计(html/css控制)http://mobile.51cto.com/ahot-409516.htm
http://mobile.51cto.com/ahot-409516.htm 一. 允许网页宽度自动调整: "自适应网页设计"到底是怎么做到的? 其实并不难. 首先,在网页代码的 ...
- OpenOffice将MS docx转换成pdf文件偶数页眉不显示问题解决办法
OpenOffice版本:4.0(Windows.Linux下测试都出现问题) MS Office版本:2007 问题描述 使用OpenOffice将MS的docx文件转换为pdf文件时,docx文件 ...
- vue 中view层中方法的使用
1.使用filters computed:{ }, filters: { filterA: function(value) { return value + 'wh' } }, 2.用法: {{it ...
- springmvc接口ios网络请求
springmvc: application/json;charset=utf-8的ios网络请求: 后台使用 @RequestBody注解参数接收:
- UITableView:改变 TableHeaderView 的高度
参考:http://stackoverflow.com/a/526825 有这么一种需求,在列表顶端显示一些别样的数据,而这个别样的数据则需要通过一个别样的 View 来展现,它便是 UITableV ...
- iOS: 查看 UIView 的视图树
在想要查看的 UIView 附近打个断点,运行,直到停在断点处,在控制台键入:po [view recursiveDescription],回车. (lldb) po [self recursiveD ...
- C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation
C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation 后台添加以下方法:/// <summa ...