在其他平台中经常会计算MD5值,在ios平台中也提供了该方法,首先需要导入头文件
#import <CommonCrypto/CommonDigest.h>
方法CC_MD5可以获取MD5的16个字符的数组,再通过%02X的形式输出即可获取32位MD5值。
@implementation NSString (CCCryptUtil) -(NSString*) md5 { const char * cStrValue = [self UTF8String]; unsigned char theResult[CC_MD5_DIGEST_LENGTH]; CC_MD5(cStrValue, strlen(cStrValue), theResult); return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", theResult[0], theResult[1], theResult[2], theResult[3], theResult[4], theResult[5], theResult[6], theResult[7], theResult[8], theResult[9], theResult[10], theResult[11], theResult[12], theResult[13], theResult[14], theResult[15]]; } @end
MD5只能称为一种不可逆的加密算法,只能用作一些检验过程,不能恢复其原文。
apple还提供了RSA、DES、AES等加密算法,见到国外的网站关于AES加密的算法,在此经过加工可以用于字符串加密机密,可用于安全性要求较高的应用。
首先需要导入头文件
#import <CommonCrypto/CommonCryptor.h>
将NSData分类,添加NSData加密解密方法
@implementation NSData (CCCryptUtil) - (NSData*)AES256EncryptWithKey:(NSString*)key { char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused) bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) [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, keyPtr, kCCKeySizeAES256, NULL /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); return nil; } - (NSData*)AES256DecryptWithKey:(NSString*)key { char keyPtr[kCCKeySizeAES256 + 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]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void* buffer = malloc(bufferSize); size_t numBytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesDecrypted); if (cryptStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; } free(buffer); //free the buffer; return nil; } @end
上述代码AES256EncryptWithKey方法为加密函数,AES256DecryptWithKey为解密函数,加密和解密方法使用的参数密钥均为32位长度的字符串,所以可以将任意的字符串经过md5计算32位字符串作为密钥,这样可以允许客户输入任何长度的密钥,并且不同密钥的MD5值也不会重复。
结合上述代码,加工NSString类,提供字符串的AES加密解密方法。代码如下:
@implementation NSString (CCCryptUtil) // md5方法此处省略 + (NSData*)AES256Encrypt:(NSString*)strSource withKey:(NSString*)key { NSData *dataSource = [strSource dataUsingEncoding:NSUTF8StringEncoding]; return [dataSource AES256EncryptWithKey:[key md5]]; } + (NSString*)AES256Decrypt:(NSData*)dataSource withKey:(NSString*)key { NSData *decryptData = [dataSource AES256DecryptWithKey:[key md5]]; return [[NSString alloc] initWithData:decryptData encoding:NSUTF8StringEncoding]; } @end
需要源码的盆友可以邮件联系。