最近在整一個密碼鍵盤的加密,第一次涉及到AES這種加密方式,在這裏總結一下:html
話很少說,直接上代碼 .h import <Foundation/Foundation.h> @interface LanAES : NSObject +(NSData *)AES256ParmEncryptWithKey:(NSString *)key Encrypttext:(NSData *)text; //加密 +(NSData *)AES256ParmDecryptWithKey:(NSString *)key Decrypttext:(NSData *)text; //解密 +(NSString *) aes256_encrypt:(NSString *)key Encrypttext:(NSString *)text; +(NSString *) aes256_decrypt:(NSString *)key Decrypttext:(NSString *)text; @end .m #import "LanAES.h" #import <CommonCrypto/CommonCryptor.h> @implementation LanAES +(NSData *)AES256ParmEncryptWithKey:(NSString *)key Encrypttext:(NSData *)text //加密 { char keyPtr[kCCKeySizeAES256+1]; bzero(keyPtr, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [text length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL, [text bytes], dataLength, buffer, bufferSize, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); return nil; } + (NSData *)AES256ParmDecryptWithKey:(NSString *)key Decrypttext:(NSData *)text //解密 { char keyPtr[kCCKeySizeAES256+1]; bzero(keyPtr, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [text length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL, [text bytes], dataLength, buffer, bufferSize, &numBytesDecrypted); if (cryptStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; } free(buffer); return nil; } +(NSString *) aes256_encrypt:(NSString *)key Encrypttext:(NSString *)text { const char *cstr = [text cStringUsingEncoding:NSUTF8StringEncoding]; NSData *data = [NSData dataWithBytes:cstr length:text.length]; //對數據進行加密 NSData *result = [LanAES AES256ParmEncryptWithKey:key Encrypttext:data]; //轉換爲2進制字符串 if (result && result.length > 0) { Byte *datas = (Byte*)[result bytes]; NSMutableString *output = [NSMutableString stringWithCapacity:result.length * 2]; for(int i = 0; i < result.length; i++){ [output appendFormat:@"%02x", datas[i]]; } return output; } return nil; } +(NSString *) aes256_decrypt:(NSString *)key Decrypttext:(NSString *)text { //轉換爲2進制Data NSMutableData *data = [NSMutableData dataWithCapacity:text.length / 2]; unsigned char whole_byte; char byte_chars[3] = {'\0','\0','\0'}; int i; for (i=0; i < [text length] / 2; i++) { byte_chars[0] = [text characterAtIndex:i*2]; byte_chars[1] = [text characterAtIndex:i*2+1]; whole_byte = strtol(byte_chars, NULL, 16); [data appendBytes:&whole_byte length:1]; } //對數據進行解密 NSData* result = [LanAES AES256ParmDecryptWithKey:key Decrypttext:data]; if (result && result.length > 0) { return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; } return nil; }
參考app
http://www.cnblogs.com/starwolf/p/3365834.htmlpost
http://blog.csdn.net/lanzeng_in_sz/article/details/50218683加密