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. Selenium脚本编写环境的搭建/XPath

    编写环境主要分为三个部分: JUnit : java单元测试框架: Firebug: firefox 附加组件,Firebug是firefox下的一个扩展,能够调试所有网站语言,如Html,Css等, ...

  2. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

  3. SQL基础巩固1

    直接开门见山------F4 1.查询 对指定的数据表或是视图进行检索查询,找出符合查询条件功能,具体包括以下SQL语法如下所示,其中[]为可选项 Select<列名> From<表 ...

  4. sql 在not in 子查询有null值情况下经常出现的陷阱

    如果下:TempSalesPriceFixedValues表和SalesPriceFixedValues表,要求查询出在TempSalesPriceFixedValues表中且不在SalesPrice ...

  5. Java实现堆排序(大根堆)

    堆排序是一种树形选择排序方法,它的特点是:在排序的过程中,将array[0,...,n-1]看成是一颗完全二叉树的顺序存储结构,利用完全二叉树中双亲节点和孩子结点之间的内在关系,在当前无序区中选择关键 ...

  6. TCP/IP网络编程中socket的行为

    一. read/write的语义:为什么会阻塞? 先从write说起: #include <unistd.h>ssize_t write(int fd, const void *buf, ...

  7. 我的AngularJS 学习之旅(二)

    记得某位大神说过,"时间就像海绵里的水,挤挤总是有的.".大多时候,与其说我是很忙而没时间去做自己想做的事, 倒不如说是懒得去做罢了. 废话不多说,接前一篇继续吧 3.3 指令(D ...

  8. SqlServer数据库端口默认是1433吗?

    1433端口,是SQL Server默认的端口,SQL Server服务使用两个端口:TCP-1433.UDP-1434.其中1433用于供SQL Server对外提供服务,1434用于向请求者返回S ...

  9. [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  10. java获取获得Timestamp类型的当前系统时间。以及java.util.date 、java.sql.Date之间的转换

    java获取取得Timestamp类型的当前系统时间java获取取得Timestamp类型的当前系统时间 格式:2010-11-04 16:19:42 方法1: Timestamp d = new T ...