框架从 CryptoExercise(苹果3.0时的包)进行提取扩展 iOS 系统自带相关函数说明,框架主要使用前两种: SecKeyEncrypt 使用公钥对数据加密 SecKeyDecrypt 使用私钥对数据解密 SecKeyRawVerify 使用公钥对数字签名进行验证 SecKeyRawSign 使用私钥生成数字签名
RSA算法原理:
XHCryptorTools *tools = [[XHCryptorTools alloc] init];
// 1. 加载公钥
NSString *pubPath = [[NSBundle mainBundle] pathForResource:@"rsacert.der" ofType:nil];
[tools loadPublicKeyWithFilePath:pubPath];
// 2. 使用公钥加密,加密内容最大长度 117
NSString *result = [tools RSAEncryptString:@"abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghi"];
NSLog(@"RSA 加密 %@", result);
// 3. 加载私钥,并指定导出 p12 时设置的密码
NSString *privatePath = [[NSBundle mainBundle] pathForResource:@"p.p12" ofType:nil];
[tools loadPrivateKey:privatePath password:@"123"];
// 4. 使用私钥解密
NSLog(@"解密结果 %@", [tools RSADecryptString:result]);
公钥:就是签名机构签完给我们颁发的,放在网站的根目录上,可以分发 私钥:一般保存在中心服务器
openssl genrsa -out private.pem 512
openssl req -new -key private.pem -out rsacert.csr
终端提示如下:
生成证书请求界面
openssl x509 -req -days 3650 -in rsacert.csr -signkey private.pem -out rsacert.crt
由于iOS开发时使用的时候不能是base64的,必须解成二进制文件!openssl x509 -outform der -in rsacert.crt -out rsacert.der
openssl pkcs12 -export -out p.p12 -inkey private.pem -in rsacert.crt
输入一个导出密码(框架中loadPrivateKey:方法的password参数需要用的密码):
输入导出密码界面.png
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有