#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. IOS 从一个小地方想到……

    //(一个比较好的地方是 : cancel代表取消的意思,suspended,表示已经挂起,这些英文记住了用来命名挺好的,看看别人的过去时都是加ed的,就是这么强,所以语法不好的话,多关注ios的命名 ...

  2. Nginx环境中如何将HTTP跳转至HTTPS设置

    如果我们VPS服务器的WEB环境采用的是NGINX架构,那如果我们将安装SSL证书的网站希望强制跳转至HTTPS网站URL的时候那需要如何设置呢?这里个人建议是这样的,我们必须要强制一个地址,这样网站 ...

  3. VB6.0 调用存储过程

    最近有在做一个需求,需要在VB6.0中调用存储过程,整理了一下,供大家参考, 范例: Function callStoredProcedure(sEmployeeID As String, Optio ...

  4. linux 常用目录

    /bin   执行文件 ls, mv, rm, mkdir, rmdir, gzip, tar, telnet /boot linux核心文件内核 vmlinuz-xxx(Linux 的 Kernel ...

  5. linux eclipse epic perl padwalker

    1, 在Eclipse中安装EPIC:Help->Install New Software->Add:name:EPICLocation:http://e-p-i-c.sourceforg ...

  6. jquery实现div遮罩层

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. require.js 使用博客

    没时间写博客,又觉得这篇很不错,或许以后能用到,只能copy与点赞: Javascript模块化编程(三):require.js的用法   作者: 阮一峰 日期: 2012年11月 7日 这个系列的第 ...

  8. CodeForces 742B Batch Sort

    B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. ARP协议

    ARP协议就是一个获取对方MAC地址的协议,ARP协议它是一个网络层的协议,它的作用是通过ARP request报文来获得对方的MAC地址,ARP报文里面发送的内容大概是192.168.1.20你的M ...

  10. berkeley db replica机制 - election algorithm

    repmgr_method.c, __repmgr_start_int() 初始2个elect线程. repmgr_elect.c, __repmgr_init_election() __repmgr ...