最近项目中对接口进行了rsa 加密。 写下过程以便复习
公钥私钥一般是有后台给的,这里为了方便 自己生成一对秘钥
生成公钥 私钥。
终端中:
-
生成原始 RSA私钥文件 private_key.pem
openssl genrsa -out private_key.pem 1024
-
将原始 RSA私钥转换为 pkcs8格式
openssl pkcs8 -topk8 -inform PEM -in private_key.pem -outform PEM -nocrypt -out rsa_private_key.pem
-
生成 RSA公钥 rsa_public_key.pem
openssl rsa -in private_key.pem -pubout -out rsa_public_key.crt
-
从公钥 rsa_public_key.pem 获取十六进制的公钥(第一段16进制字符串)
openssl asn1parse -out temp.ans -i -inform PEM<private_key.pem
16进制公钥字符串用于js端加密
最终获取文件列表:
使用方法 :
1、直接使用公钥私钥字符串
直接改后缀就行
直接使用字符串 必须去掉 -----BEGIN PRIVATE KEY----- -----END PRIVATE KEY-----
加密:
<pre>- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
encryString = [RSAUtil encryptString:@"wd" publicKey:@"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnirveHF6Fur922p+nFrVaiiAd4EmYcugZ59gWLMsMMpZkvxe/cV74phvYFVXdRNPbFooZt7F0pjEYKNlSVMJfEUJYMXN0WEolh0lWYlpHNsB1IysFheZbtwGiqo+lKgFVS9BCJSs7be7T22LvxLlcXrvoCKq4ZlzwcTTCqdrbNQIDAQAB"];
NSLog(@"%@",encryString);
}
</pre>
解密:
<pre>- (void)buttonClick{
NSString * decryString = [RSAUtil decryptString:encryString privateKey:@"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKeKu94cXoW6v3ban6cWtVqKIB3gSZhy6Bnn2BYsywwylmS/F79xXvimG9gVVd1E09sWihm3sXSmMRgo2VJUwl8RQlgxc3RYSiWHSVZiWkc2wHUjKwWF5lu3AaKqj6UqAVVL0EIlKztt7tPbYu/EuVxeu+gIqrhmXPBxNMKp2ts1AgMBAAECgYEAlcriAMlYcTPwLvF0QE17Q0GvSUbYReamQVMZzWmq+HLaJ6T6NH3GLYVF68HXjQcWaxER08B6dhKIXlNXSdbiF+5lGuSb5gqQ92FlcRhHq/pw3maMg1g0Ve56Ej5g1scBWiOTFB4YsSTGjaflgtdQn617zyBXSjgr8BByNTyoHtkCQQDZ8+Mg3evM/4CilovTvP4JHWOzdN74vRM2HE8o9lyrV1SYi6uo1bIT96q/4eM5XFVlnSun7Zaeq+r6zNqSv6LvAkEAxMoI2D8Dv82aKVCpB70Qr5zKXP2ThWT9cs0eV3n19tIxZwFXPjGpRiYXTBdv6RB369lhAMH6qXT9VZxRr7YUGwJADupe38/wJC5UGkxdM1datzr4qL5VyAYAe5KCZFY/z0YBtxLP45UEQ9OOLuWBjFpiSbrw0uQyi5dOVG2Sg7pzpwJAb+9zxH1jhpBOpj2JSCuU+C85NFfQt8nQGKLwJVXsQK8jBrJOi8P3wz0bd17Fe06wx+joMmQkTouLJk06cbYiDQJBAIL+CJsCr3qeEI7GcTsH+xzz5y6O2jrie2H7kiXKzOn+84atkuelnP5lB/FSlbKxpKD163Fn/UQDzKdAfezZb9s="];
NSLog(@"-----%@",decryString);
}</pre>
2、读取文件
直接读取文件时 -----BEGIN PRIVATE KEY----- -----END PRIVATE KEY-----这一个不能删掉
控制台输出:2016-09-27 14:01:42.270 RSADemo[7987:1724593] Bq+5fQisxoFeW80nY5BLZdIOpn15bfV9G2bI8ps+0O+0RuYpVXEwwwKit1/MzAAY0X6krQf8P3qFWtAgROvi7OtK7m8NtdlWAQ9DTea6pe27Ieh3oZ2VfiIVhtBw7T3vwmoowbnV892Rey8OUW94tz8tR0Cwmtxxf5wDSdjolsg=
2016-09-27 14:01:43.863 RSADemo[7987:1724593] -----wd
<pre>#import "RSAUtil.h"
@interface ViewController (){
NSString * encryString;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClick{
NSString * decryString = [RSAUtil decryptString:encryString privateKey:[RSAUtil getKeyWithfileName:@"rsa_private_key" type:@"pem"]];
NSLog(@"-----%@",decryString);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
encryString = [RSAUtil encryptString:@"wd" publicKey:[RSAUtil getKeyWithfileName:@"rsa_public_key" type:@"crt"]];
NSLog(@"%@",encryString);
}</pre>
如果没有输出结果请检查一下这个地方
点击demo下载
[参考链接]:http://blog.csdn.net/thor027/article/details/52074469