IOS開發之----經常使用加密方法

本文轉載至 http://blog.csdn.net/wildfireli/article/details/23191983app

 

(AES、MD五、Base64) 分類: iPhone 2014-04-08 16:30 187人閱讀 評論(0) 收藏 舉報 目錄(?)[+] 一、AES加密 NSData+AES.h文件 // // NSData-AES.h // Smile // // Created by Gary on 12-11-24. // Copyright (c) 2012年 BOX. All rights reserved. // #import @class NSString; @interface NSData (Encryption) - (NSData *)AES256EncryptWithKey:(NSString *)key; //加密 - (NSData *)AES256DecryptWithKey:(NSString *)key; //解密 @end NSData+AES.m文件 // // NSData-AES.h // Smile // // Created by Gary on 12-11-24. // Copyright (c) 2012年 BOX. All rights reserved. // #import "NSData+AES.h" #import @implementation NSData (Encryption) - (NSData *)AES256EncryptWithKey:(NSString *)key {//加密 char keyPtr[kCCKeySizeAES256+1]; bzero(keyPtr, 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, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL, [self bytes], dataLength, buffer, bufferSize, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); return nil; } - (NSData *)AES256DecryptWithKey:(NSString *)key {//解密 char keyPtr[kCCKeySizeAES256+1]; bzero(keyPtr, 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 numBytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL, [self bytes], dataLength, buffer, bufferSize, &numBytesDecrypted); if (cryptStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; } free(buffer); return nil; } @end 這裏AES在iOS加過密之後以nsdata的形式存下來,若是想以nsstring形式存儲,那麼對nsdata進行base64位編碼。 二、BASE64編碼 首先下載GTMBase64文件,在工程中加入三個文件 GTMDefines.h GTMBase64.h GTMBase64.m 你能夠在這裏找到這三個文件 http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/?r=87 你也能夠在下面的demo裏面找到這3個文件,demo會完整實現文章裏面經常使用的3種編碼方法。 我在此稍微封裝一下: .h文件 #pragma mark - base64 + (NSString*)encodeBase64String:(NSString *)input; + (NSString*)decodeBase64String:(NSString *)input; + (NSString*)encodeBase64Data:(NSData *)data; + (NSString*)decodeBase64Data:(NSData *)data; .m文件 #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; } 三、MD5加密 NSString+MD5.h文件 // // NSString+MD5Encrypt.h // Smile // // Created by Gary on 12-11-24. // Copyright (c) 2012年 BOX. All rights reserved. // #import @interface NSString (MD5) - (NSString *)md5Encrypt; @end NSString+MD5.m文件 // // NSString+MD5Encrypt.h // Smile // // Created by Gary on 12-11-24. // Copyright (c) 2012年 BOX. All rights reserved. // #import "NSString+MD5.h" @implementation NSString (MD5) - (NSString *)md5Encrypt { const char *original_str = [self UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(original_str, strlen(original_str), result); NSMutableString *hash = [NSMutableString string]; for (int i = 0; i < 16; i++) [hash appendFormat:@"X", result[i]]; return [hash lowercaseString]; } @endgoogle

相關文章
相關標籤/搜索