UI-Senior网络安全-RSA公钥加密实例

1. 首先,我们要引入RSA加密的工具类

RSA工具类: http://pan.baidu.com/s/1qYRC3wS
提取密码: 293j

2. 然后,实现加密

ViewController.m文件

//网络进阶RSA
//  Copyright © 2016年 xiaojie. All rights reserved.
#import “ViewController.h”
//引入工具类头文件
#import “RSA.h”

@interfaceViewController ()
@end
@implementation ViewController
– (void)viewDidLoad {
[superviewDidLoad];
//公钥和私钥都是由加密证书生成的!
//公钥,用于加密数据. 用于公开, 一般存放在数据提供方, 例如iOS客户端.
NSString *pubkey = @”—–BEGIN PUBLIC KEY—–\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEChqe80lJLTTkJD3X3Lyd7Fj+\nzuOhDZkjuLNPog3YR20e5JcrdqI9IFzNbACY/GQVhbnbvBqYgyql8DfPCGXpn0+X\nNSxELIUw9Vh32QuhGNr3/TBpechrVeVpFPLwyaYNEk1CawgHCeQqf5uaqiaoBDOT\nqeox88Lc1ld7MsfggQIDAQAB\n—–END PUBLIC KEY—–“;
//私钥,用于解密数据. 必须保密, 私钥泄露会造成安全问题.
NSString *privkey = @”—–BEGIN RSA PRIVATE KEY—–\nMIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMQKGp7zSUktNOQk\nPdfcvJ3sWP7O46ENmSO4s0+iDdhHbR7klyt2oj0gXM1sAJj8ZBWFudu8GpiDKqXw\nN88IZemfT5c1LEQshTD1WHfZC6EY2vf9MGl5yGtV5WkU8vDJpg0STUJrCAcJ5Cp/\nm5qqJqgEM5Op6jHzwtzWV3syx+CBAgMBAAECgYEApSzqPzE3d3uqi+tpXB71oY5J\ncfB55PIjLPDrzFX7mlacP6JVKN7dVemVp9OvMTe/UE8LSXRVaFlkLsqXC07FJjhu\nwFXHPdnUf5sanLLdnzt3Mc8vMgUamGJl+er0wdzxM1kPTh0Tmq+DSlu5TlopAHd5\nIqF3DYiORIen3xIwp0ECQQDj6GFaXWzWAu5oUq6j1msTRV3mRZnx8Amxt1ssYM0+\nJLf6QYmpkGFqiQOhHkMgVUwRFqJC8A9EVR1eqabcBXbpAkEA3DQfLVr94vsIWL6+\nVrFcPJW9Xk28CNY6Xnvkin815o2Q0JUHIIIod1eVKCiYDUzZAYAsW0gefJ49sJ4Y\niRJN2QJAKuxeQX2s/NWKfz1rRNIiUnvTBoZ/SvCxcrYcxsvoe9bAi7KCMdxObJkn\nhNXFQLav39wKbV73ESCSqnx7P58L2QJABmhR2+0A5EDvvj1WpokkqPKmfv7+ELfD\nHQq33LvU4q+N3jPn8C85ZDedNHzx57kru1pyb/mKQZANNX10M1DgCQJBAMKn0lEx\nQH2GrkjeWgGVpPZkp0YC+ztNjaUMJmY5g0INUlDgqTWFNftxe8ROvt7JtUvlgtKC\nXdXQrKaEnpebeUQ=\n—–END RSA PRIVATE KEY—–“;
//初始化一个要加密的字符串
NSString *string = @”魍魉”;
//创建一个字符串,用来存放公钥加密的结果
NSString *encPubKey;
//创建一个字符串,用来存放私钥加密的结果
NSString *decPrivKey;
//使用RSA进行加密
//参数一: 要加密的数据
//参数二: 公钥
//RSA加密之后得到的每一次结果都不一样, 但是, 都可以通过同一个私钥进行解密
encPubKey = [RSAencryptString:string publicKey:pubkey];
NSLog(@”encPubKey = %@”, encPubKey);
//利用私钥进行解密
decPrivKey = [RSAdecryptString:encPubKey privateKey:privkey]
;
NSLog(@”decPubkey = %@”, decPrivKey);
}
– (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end```

你可能感兴趣的:(UI-Senior网络安全-RSA公钥加密实例)