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. dom4j微信接口开发

    新建一个web项目,我用的是eclipse和tomcat7.0 ,外网环境用的nat123 先建立一个实体bean:TextMessage /** * xml基本对象 * @author xiaohu ...

  2. Android颜色资源文件

    <?xml version="1.0" encoding="utf-8"?><resources> <color name=&qu ...

  3. 构建Logstash+tomcat镜像(让logstash收集tomcat日志)

    1.首先pull logstash镜像作为父镜像(logstash的Dockerfile在最下面): 2.构建my-logstash镜像,使其在docker镜像实例化时,可以使用自定义的logstas ...

  4. Android ListView 子控件点击事件

    android:descendantFocusability beforeDescendants:viewgroup会优先其子类控件而获取到焦点 afterDescendants:viewgroup只 ...

  5. PythonDay02

    >三目运算符 简单的if---else---语句 result = 1234 if 1 > 2 else 4321 print(result) >集合 set集合,是一个无序且不重复 ...

  6. kafka 命令行操作

    1.创建主题(topic) bin/kafka-topics.sh --create --zookeeper m6:2181 --replication-factor 1 --partitions 1 ...

  7. 在xcode运行编译时,编译成功,但项目中显示缺少该文件,这是只要关闭重启xcode即可。

    在xcode运行编译时,编译成功,但项目中显示缺少该文件,这是只要关闭重启xcode即可.

  8. windows磁盘分区

    windows 下对磁盘进行分区吗,如何调整大小. N的输入单位为GB,输出单位为MB; (N-1)4+1024N;

  9. objective-c第六章课后练习6

    题6:接受从终端输入的整数,提取并用英语显示这个数的每一个数字,如932,显示nine three two (题目中注了.这个练习很难)的确有点难,自己想了很久网上也各种搜索.也算是找到参考了 cod ...

  10. mysql数据库的主从

    1. Slave 上面的IO线程连接上 Master,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容; 2. Master 接收到来自 Slave 的 IO 线程的请求后,通过负责 ...