#import "AppDelegate.h"
#import "GTMBase64.h"
#import <Security/Security.h>
#import <CommonCrypto/CommonCrypto.h>
@interface AppDelegate ()
@end
#define Mykey @"你和后台约定好的key"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 要解密的字符串 举例
NSString *str1 = @"*iTR33oAAylAUqEiuq6+q+3+qZgU1Apo7gOxx0EHn+hrQlpTulOviNQ==#";
//要加密的字符串 举例
NSString *str2 = @"11111";
//去除前后符号
NSString *encryptStr = [str1 substringWithRange:NSMakeRange(1, str1.length-2)];
//decrypted 解密 encrypted 加密
NSString *resultStr = [self decryptWithText:encryptStr andKey:Mykey];
NSString *resultStr2 = [self encryptWithText:str2 andKey:Mykey];
NSLog(@"resultStr--->%@",resultStr);
NSLog(@"resultStr2-->%@",resultStr2);
return YES;
}
- (NSString *)encryptWithText:(NSString *)sText andKey:(NSString*)key
{
//kCCEncrypt 加密
return [self encrypt:sText encryptOrDecrypt:kCCEncrypt key:key];
}
- (NSString *)decryptWithText:(NSString *)sText andKey:(NSString*)key
{
//kCCDecrypt 解密
return [self encrypt:sText encryptOrDecrypt:kCCDecrypt key:key];
}
-(NSString *)encrypt:(NSString *)sText encryptOrDecrypt:(CCOperation)encryptOperation key:(NSString *)key
{
const void *dataIn;
size_t dataInLength;
if (encryptOperation == kCCDecrypt)//传递过来的是decrypt 解码
{
//解码 base64
NSData *decryptData = [GTMBase64 decodeData:[sText dataUsingEncoding:NSUTF8StringEncoding]];//转成utf-8并decode
dataInLength = [decryptData length];
dataIn = [decryptData bytes];
}
else //encrypt
{
NSData* encryptData = [sText dataUsingEncoding:NSUTF8StringEncoding];
dataInLength = [encryptData length];
dataIn = (const void *)[encryptData bytes];
}
/*
DES加密 :用CCCrypt函数加密一下,然后用base64编码下,传过去
DES解密 :把收到的数据根据base64,decode一下,然后再用CCCrypt函数解密,得到原本的数据
*/
CCCryptorStatus ccStatus;
uint8_t *dataOut = NULL; //可以理解位type/typedef 的缩写(有效的维护了代码,比如:一个人用int,一个人用long。最好用typedef来定义)
size_t dataOutAvailable = 0; //size_t 是操作符sizeof返回的结果类型
size_t dataOutMoved = 0;
dataOutAvailable = (dataInLength + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
dataOut = malloc( dataOutAvailable * sizeof(uint8_t));
memset((void *)dataOut, 0x0, dataOutAvailable);//将已开辟内存空间buffer的首 1 个字节的值设为值 0
// NSString *initIv = @"12345678";
const void *vkey = (const void *) [key UTF8String];
// const void *iv = (const void *) [initIv UTF8String];
//CCCrypt函数 加密/解密
ccStatus = CCCrypt(encryptOperation,// 加密/解密
kCCAlgorithm3DES,// 加密根据哪个标准(des,3des,aes。。。。)
kCCOptionPKCS7Padding|kCCOptionECBMode,// 选项分组密码算法(des:对每块分组加一次密 3DES:对每块分组加三个不同的密)
vkey, //密钥 加密和解密的密钥必须一致
kCCKeySize3DES,// DES 密钥的大小(kCCKeySizeDES=8)
nil, // 可选的初始矢量
dataIn, // 数据的存储单元
dataInLength,// 数据的大小
(void *)dataOut,// 用于返回数据
dataOutAvailable,
&dataOutMoved);
NSString *result = nil;
if (encryptOperation == kCCDecrypt)//encryptOperation==1 解码
{
//得到解密出来的data数据,改变为utf-8的字符串
result = [[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)dataOut length:(NSUInteger)dataOutMoved] encoding:NSUTF8StringEncoding];
}
else //encryptOperation==0 (加密过程中,把加好密的数据转成base64的)
{
//编码 base64
NSData *data = [NSData dataWithBytes:(const void *)dataOut length:(NSUInteger)dataOutMoved];
result = [GTMBase64 stringByEncodingData:data];
}
return result;
}