Cryptopp 是一个c++写的功能完善的密码学工具,类似于openssl

官网:https://www.cryptopp.com

以下主要演示Cryptopp 在iOS上的RSA加密解密签名与验证签名

1. 编译cryptopp为iOS上使用的静态库

我整理好了一份 cryptopp5.6.2版本的打包脚本随后在下面DEMO中一起发布,需要可自行下载

编译其他版本的,简单修改脚本就行

终端运行脚本

sh build-cryptopp.sh

就会生成如下目录结构,

生成的静态库是通用版的,真机模拟器合并的,比较大200多M,实测打包之后几百K,

打包之后libcryptopp.a 和 include 的头文件,在ios上使用

2. 将上述打包的静态库以及 头文件导入DEMO工程CryptoppDemo

将测试用的viewController.m 修改为 ViewController.mm

主要为了实现 oc ,c++的混编

3. 使用 md5

当然还支持其他hash算法

-(NSData*)getMD5Value:(NSData*)data {
CryptoPP::MD5 md5;
byte digest[ CryptoPP::MD5::DIGESTSIZE ]; md5.CalculateDigest(digest, (const byte*)[data bytes], [data length]); NSData * hashVale = [NSData dataWithBytes:digest length:sizeof digest];
return hashVale;
} //MD5
NSString *testStr = @"hello world md5";
NSData *testDAT = [testStr dataUsingEncoding:NSUTF8StringEncoding];
NSData *md5Dat = [self getMD5Value:testDAT];
NSLog(@"%lu",md5Dat.length);
NSMutableString *s = [NSMutableString string];
unsigned char * hashValue = (byte *)[md5Dat bytes]; int i;
for (i = ; i < [md5Dat length]; i++) {
[s appendFormat:@"%02x", hashValue[i]];
}
NSLog(@"%@",s);

4. RSA 密钥对生成

    //RSA 生成密钥对
char seed[]={};
unsigned int keyLength = ;
char *privFilename = "/Users/cocoajin/Desktop/priKey.txt";
char *pubFilename = "/Users/cocoajin/Desktop/pubKey.txt"; CryptoPP::RandomPool randPool;
randPool.IncorporateEntropy((byte *)seed, strlen(seed)); CryptoPP::RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength);
CryptoPP::HexEncoder privFile(new CryptoPP::FileSink(privFilename));
priv.DEREncode(privFile);
privFile.MessageEnd(); CryptoPP::RSAES_OAEP_SHA_Encryptor pub(priv);
CryptoPP::HexEncoder pubFile(new CryptoPP::FileSink(pubFilename));
pub.DEREncode(pubFile);
pubFile.MessageEnd();

5. RSA 加密与解密

    //RSA 加密
char seed[]={};
char *pubFilename = "/Users/cocoajin/Desktop/pubKey.txt";
char *message = "hello ios cryptopp";
printf("%s\n",message);
CryptoPP::FileSource pubFile(pubFilename, true, new CryptoPP::HexDecoder);
CryptoPP::RSAES_OAEP_SHA_Encryptor pub(pubFile); CryptoPP::RandomPool randPool;
randPool.IncorporateEntropy((byte *)seed, strlen(seed)); std::string encresult;
CryptoPP::StringSource(message, true, new CryptoPP::PK_EncryptorFilter(randPool, pub, new CryptoPP::HexEncoder(new CryptoPP::StringSink(encresult))));
std::cout << encresult << std::endl; //RSA 解密
char *privFilename = "/Users/cocoajin/Desktop/priKey.txt"; CryptoPP::FileSource privFile(privFilename, true, new CryptoPP::HexDecoder);
CryptoPP::RSAES_OAEP_SHA_Decryptor priv(privFile); CryptoPP::AutoSeededRandomPool _rng;
CryptoPP::RSAES_OAEP_SHA_Decryptor tpriv(_rng, ); std::string decresult;
CryptoPP::StringSource(encresult.c_str(), true, new CryptoPP::HexDecoder(new CryptoPP::PK_DecryptorFilter(_rng, priv, new CryptoPP::StringSink(decresult))));
std::cout << decresult << std::endl;

6. 签名与验证签名

    //RSA 签名与较验签名
char *privFilename = "/Users/cocoajin/Desktop/priKey.txt";
char *pubFilename = "/Users/cocoajin/Desktop/pubKey.txt"; char *signatureFilename = "/Users/cocoajin/Desktop/sin.txt";
char *messageFileName = "/Users/cocoajin/Desktop/NOTES.txt";
CryptoPP::FileSource privFile(privFilename, true, new CryptoPP::HexDecoder); //GlobalRNG
CryptoPP::AutoSeededRandomPool _rng;
CryptoPP::RSAES_OAEP_SHA_Decryptor tpriv(_rng, ); CryptoPP::RSASSA_PKCS1v15_SHA_Signer priv(privFile);
CryptoPP::FileSource f(messageFileName, true, new CryptoPP::SignerFilter(_rng, priv, new CryptoPP::HexEncoder(new CryptoPP::FileSink(signatureFilename)))); //验证签名
CryptoPP::FileSource pubFile(pubFilename, true, new CryptoPP::HexDecoder);
CryptoPP::RSASSA_PKCS1v15_SHA_Verifier pub(pubFile); CryptoPP::FileSource signatureFile(signatureFilename, true, new CryptoPP::HexDecoder);
if (signatureFile.MaxRetrievable() != pub.SignatureLength())
{
printf("\nNO\n");
return;
}
CryptoPP::SecByteBlock signature(pub.SignatureLength());
signatureFile.Get(signature, signature.size()); CryptoPP::VerifierFilter *verifierFilter = new CryptoPP::VerifierFilter(pub);
verifierFilter->Put(signature, pub.SignatureLength());
CryptoPP::FileSource ff(messageFileName, true, verifierFilter);
printf("\n%d\n",verifierFilter->GetLastResult());

7. 其他相关cryptopp的用法可以参考:源码test.cpp

8. CryptoppDemo下载

https://github.com/cocoajin/TDDDemo/tree/master/CryptoppDemo

参考:

https://github.com/3ign0n/CryptoPP-for-iOS

https://my.oschina.net/u/566591/blog/168520

Cryptopp iOS 使用 RSA加密解密和签名验证签名的更多相关文章

  1. openssl 非对称加密 RSA 加密解密以及签名验证签名

    1. 简介 openssl  rsa.h 提供了密码学中公钥加密体系的一些接口, 本文主要讨论利用rsa.h接口开发以下功能 公钥私钥的生成 公钥加密,私钥解密 私钥加密,公钥解密 签名:私钥签名 验 ...

  2. RSA 加密 解密 公钥 私钥 签名 加签 验签

    http://blog.csdn.net/21aspnet/article/details/7249401# http://www.ruanyifeng.com/blog/2013/06/rsa_al ...

  3. RSA加密解密与签名验证

    关于RSACryption帮助类定义见RSACryption 一.加密与解密 //定义明文和密文变量 string plaintext = "天道酬勤,厚德载物!"; string ...

  4. iOS RSA加密解密及签名验证

    1.首先要下载openssl,这个不用说,直接官网下载或者用brew install openssl下载 2.终端生成私钥密钥 2.1生成私钥 openssl genrsa - 2.2生成密钥 ope ...

  5. iOS RSA 加密解密及签名验证

    1.首先要下载openssl.这个不用说,直接官网下载或者用brew install openssl下载. 2.终端生成私钥密钥. 2.1生成私钥 openssl genrsa - 2.2生成密钥 o ...

  6. iOS使用Security.framework进行RSA 加密解密签名和验证签名

    iOS 上 Security.framework为我们提供了安全方面相关的api: Security框架提供的RSA在iOS上使用的一些小结 支持的RSA keySize 大小有:512,768,10 ...

  7. iOS常用加密之RSA加密解密

    前言: iOS常用的加密有很多种,前两天在工作中遇到了RSA加密,现在把代吗分享出来. RSA基本原理 RSA使用"秘匙对"对数据进行加密解密.在加密解密数据前,需要先生成公钥(p ...

  8. RSA加密解密及数字签名Java实现--转

    RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院 ...

  9. RSA加密解密和读取公钥、私钥

    /// <summary>     /// RSA加密解密及RSA签名和验证    /// </summary>     public class RSADE    {    ...

随机推荐

  1. LeetCode Smallest Rectangle Enclosing Black Pixels

    原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...

  2. Android内存泄露

    Android 内存泄漏是一个十分头疼的事情.LeakCanary是一款开源软件,主要作用是检测 Android APP 内存泄露.比起以前的 MAT 工具,LeakCanary 有着十分强大的功能, ...

  3. LUA脚本调用C场景,使用C API访问脚本构造的表

    LUA调用C lua解析中集成了一些系统服务, 故脚本中可以访问系统资源, 例如, lua脚本可以调用文件系统接口, 可以调用数学库, 但是总存在一些lua脚本中访问不到的系统服务或者扩展功能, 如果 ...

  4. 分享:一款前端布局工具(alloydesigner)

    困扰 设计师给出静态的高保真图片, 需要前端工程师按照高保真图,进行html编码. 前端工程师, 一般工作方法为: 打开图片,一边看下图片, 一边编写相应的html代码. 这样有两个问题: 1. 前端 ...

  5. RDIFramework.NET ━ 9.5 组织机构管理 ━ Web部分

    RDIFramework.NET ━ .NET快速信息化系统开发框架 9.5 组织机构管理 -Web部分 组织机构管理模块提供直观方便的组织机构管理,以树型结构显示单位和部门的机构体系,可根据需要进行 ...

  6. 拼linq 时网上整理的一个类

    public static class DynamicLinqExpressions { public static Expression<Func<T, bool>> Tru ...

  7. Oracle常用操作-----(一)

    Oracle创建约束: ALTER TABLE 表名 ADD CONSTRAINT 约束名 约束内容. [注]Oracle中的default是一个值,而sql server中的default是一个约束 ...

  8. AJAX-----14HTML5中新增的API---files

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. jquery mobile界面数据刷新

    JQM里面当我们更新了某些页面标签(如: listview, radiobuttons, checkboxes, select menus)里的数据时,必须做refresh操作. 为什么必须做refr ...

  10. 【笔记】jquery hover的用法

    hover函数格式: $("A").hover(function(){ //当鼠标移入的时候执行第一个函数 },function(){ //当鼠标移出的时候执行第二个函数 }) * ...