天天看點

ios android rsa加密解密,IOS RSA加密解密

最近項目中對接口進行了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

16進制公鑰字元串用于js端加密

最終擷取檔案清單:

ios android rsa加密解密,IOS RSA加密解密

767FB37B-CB31-474F-8EF1-0671BCFC7F58.png

使用方法 :

1、直接使用公鑰私鑰字元串

ios android rsa加密解密,IOS RSA加密解密

B073F8B2-0528-4F1C-AF4E-C6F1ACBD3967.png

直接改字尾就行

ios android rsa加密解密,IOS RSA加密解密

030B492E-8A5F-4BA8-BC96-E18D611DFFDC.png

直接使用字元串 必須去掉 -----BEGIN PRIVATE KEY----- -----END PRIVATE KEY-----

加密:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{      

encryString = [RSAUtil encryptString:@"wd" publicKey:@"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnirveHF6Fur922p+nFrVaiiAd4EmYcugZ59gWLMsMMpZkvxe/cV74phvYFVXdRNPbFooZt7F0pjEYKNlSVMJfEUJYMXN0WEolh0lWYlpHNsB1IysFheZbtwGiqo+lKgFVS9BCJSs7be7T22LvxLlcXrvoCKq4ZlzwcTTCqdrbNQIDAQAB"];

NSLog(@"%@",encryString);

}

解密:

- (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);

}

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

#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 *)touches withEvent:(UIEvent *)event{

encryString = [RSAUtil encryptString:@"wd" publicKey:[RSAUtil getKeyWithfileName:@"rsa_public_key" type:@"crt"]];

NSLog(@"%@",encryString);

}

如果沒有輸出結果請檢查一下這個地方

ios android rsa加密解密,IOS RSA加密解密

BD704688-A9DB-458A-8D08-71CB8440BBCE.png

點選demo下載下傳