Cryptopp iOS 使用 RSA加密解密和签名验证签名
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加密解密和签名验证签名的更多相关文章
- openssl 非对称加密 RSA 加密解密以及签名验证签名
1. 简介 openssl rsa.h 提供了密码学中公钥加密体系的一些接口, 本文主要讨论利用rsa.h接口开发以下功能 公钥私钥的生成 公钥加密,私钥解密 私钥加密,公钥解密 签名:私钥签名 验 ...
- RSA 加密 解密 公钥 私钥 签名 加签 验签
http://blog.csdn.net/21aspnet/article/details/7249401# http://www.ruanyifeng.com/blog/2013/06/rsa_al ...
- RSA加密解密与签名验证
关于RSACryption帮助类定义见RSACryption 一.加密与解密 //定义明文和密文变量 string plaintext = "天道酬勤,厚德载物!"; string ...
- iOS RSA加密解密及签名验证
1.首先要下载openssl,这个不用说,直接官网下载或者用brew install openssl下载 2.终端生成私钥密钥 2.1生成私钥 openssl genrsa - 2.2生成密钥 ope ...
- iOS RSA 加密解密及签名验证
1.首先要下载openssl.这个不用说,直接官网下载或者用brew install openssl下载. 2.终端生成私钥密钥. 2.1生成私钥 openssl genrsa - 2.2生成密钥 o ...
- iOS使用Security.framework进行RSA 加密解密签名和验证签名
iOS 上 Security.framework为我们提供了安全方面相关的api: Security框架提供的RSA在iOS上使用的一些小结 支持的RSA keySize 大小有:512,768,10 ...
- iOS常用加密之RSA加密解密
前言: iOS常用的加密有很多种,前两天在工作中遇到了RSA加密,现在把代吗分享出来. RSA基本原理 RSA使用"秘匙对"对数据进行加密解密.在加密解密数据前,需要先生成公钥(p ...
- RSA加密解密及数字签名Java实现--转
RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院 ...
- RSA加密解密和读取公钥、私钥
/// <summary> /// RSA加密解密及RSA签名和验证 /// </summary> public class RSADE { ...
随机推荐
- 磨刀不误砍柴工,使用visual studio之前应该先了解这些...
注:以下的快捷键在vs2010中测试无误. 众所周知,vs是一个非常强大的开发平台,但是又有多少小伙伴熟悉以下这些快捷键呢? 当然,不知道这些快捷键与开发并没有直接关系,不过,就我而言,以下这些快捷键 ...
- sql 时间差
select * from Tickets where ( case when UnloadTime is null then datediff(hh,LoadTime,getdate()) else ...
- RDIFramework.NET 中多表关联查询分页实例
RDIFramework.NET 中多表关联查询分页实例 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,给用户和开发者最佳的.Net框架部署方案.该框架以SOA范式作为 ...
- 对hashmap与hashcode()、equals()的理解
1.equals方法没被重写的时候 比较的只是对象的地址 重写之后 比较的才是对象里的内容 2.重写equals的时候 务必需要重写hashcode 不然在用到容器的时候 会出现问题 因为容器会 ...
- c# AES加解密并转ASCII码
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...
- 决策树Decision Tree 及实现
Decision Tree 及实现 标签: 决策树熵信息增益分类有监督 2014-03-17 12:12 15010人阅读 评论(41) 收藏 举报 分类: Data Mining(25) Pyt ...
- cookie的三种操作方法
1,jquery.cookie.js 这一篇文章已经写的很详细了: http://www.cnblogs.com/afuge/archive/2013/07/03/3169048.html 2,原生j ...
- spring1冲刺感想与总结
感想与体会: 通过团队合作的第一个sprint,我们知道如何在团队合作中做好自己,还有与队友不合时,如何和平处理问题.产品负责人是一个重要的的角色,他负责任务的安排,处理合作中意见不统一的问题.通过这 ...
- IOS基础面试题
最近离职了,找工作,光会做项目,对基础不熟,今天就总结了一点面试题. 废话不多说,上题吧: 1.objective-c中的数字对象都有哪些,简述它们与基本数据类型的区别是什么. 基本类型和C一样,主要 ...
- android studio 引入第三方类库jar包
第三方类库jar包 这就简单多了,直接将jar包拷贝到app/libs下,然后在app下的build.gradle中添加此jar的依赖.如下: dependencies { compile 'com. ...