IOS AES 128 ECB PKCS7Padding 16进制加密解密

[objc]  view plain  copy
  1. //////////////AES加密方式: AES 128 ECB PKCS7Padding 16进制加密解密  //////////////  
  2.     NSString *originalStr=@"AES";  
  3.   
  4.     //加密  
  5.     NSString *encryStr = [AES128Util AES128Encrypt:originalStr key:AES_KEY];  
  6.     //解密  
  7.     NSString *decryStr = [AES128Util AES128Decrypt:encryStr key:AES_KEY];  
  8.       
  9.     NSLog(@"\n加密前:%@\n加密后:%@ \n解密后:%@",originalStr,encryStr,decryStr);  


[objc]  view plain  copy
  1. //  
  2. //  AES128Util.h  
  3. //  ASE128Demo  
  4. //  
  5. //  Created by zhenghaishu on 11/11/13.  
  6. //  Copyright (c) 2013 Youku.com inc. All rights reserved.  
  7. //  
  8.   
  9. #import   
  10.   
  11. @interface AES128Util : NSObject  
  12.   
  13. +(NSString *)AES128Encrypt:(NSString *)plainText key:(NSString *)key;  
  14.   
  15. +(NSString *)AES128Decrypt:(NSString *)encryptText key:(NSString *)key;  
  16.   
  17. @end  


[objc]  view plain  copy
  1. //  
  2. //  AES128Util.m  
  3. //  ASE128Demo  
  4. //  
  5. //  Created by zhenghaishu on 11/11/13.  
  6. //  Copyright (c) 2013 Youku.com inc. All rights reserved.  
  7. //  
  8.   
  9. #import "AES128Util.h"  
  10. #import   
  11. #import "GTMBase64.h"  
  12.   
  13. @implementation AES128Util  
  14.   
  15. +(NSString *)AES128Encrypt:(NSString *)plainText key:(NSString *)key  
  16. {  
  17.     char keyPtr[kCCKeySizeAES128+1];  
  18.     memset(keyPtr, 0sizeof(keyPtr));  
  19.     [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];  
  20.       
  21.     NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];  
  22.     NSUInteger dataLength = [data length];  
  23.       
  24.     size_t bufferSize = dataLength + kCCBlockSizeAES128;  
  25.     voidvoid *buffer = malloc(bufferSize);  
  26.     size_t numBytesEncrypted = 0;  
  27.     CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,  
  28.                                           kCCAlgorithmAES128,  
  29.                                           kCCOptionPKCS7Padding|kCCOptionECBMode,  
  30.                                           keyPtr,  
  31.                                           kCCBlockSizeAES128,  
  32.                                           NULL,  
  33.                                           [data bytes],  
  34.                                           dataLength,  
  35.                                           buffer,  
  36.                                           bufferSize,  
  37.                                           &numBytesEncrypted);  
  38.     if (cryptStatus == kCCSuccess) {  
  39.         NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];  
  40.         //return [GTMBase64 stringByEncodingData:resultData];  
  41.         return [self hexStringFromData:resultData];  
  42.   
  43.     }  
  44.     free(buffer);  
  45.     return nil;  
  46. }  
  47.   
  48.   
  49. +(NSString *)AES128Decrypt:(NSString *)encryptText key:(NSString *)key  
  50. {  
  51.     char keyPtr[kCCKeySizeAES128 + 1];  
  52.     memset(keyPtr, 0sizeof(keyPtr));  
  53.     [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];  
  54.       
  55.     //NSData *data = [GTMBase64 decodeData:[encryptText dataUsingEncoding:NSUTF8StringEncoding]];  
  56.       
  57.     NSData *data=[self dataForHexString:encryptText];  
  58.       
  59.     NSUInteger dataLength = [data length];  
  60.     size_t bufferSize = dataLength + kCCBlockSizeAES128;  
  61.     voidvoid *buffer = malloc(bufferSize);  
  62.       
  63.     size_t numBytesCrypted = 0;  
  64.     CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,  
  65.                                           kCCAlgorithmAES128,  
  66.                                           kCCOptionPKCS7Padding|kCCOptionECBMode,  
  67.                                           keyPtr,  
  68.                                           kCCBlockSizeAES128,  
  69.                                           NULL,  
  70.                                           [data bytes],  
  71.                                           dataLength,  
  72.                                           buffer,  
  73.                                           bufferSize,  
  74.                                           &numBytesCrypted);  
  75.     if (cryptStatus == kCCSuccess) {  
  76.         NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];  
  77.         return [[[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding] autorelease];  
  78.     }  
  79.     free(buffer);  
  80.     return nil;  
  81. }  
  82.   
  83. // 普通字符串转换为十六进  
  84. + (NSString *)hexStringFromData:(NSData *)data {  
  85.     Byte *bytes = (Byte *)[data bytes];  
  86.     // 下面是Byte 转换为16进制。  
  87.     NSString *hexStr = @"";  
  88.     for(int i=0; i<[data length]; i++) {  
  89.         NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i] & 0xff]; //16进制数  
  90.         newHexStr = [newHexStr uppercaseString];  
  91.           
  92.         if([newHexStr length] == 1) {  
  93.             newHexStr = [NSString stringWithFormat:@"0%@",newHexStr];  
  94.         }  
  95.           
  96.         hexStr = [hexStr stringByAppendingString:newHexStr];  
  97.           
  98.     }  
  99.     return hexStr;  
  100. }  
  101.   
  102. //参考:http://blog.csdn.net/linux_zkf/article/details/17124577  
  103. //十六进制转Data  
  104. + (NSData*)dataForHexString:(NSString*)hexString  
  105. {  
  106.     if (hexString == nil) {  
  107.           
  108.         return nil;  
  109.     }  
  110.       
  111.     const char* ch = [[hexString lowercaseString] cStringUsingEncoding:NSUTF8StringEncoding];  
  112.     NSMutableData* data = [NSMutableData data];  
  113.     while (*ch) {  
  114.         if (*ch == ' ') {  
  115.             continue;  
  116.         }  
  117.         char byte = 0;  
  118.         if ('0' <= *ch && *ch <= '9') {  
  119.               
  120.             byte = *ch - '0';  
  121.         }else if ('a' <= *ch && *ch <= 'f') {  
  122.               
  123.             byte = *ch - 'a' + 10;  
  124.         }else if ('A' <= *ch && *ch <= 'F') {  
  125.               
  126.             byte = *ch - 'A' + 10;  
  127.               
  128.         }  
  129.           
  130.         ch++;  
  131.           
  132.         byte = byte << 4;  
  133.           
  134.         if (*ch) {  
  135.               
  136.             if ('0' <= *ch && *ch <= '9') {  
  137.                   
  138.                 byte += *ch - '0';  
  139.                   
  140.             } else if ('a' <= *ch && *ch <= 'f') {  
  141.                   
  142.                 byte += *ch - 'a' + 10;  
  143.                   
  144.             }else if('A' <= *ch && *ch <= 'F'){  
  145.                   
  146.                 byte += *ch - 'A' + 10;  
  147.                   
  148.             }  
  149.               
  150.             ch++;  
  151.               
  152.         }  
  153.           
  154.         [data appendBytes:&byte length:1];  
  155.           
  156.     }  
  157.       
  158.     return data;  
  159. }  
  160.   
  161. @end  

IOS AES 128 ECB PKCS7Padding 16进制加密解密_第1张图片

AES在线加密工具

http://www.seacha.com/tools/aes.html?src=wenhan123455678888050i33i206279278&mode=ECB&keylen=128&key=1234567890123456&iv=1234567890123456&bpkcs=pkcs7padding&session=FH4xlZr0LlqsJVtwfbVw&aes=ac518f9f8429369bf129da6d6633cbb2&encoding=hex&type=0

参考工程:https://github.com/zhenghaishu/AES128-for-iOS



http://pan.baidu.com/s/1jHmo9Pc

你可能感兴趣的:(IOS AES 128 ECB PKCS7Padding 16进制加密解密)