+ (NSString*)encodeBase64Data:(NSData *)data { data = [GTMBase64 encodeData:data]; NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; return base64String; } + (NSData*)decodeBase64String:(NSString * )input { NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; data = [GTMBase64 decodeData:data]; return data; }
AES128使用系統CommonCrypto/CommonCryptor.h實現 //用於AES
添加NSData分類,增長兩個方法web
#pragma mark - AES128位加解密 - (NSData *)AES128EncryptWithKey:(NSString *)key { char keyPtr[kCCKeySizeAES128 + 1]; memset(keyPtr, 0, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES, kCCOptionPKCS7Padding|kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); //free the buffer; return nil; } - (NSData *)AES128DecryptWithKey:(NSString *)key { // 'key' should be 32 bytes for AES256, will be null-padded otherwise char keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused) bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) // fetch key data [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; //See the doc: For block ciphers, the output size will always be less than or //equal to the input size plus the size of one block. //That's why we need to add the size of one block here size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding|kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesDecrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; } free(buffer); //free the buffer; return nil; }
- (AFHTTPRequestOperation *)POST:(NSString *)URLString parameters:(id)parameters constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
NSMutableDictionary *para = [NSMutableDictionary dictionary]; para[@"method"] = @"securityAdd"; para[@"userId"] = userId; para[@"userPsw"] = userPsw; para[@"content"] = @"ddddd123891237"; NSString *url = [NSString stringWithFormat:@"https://%@:82/frame/webInteface.do?", NHBaseURL]; AFHTTPRequestOperation *operation = [NetWorkInst POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { } success:^(AFHTTPRequestOperation *operation, id responseObject) { }];
NSMutableDictionary *para = [NSMutableDictionary dictionary]; para[@"method"] = @"securityAdd"; para[@"userId"] = userId; para[@"userPsw"] = userPsw; para[@"content"] = @"ddddd123891237"; // 開始加密,格式化數據**************************** NSString *str = [NSString stringWithFormat:@"'method':'securityAdd','userId':'%@','userPsw':'%@','content':'%@'",userId,userPsw,content]; NSLog(@"原始數據:%@",str); NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; NSData *aaa = [data AES128EncryptWithKey:@"song.com"]; // aes加密 NSLog(@"加密AES128後:%@",aaa); NSString *bbb = [PublicMethod encodeBase64Data:aaa];//base64加密 NSLog(@"base64加密後:%@",bbb); NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[@"info"] = bbb; // 開始解密**************************** NSData *da = [PublicMethod decodeBase64String:bbb]; //base64解密 NSString *ccc = [[NSString alloc] initWithData:da encoding:NSUTF8StringEncoding]; NSLog(@"base64解密後:%@",ccc); NSData *ddd = [da AES128DecryptWithKey:@"song.com"];// aes解密 NSString *eee = [[NSString alloc] initWithData:ddd encoding:NSUTF8StringEncoding]; NSLog(@"解密AES128後:%@",eee); NSString *url = [NSString stringWithFormat:@"https://%@:82/frame/webInteface.do?", NHBaseURL]; AFHTTPRequestOperation *operation = [NetWorkInst POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { } success:^(AFHTTPRequestOperation *operation, id responseObject) { }];