一、MD5加密
//声明一个字符串
NSString *name = @"linzhisheng1";
//获取c的字符串
const char *nameStr = [name UTF8String];
//创建字符串数组接收MD5值
unsigned char result[CC_MD5_DIGEST_LENGTH];
//计算MD5值(结果储存在result数组中)
CC_MD5(nameStr, (CC_LONG)strlen(nameStr), result);
//获取MD5加密后的值
NSMutableString *bar = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[bar appendFormat:@"%02X",result[i]];
}
NSLog(@"bar------>%@",bar);
二、Base64加密(事先需要导入第三方加密文件Base64)
NSString *password = @"6666";
NSString *resultPass = [Base64 encodeString:password];
NSLog(@"resultPass--->%@",resultPass);
NSString *getPass = [Base64 decodeString:resultPass];
NSLog(@"getPass-->%@",getPass);
三、公钥私钥加密(事先需要导入第三方加密文件RAS)
NSString *secure = @"peanut";
NSString *pubKey = @"-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEChqe80lJLTTkJD3X3Lyd7Fj+\nzuOhDZkjuLNPog3YR20e5JcrdqI9IFzNbACY/GQVhbnbvBqYgyql8DfPCGXpn0+X\nNSxELIUw9Vh32QuhGNr3/TBpechrVeVpFPLwyaYNEk1CawgHCeQqf5uaqiaoBDOT\nqeox88Lc1ld7MsfggQIDAQAB\n-----END PUBLIC KEY-----";
NSString *encWithPubKey = [RSA encryptString:secure publicKey:pubKey];
NSLog(@"encWithPubKey--->%@",encWithPubKey);
NSString *priKey = @"-----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 *decodePrivatyKeyStr = [RSA decryptString:encWithPubKey privateKey:priKey];
四、钥匙串加密(事先需要导入第三方加密文件KeychainItemWrapper)
//创建并初始化
KeychainItemWrapper * wrapper = [[KeychainItemWrapper alloc]
initWithIdentifier:@"MyKeychainItem"
accessGroup:nil];
//系统提供的键值对中的两个键,非系统的键是没法添加到字典中的
id kUsernameKey = (__bridge id)kSecAttrAccount;
id kPasswordKey = (__bridge id)kSecValueData;
//存值
[wrapper setObject:@"user" forKey:kUsernameKey];
[wrapper setObject:@"123" forKey:kPasswordKey];
//通过相同 的标记创建的钥匙串中具有相同的数据,可以看做是一个对象
KeychainItemWrapper *
wrapper2 = [[KeychainItemWrapper alloc]
initWithIdentifier:@"MyKeychainItem"
accessGroup:nil];
//取值
NSString *username = [wrapper2 objectForKey:kUsernameKey];
/*
1、md5只能加密不能解密(客户端知道,服务器不知道)
2、Base64 能加密也能解密 算法是公开的(客户端知道,服务器也知道)
3、钥匙串 数据完整性的安全 卸载App,重新安装还能找到用户名和密码
4、公钥加密 私钥解密(只有内部人员知道,客户端知道,服务器也知道)
*/
网友评论