iOS之那幾種加密解密 公鑰密鑰加密解密

哈哈哈哈😆 ------------- 大哥原創數據庫

請多多指教 .....................................................服務器


 

//
//  ViewController.m
//  公鑰密鑰加密解密
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "ViewController.h"
#import "RSA.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    
    /*
     
     //公鑰祕鑰加密解密:
     //非對稱加密解密(公鑰,加密後,只有密鑰能解),經過公鑰進行加密,祕鑰進行解密.
     //一般,公鑰放在客戶端, 祕鑰存放在數據庫. **密鑰必需要保密**,不能泄露.
     //一般用於支付,如:微信,QQ,支付寶,銀聯..都使用非對稱加密,解密方式.
     
     */

    
    //公鑰字符串
    NSString *publicKey = @"-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEChqe80lJLTTkJD3X3Lyd7Fj+\nzuOhDZkjuLNPog3YR20e5JcrdqI9IFzNbACY/GQVhbnbvBqYgyql8DfPCGXpn0+X\nNSxELIUw9Vh32QuhGNr3/TBpechrVeVpFPLwyaYNEk1CawgHCeQqf5uaqiaoBDOT\nqeox88Lc1ld7MsfggQIDAQAB\n-----END PUBLIC KEY-----";
    //公鑰加密
    NSString *string = @"海騰"; /*要加密的東西*/
    //加密
    NSString *encryStr = [RSA encryptString:string publicKey:publicKey];
    NSLog(@"%@",encryStr); /*打印看看,加密後的字符串*/
    
    //私鑰解密
    //私鑰字符串
    NSString *privateKey = @"-----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 *decryStr = [RSA decryptString:encryStr privateKey:privateKey];
    NSLog(@"%@",decryStr); /* 打印看看 */
    
    
    
    
    //字典. 進行加密解密
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"James",@"name", nil];
    //轉化爲NSData(NSData萬能轉化)
    NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    //公鑰加密
    NSData *encryData = [RSA encryptData:data publicKey:publicKey];
    NSLog(@"%@",encryData);  /*看下加密後的Data也就是字典*/
    
    //服務器
    //私鑰解密
    NSData *dencryData = [RSA decryptData:encryData privateKey:privateKey];
    //Data -> 字典
    NSDictionary *dic1 = [NSJSONSerialization JSONObjectWithData:dencryData options:0 error:nil];
    NSLog(@"%@",dic1); /*打印看看解密後的字典*/
    
    
    
    //練習,解密,服務器加密返回的字符串,全部這裏用公鑰進行解密
  //  服務器用私鑰已經加過密的簽名
    NSString *encryptStr = @"F+feHOH6807tUV/drvepAMzKlVKRsoDFRkFNfhS9kgVoBt2E6OnUIVw12l608yQGWiqtq8rgZgMY/VCQYZB+3r2rhDlyZ2fjo00sUFOp5BkNPFTs/NpQAolD6V3ifNgDmBQP92uVbxbod1pLRwLC0wLOhr5flQXvvobeg5KrDeE=";
    NSString *a = [RSA decryptString:encryptStr publicKey:publicKey];
    NSLog(@"------ %@",a);

}
相關文章
相關標籤/搜索