iOS AES的加密解密
Posted clnchanpin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS AES的加密解密相关的知识,希望对你有一定的参考价值。
主要是要得到加密后的原来的字符创,MD5无法得到原有的,仅仅能用AES+base64
要用GTMBase64这个能够自己去下载,
主要看另外2个类文件。
#import <Foundation/Foundation.h> @class NSString; @interface NSData (NSData_AES) - (NSData *)AES128EncryptWithKey:(NSString *)key; //加密 - (NSData *)AES128DecryptWithKey:(NSString *)key; //解密 @end
#import "NSData+NSData_AES.h" #import <CommonCrypto/CommonCryptor.h> #define gIv @"xxxxxxxxxxx" //能够自行定义16位 @implementation NSData (NSData_AES) //(key和iv向量这里是16位的) 这里是CBC加密模式,安全性更高 - (NSData *)AES128EncryptWithKey:(NSString *)key//加密 { char keyPtr[kCCKeySizeAES128+1]; bzero(keyPtr, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; char ivPtr[kCCKeySizeAES128+1]; memset(ivPtr, 0, sizeof(ivPtr)); [gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCBlockSizeAES128, ivPtr, [self bytes], dataLength, buffer, bufferSize, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); return nil; } - (NSData *)AES128DecryptWithKey:(NSString *)key//解密 { char keyPtr[kCCKeySizeAES128+1]; bzero(keyPtr, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; char ivPtr[kCCKeySizeAES128+1]; memset(ivPtr, 0, sizeof(ivPtr)); [gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCBlockSizeAES128, ivPtr, [self bytes], dataLength, buffer, bufferSize, &numBytesDecrypted); if (cryptStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; } free(buffer); return nil; } @end
#import <Foundation/Foundation.h> @interface SecurityUtil : NSObject #pragma mark - base64 + (NSString*)encodeBase64String:(NSString *)input; + (NSString*)decodeBase64String:(NSString *)input; + (NSString*)encodeBase64Data:(NSData *)data; + (NSString*)decodeBase64Data:(NSData *)data; #pragma mark - AES加密 //将string转成带password的data + (NSString*)encryptAESData:(NSString*)string app_key:(NSString*)key ; //将带password的data转成string +(NSString*)decryptAESData:(NSData*)data app_key:(NSString*)key ; @end
#import "SecurityUtil.h" #import "GTMBase64.h" #import "NSData+AES.h" @implementation SecurityUtil #pragma mark - base64 + (NSString*)encodeBase64String:(NSString * )input { NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; data = [GTMBase64 encodeData:data]; NSString *base64String = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; return base64String; } + (NSString*)decodeBase64String:(NSString * )input { NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; data = [GTMBase64 decodeData:data]; NSString *base64String = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; return base64String; } + (NSString*)encodeBase64Data:(NSData *)data { data = [GTMBase64 encodeData:data]; NSString *base64String = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; return base64String; } + (NSString*)decodeBase64Data:(NSData *)data { data = [GTMBase64 decodeData:data]; NSString *base64String = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; return base64String; } #pragma mark - AES加密 //将string转成带password的data +(NSString*)encryptAESData:(NSString*)string app_key:(NSString*)key { //将nsstring转化为nsdata NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; //使用password对nsdata进行加密 NSData *encryptedData = [data AES128EncryptWithKey:key]; NSLog(@"加密后的字符串 :%@",[encryptedData base64Encoding]); return [encryptedData base64Encoding]; } #pragma mark - AES解密 //将带password的data转成string +(NSString*)decryptAESData:(NSData*)data app_key:(NSString*)key { //使用password对data进行解密 NSData *decryData = [data AES128DecryptWithKey:key]; //将解了password的nsdata转化为nsstring NSString *str = [[NSString alloc] initWithData:decryData encoding:NSUTF8StringEncoding]; NSLog(@"解密后的字符串 :%@",str); return [str autorelease]; } @end
详细的用法例如以下:
加密:
[SecurityUtilencryptAESData:@"XXXXXX" app_key:KEY]; 这里的是能够自己设置一个16位的字符串
解密:
NSData *EncryptData1 = [GTMBase64decodeString:[SecurityUtil encryptAESData:@"XXXXXX" app_key:KEY]];//解密前进行GTMBase64编码
NSString * string1 = [SecurityUtildecryptAESData:EncryptData1app_key:KEY];
以上是关于iOS AES的加密解密的主要内容,如果未能解决你的问题,请参考以下文章
iOS开发之Objective-c的AES加密和解密算法的实现
AES 加密在 iOS 和 Android 中产生不同的结果