#import "GLQyRsa.h"
#import "GLSupprot.h"
#import "GLLoginViewController.h" @implementation GLQyRsa static SecKeyRef _public_key=nil;
+ (SecKeyRef) getPublicKeyFile
{ // 从公钥证书文件中获取到公钥的SecKeyRef指针
if(_public_key == nil){
//NSData *certificateData = [RSA_KEY_BASE64 dataUsingEncoding:NSUTF8StringEncoding];
// NSString *publicKeyPath = [[NSBundle mainBundle] pathForResource:@"public_key"
// ofType:@"der"];
// if (publicKeyPath == nil) {
// NSLog(@"Can not find pub.der");
// return nil;
// }
NSString *fielName = [[NSUserDefaults standardUserDefaults]objectForKey:my_publicKeyFileName];
//NSLog(@"fielName:%@",fielName); fileName为.cer证书
if(!fielName)
{
NSLog(@"fielName nil");
return nil;
}
NSDate *certificateData = [NSData dataWithContentsOfFile:fielName];
if (certificateData == nil) {
NSLog(@"Can not read from pub.der");
return nil;
}
SecCertificateRef myCertificate = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)certificateData);
SecPolicyRef myPolicy = SecPolicyCreateBasicX509();
SecTrustRef myTrust;
OSStatus status = SecTrustCreateWithCertificates(myCertificate,myPolicy,&myTrust);
SecTrustResultType trustResult;
if (status == noErr) {
status = SecTrustEvaluate(myTrust, &trustResult);
}
_public_key = SecTrustCopyPublicKey(myTrust);
CFRelease(myCertificate);
CFRelease(myPolicy);
CFRelease(myTrust);
}
return _public_key;
} + (NSData*) rsaEncryptString:(NSString*) string{ SecKeyRef key = [self getPublicKeyFile];
if(!key)
{
NSLog(@"secKeyRefNULL");
return nil;
} size_t cipherBufferSize = SecKeyGetBlockSize(key);
uint8_t *cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));
NSData *stringBytes = [string dataUsingEncoding:NSUTF8StringEncoding];
size_t blockSize = cipherBufferSize - ;
size_t blockCount = (size_t)ceil([stringBytes length] / (double)blockSize);
NSMutableData *encryptedData = [[NSMutableData alloc] init];
for (int i=; i<blockCount; i++) {
int bufferSize = MIN(blockSize,[stringBytes length] - i * blockSize);
NSData *buffer = [stringBytes subdataWithRange:NSMakeRange(i * blockSize, bufferSize)];
OSStatus status = SecKeyEncrypt(key, kSecPaddingPKCS1, (const uint8_t *)[buffer bytes],
[buffer length], cipherBuffer, &cipherBufferSize);
if (status == noErr){
NSData *encryptedBytes = [[NSData alloc] initWithBytes:(const void *)cipherBuffer length:cipherBufferSize];
[encryptedData appendData:encryptedBytes]; }else{
if (cipherBuffer) free(cipherBuffer);
return nil;
}
} if (cipherBuffer) free(cipherBuffer);
// NSLog(@"Encrypted text (%d bytes): %@", [encryptedData length], [encryptedData description]);
// NSLog(@"Encrypted text base64: %@", [Base64 encode:encryptedData]);
return encryptedData;
}

iOS RSA 证书加密的更多相关文章

  1. IdentityServer4 SigningCredential(RSA 证书加密)

    IdentityServer4 默认提供了两种证书加密配置: services.AddIdentityServer() .AddDeveloperSigningCredential() .AddTem ...

  2. 使用X.509数字证书加密解密实务(二)-- 使用RSA证书加密敏感数据

    一.  使用RSA证书加.解密敏感数据 X.509证书标准支持三种不对称加密算法:RSA, DSA, Diffie-Hellman algorithms.最常用的是RSA算法.所以本文就以前面章节使用 ...

  3. iOS RSA非对称加密测试流程

    非对称加密需要两把钥匙:公钥和秘钥. 单向加密:一般情况下服务器会持有秘钥和公钥,那该怎么使用呢?以注册场景为例,最初服务器持有公钥和密钥. 用户注册时不是直接发送用户名,密码,验证码等明文信息给服务 ...

  4. iOS RSA的加密和签名

    1.RSA加密使用服务端给的公钥.pem,RSA签名使用客户端的私钥.pem. 参考文章:http://www.jianshu.com/p/4580bee4f62f 把文件夹导入项目中,然后配置这两个 ...

  5. C#使用RSA证书文件加密和解密示例

    修改MSDN上的示例,使之可以通过RSA证书文件加密和解密,中间遇到一个小问题. Q:执行ExportParameters()方法时,回报CryptographicException:该项不适于在指定 ...

  6. 使用X.509数字证书加密解密实务(三)-- 使用RSA证书结合对称加密技术加密长数据

    一.  使用证书结合对称加密算法加.解密长数据 上一章节讨论了如何使用RSA证书加密数据,文中提到:“Dotnet的RSA实现有个特点,它必须要在明文中添加一些随机数,所以明文不能把128字节占满,实 ...

  7. RSA加密,以及证书加密

    简单的加密:https://blog.csdn.net/qq_32523587/article/details/79092364 生成证书:https://blog.csdn.net/u0121916 ...

  8. RSA der加密 p12解密以及配合AES使用详解

    在前面的文章中我有说过AES和RSA这两种加密方式,正好在前段时间再项目中有使用到,在这里再把这两种加密方式综合在一起写一下,具体到他们的使用,以及RSA各种加密文件的生成. 一: RSA各种加密相关 ...

  9. iOS RSA加解密签名和验证

    转自:http://www.jianshu.com/p/81b0b54436b8 Pre:在公司负责了一个项目,需要用到iOS RSA验证签名的功能.后台给我的仅仅是一个公钥的字符串.经过起初的一段时 ...

随机推荐

  1. c# 调用分页(控制器端的)与时间的格式

    1.在操作的model中: 2.在控制器中: , command.PageSize);//将从数据库获取到的集合进行分页 var gridModel = new DataSourceResult { ...

  2. 网站建设中前端常用的jQuery+easing缓动的动画

    网站建设中前端人员利用jQuery实现动画再简单不过了,只是要实现更酷的效果还需要插件来帮忙,easing就是一款帮助jQuery实现缓动动画的插件,经过试用,效果很不错! 下载该插件:jquery. ...

  3. C++中new和malloc的区别

    原文:http://blog.163.com/ji_wei8888/blog/static/4868044620117361747282/ 1.new 是c++中的操作符,malloc是c 中的一个函 ...

  4. STL之map应用 +hash表(51nod 1095)

    题目:Anigram单词 题意:给出词典,再给出一些单词,求单词的Anigram数量. 思路:先将字串转换成哈希表,然后再用map链接. hash表构造方法汇总:http://www.cnblogs. ...

  5. android的多渠道打包

    本文出处:http://www.cnblogs.com/0616--ataozhijia/p/4203997.html 这里以友盟为例子. 项目快上线了,要做一个多渠道打包.不然每次都要在Androi ...

  6. Mysql --分区表(7)Key分区

    Key分区 按照Key进行分区非常类似于按照Hash进行分区,只不过Hash分区允许使用用户自定义的表达式,而Key分区不允许使用用户自定义的表达式,需要使用MySQL服务器提供的HASH函数;同时H ...

  7. Linux中exec命令相关

    Linux中exec命令相关 exec和source都属于bash内部命令(builtins commands),在bash下输入man exec或man source可以查看所有的内部命令信息. b ...

  8. STL源码--iterator和traits编程技法

    第一部分 iterator学习 STL iterators定义: 提供一种方法,使之能够依序巡访某个聚合物(容器)所含的各个元素,而又无需暴露该聚合物的内部表达方式. 任何iteartor都应该提供5 ...

  9. 热烈庆祝杨学明老师为苏宁、中兴、烽火、CNNIC、创维、金立、中航信等知名企业提供培训和咨询服务!

    在2015年三季度,研发资深顾问.资深讲师杨学明先生为国内多家名企提供了培训和咨询服务!由于杨学明老师在软件及互联网方面的管理经验极为丰富,被多家公司选为首席研发讲师!并聘为常年顾问!

  10. Orchard 精简版

    Orchard Express v1.7.2 精简版 保留Orchard.Framework和Orchard.Core全部源码(一字未改),去除非必要模块(仅剩Orchard.jQuery, Orch ...