在加密和解密中,我们需要了解的知识有什么事openssl;RSA加密算法的基本原理;如何通过openssl生成最后我们需要的der和p12文件。

废话不多说,直接写步骤:

第一步:openssl来生成公钥和私钥证书,最后需要得到公钥证书和私钥证书

这是在mac OX系统下显示的证书,如果我们用文本编辑器打开它,会发现里面是----BEGIN RSA 开头  并且----END RSA 结尾的一串字符串。

第二步:我们需要在代码中写我们的加密和机密方法,加密的字符串通过公钥进行加密,加密后的字符串也可以通过私钥进行解密。

1.在命令行下通过openssl指令获得证书

//生成长度为 1024 的私钥:private_key.pem (文件名可自定义)
openssl genrsa -out private_key.pem //使用私钥文件创建所需的证书:rsaCertReq.csr(文件名可自定义)
openssl req -new -key private_key.pem -out rsaCertReq.csr //使用 x509 创建证书:rsaCert.crt(文件名可自定义)
openssl x509 -req -days -in rsaCertReq.csr -signkey private_key.pem -out rsaCert.crt //生成 .der 格式的公钥:public_key.der(文件名可自定义)
openssl x509 -outform der -in rsaCert.crt -out public_key.der //生成解密所需 .p12文件:private_key.p12(文件名可自定义)
openssl pkcs12 -export -out private_key.p12 -inkey private_key.pem -in rsaCert.crt

在命令行种可能需要你的一些信息去生成公钥和私钥

Country Name (2 letter code) [AU]:CN                                              // 国家码

State or Province Name (full name) [Some-State]:china                     //地区码

Locality Name (eg, city) []:wuhan                   // 本地码

Organization Name (eg, company) [Internet Widgits Pty Ltd]:airway   // 公司名称

Organizational Unit Name (eg, section) []:airway                               // 部门

Common Name (eg, YOUR name) []:airway                                       // 名字

Email Address []:                            //邮箱

注意:在生成密钥对的时候需要填入 私钥的提取密码,请记住,解密的时候需要用到。

2.我们需要完善我们的代码来实现RSA加密和解密

通过在命令行中的操作,我们最后可以得到我们需要的公钥和私钥文件。

static SecKeyRef _public_key = nil;
// 从公钥证书文件中获取到公钥的SecKeyRef指针: @"public_key" ofType:@"der"
NSString *publicKeyPath = [paramsDict stringValueForKey:@"publicKey" defaultValue:@""];
publicKeyPath = [self getPathWithUZSchemeURL:publicKeyPath];
if (![[NSFileManager defaultManager] fileExistsAtPath:publicKeyPath]) {
[self sendResultEventWithCallbackId:rsaCbId dataDict:@{@"status":[NSNumber numberWithBool:false]} errDict:@{@"code":@(-)} doDelete:NO];
return;
}
NSData *certificateData = [[NSData alloc]initWithContentsOfFile:publicKeyPath];
SecCertificateRef myCertificate = SecCertificateCreateWithData(kCFAllocatorDefault, (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); SecKeyRef key = _public_key;
size_t cipherBufferSize = SecKeyGetBlockSize(key);
uint8_t *cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));
NSData *stringBytes = [inString dataUsingEncoding:NSUTF8StringEncoding];
size_t blockSize = cipherBufferSize - ;
size_t blockCount = (size_t)ceil([stringBytes length] / (double)blockSize);
NSMutableData *encryptedData = [[NSMutableData alloc] init];
NSString *outString = [[NSString 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{
outString = @"";
}
}
if (cipherBuffer) {
free(cipherBuffer);
}
outString = [encryptedData base64Encoding];
}
return outString;
NSString *password = [paramsDict stringValueForKey:@"password" defaultValue:@""];
NSString *inString = [paramsDict stringValueForKey:@"data" defaultValue:@""];
NSString *value = @"";
if (inString.length <= ) {
//err:1
return value;
} else {
// 从私钥证书文件中获取到公钥的SecKeyRef指针: @"private_key" ofType:@"pem"
NSString *privateKeyPath = [paramsDict stringValueForKey:@"privateKey" defaultValue:@""];
privateKeyPath = [self getPathWithUZSchemeURL:privateKeyPath];
if (![[NSFileManager defaultManager] fileExistsAtPath:privateKeyPath]) {
return value;
}
NSData *p12Data = [[NSData alloc]initWithContentsOfFile:privateKeyPath];
SecKeyRef privateKeyRef = NULL;
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setObject:password forKey:(__bridge id)kSecImportExportPassphrase];
CFArrayRef items = CFArrayCreate(NULL, , , NULL);
OSStatus securityError = SecPKCS12Import((__bridge CFDataRef) p12Data, (__bridge CFDictionaryRef)options, &items);
if (securityError == noErr && CFArrayGetCount(items) > ) {
CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, );
SecIdentityRef identityApp = (SecIdentityRef)CFDictionaryGetValue(identityDict, kSecImportItemIdentity);
securityError = SecIdentityCopyPrivateKey(identityApp, &privateKeyRef);
if (securityError != noErr) {
privateKeyRef = NULL;
}
} else {
return value;
}
CFRelease(items); NSData *cipherData = [NSData dataWithBase64EncodedString:inString];
// NSData* decryptData = [self rsaDecryptData: data];
size_t cipherLen = [cipherData length];
void *cipher = malloc(cipherLen);
[cipherData getBytes:cipher length:cipherLen];
size_t plainLen = SecKeyGetBlockSize(privateKeyRef) - ;
void *plain = malloc(plainLen);
OSStatus status = SecKeyDecrypt(privateKeyRef, kSecPaddingPKCS1, cipher, cipherLen, plain, &plainLen);
if (status != noErr) {
return value;
}
NSData *decryptedData = [[NSData alloc] initWithBytes:(const void *)plain length:plainLen];
value = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
NSMutableString *outString = [[NSMutableString alloc] init];
//如果没有进行加密直接解密,outString 将为 nil
if (!value && decryptedData && decryptedData.length > ) {
Byte *datas = (Byte*)[decryptedData bytes];
outString = [NSMutableString stringWithCapacity:decryptedData.length * ];
for(int i = ; i < decryptedData.length; i++){
[outString appendFormat:@"%02x", datas[i]];
}
value = [outString copy];
}
return value;
}

通过ios实现RSA加密和解密的更多相关文章

  1. Cryptopp iOS 使用 RSA加密解密和签名验证签名

    Cryptopp 是一个c++写的功能完善的密码学工具,类似于openssl 官网:https://www.cryptopp.com 以下主要演示Cryptopp 在iOS上的RSA加密解密签名与验证 ...

  2. IOS, Android, Java Web Rest : RSA 加密和解密问题

    IOS, Android, Java Web Rest :  RSA 加密和解密问题 一对公钥私钥可以使用 OpenSSL创建, 通常 1024位长度够了. 注意: 1. 公钥私钥是BASE64编码的 ...

  3. C#实现RSA加密和解密详解

    原文:C#实现RSA加密和解密详解 RSA加密解密源码: Code highlighting produced by Actipro CodeHighlighter (freeware) http:/ ...

  4. ASP.NET Core RSA加密或解密

    前言 这两天主要是公司同事用到了RSA加密,事后也看了下,以为很简单,最终利用RSACryptoServiceProvider来实现RSA加密,然后大致了解到RSACryptoServiceProvi ...

  5. C#实现RSA加密与解密、签名与认证(转)

    一.RSA简介 RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力 ...

  6. RSA加密和解密工具类

    import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import java.security.*; i ...

  7. C#实现RSA加密与解密、签名与认证

    一.RSA简介 RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力 ...

  8. C# -- RSA加密与解密

    1.  RSA加密与解密  --  使用公钥加密.私钥解密 public class RSATool { public string Encrypt(string strText, string st ...

  9. python RSA加密、解密、签名

    python RSA加密.解密.签名 python中用于RSA加解密的库有好久个,本文主要讲解rsa.M2Crypto.Crypto这三个库对于RSA加密.解密.签名.验签的知识点. 知识基础 加密是 ...

随机推荐

  1. python使用uuid库生成唯一id

    概述: UUID是128位的全局唯一标识符,通常由32字节的字符串表示. 它可以保证时间和空间的唯一性,也称为GUID,全称为: UUID -- Universally Unique IDentifi ...

  2. java-collections.sort异常Comparison method violates its general contract!

    转载:http://www.tuicool.com/articles/MZreyuv 异常信息 java.lang.IllegalArgumentException: Comparison metho ...

  3. 怎么样修改小猪cms(从功能库添加)模块关键字

    需求:修改或者添加从功能库添加中的关键字 这里以添加咨询投诉为列: 找到wwwroot\PigCms\Lib\Action\User目录下的LinkAction.class.php文件(手动找不到直接 ...

  4. bzoj 2738 矩阵乘法

    其实这题跟矩阵乘法没有任何卵关系,直接整体二分,用二维树状数组维护(刚刚学会>_<),复杂度好像有点爆炸(好像有十几亿不知道是不是算错了),但我们不能怂啊23333. #include&l ...

  5. iOS - 适配器模式场景总结

    适配器模式: 1.出现场景 TableViewCell视图层 - 输出 无论NSData怎么变化,数据都可以显示在视图层中. 2.组成结构 输入 - [适配器]- 输出 (数据层) - [适配器]- ...

  6. 介绍对称加密算法,最常用的莫过于DES数据加密算法

    DES DES-Data Encryption Standard,即数据加密算法.是IBM公司于1975年研究成功并公开发表的.DES算法的入口参数有三个:Key.Data.Mode.其中Key为8个 ...

  7. 网络基础知识之 Ping

    ========================================假定主机A的IP地址是192.168.1.1,主机B的IP地址是192.168.1.2,都在同一子网内,则当你在主机A上 ...

  8. ubuntu下安装gedit插件

    因为gedit-plugins : 依赖: gir1.2-zeitgeist-2.0 所以首先 sudo apt-get install gir1.2-zeitgeist-2.0 如果报错可以先 su ...

  9. /usr/bin/cd 是什么鬼

    上文中曾讲到,我在我的 Mac 上发现很多和 Bash 内部命令同名的外部命令,在那 24 个外部命令中,我发现个奇怪的现象:它们中有 15 个居然是 Shell 脚本,更奇怪的是,居然是同一个 Sh ...

  10. 利用Render Texture实现游戏的小雷达效果(摄影机分屏)

    最近游戏蛮牛在举办一个活动,就是要做出这样的效果: 题目:实现游戏分屏效果 要求:1.        分屏,且分割线不规则(即非水平或垂直):2.        各屏可单独操作(移动.缩放),操作指该 ...