#import "ViewController.h"
#import "RSA.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//公钥,用于加密数据. 用于公开, 一般存放在数据提供方, 例如iOS客户端.
NSString *pubkey = @"-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEChqe80lJLTTkJD3X3Lyd7Fj+
zuOhDZkjuLNPog3YR20e5JcrdqI9IFzNbACY/GQVhbnbvBqYgyql8DfPCGXpn0+X
NSxELIUw9Vh32QuhGNr3/TBpechrVeVpFPLwyaYNEk1CawgHCeQqf5uaqiaoBDOT
qeox88Lc1ld7MsfggQIDAQAB
-----END PUBLIC KEY-----";
//私钥,用于解密数据. 必须保密, 私钥泄露会造成安全问题.
NSString *privkey = @"-----BEGIN RSA PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMQKGp7zSUktNOQk
PdfcvJ3sWP7O46ENmSO4s0+iDdhHbR7klyt2oj0gXM1sAJj8ZBWFudu8GpiDKqXw
N88IZemfT5c1LEQshTD1WHfZC6EY2vf9MGl5yGtV5WkU8vDJpg0STUJrCAcJ5Cp/
m5qqJqgEM5Op6jHzwtzWV3syx+CBAgMBAAECgYEApSzqPzE3d3uqi+tpXB71oY5J
cfB55PIjLPDrzFX7mlacP6JVKN7dVemVp9OvMTe/UE8LSXRVaFlkLsqXC07FJjhu
wFXHPdnUf5sanLLdnzt3Mc8vMgUamGJl+er0wdzxM1kPTh0Tmq+DSlu5TlopAHd5
IqF3DYiORIen3xIwp0ECQQDj6GFaXWzWAu5oUq6j1msTRV3mRZnx8Amxt1ssYM0+
JLf6QYmpkGFqiQOhHkMgVUwRFqJC8A9EVR1eqabcBXbpAkEA3DQfLVr94vsIWL6+
VrFcPJW9Xk28CNY6Xnvkin815o2Q0JUHIIIod1eVKCiYDUzZAYAsW0gefJ49sJ4Y
iRJN2QJAKuxeQX2s/NWKfz1rRNIiUnvTBoZ/SvCxcrYcxsvoe9bAi7KCMdxObJkn
hNXFQLav39wKbV73ESCSqnx7P58L2QJABmhR2+0A5EDvvj1WpokkqPKmfv7+ELfD
HQq33LvU4q+N3jPn8C85ZDedNHzx57kru1pyb/mKQZANNX10M1DgCQJBAMKn0lEx
QH2GrkjeWgGVpPZkp0YC+ztNjaUMJmY5g0INUlDgqTWFNftxe8ROvt7JtUvlgtKC
XdXQrKaEnpebeUQ=
-----END RSA PRIVATE KEY-----";
NSString *string = @"王梁";
NSString *encPubkey;// 公钥加密的结果
NSString *decPrivkey;// 私钥解密的结果
//使用RSA进行加密
//第一个参数: 要加密的数据
//第二个参数: 公钥
//RSA 加密之后得到的结果 每一次都不一样 但是都可以通过同一个私钥进行解密
encPubkey = [RSA encryptString:string publicKey:pubkey];
NSLog(@"encPubKey == %@", encPubkey);
decPrivkey = [RSA decryptString:encPubkey privateKey:privkey];
NSLog(@"decPrickey == %@", decPrivkey);
NSString *resultString = @"DJR22zZVGcyQ6m9J7D1j/VA/xv6ZhhssIzs1n1I/vCOFnKwx6rEhhgfE9ReT3yNLPvkkxKph4WNSxJMDCOWKrrWxKpH7Aod6eJbmkZYnNOGfwrf+2XwVfKMCn1CmvuVmqWw1k/21Rj4PB92qHEf+E519kF4rYkg72tb+YZ5487I=";
decPrivkey = [RSA decryptString:resultString privateKey:privkey];
NSLog(@"decPrickey == %@", decPrivkey);
}