iOS AES128加密解密

有个项目用过AES128的加密,当时搜索了很久的资料,基本不能使用。这里我自己写了一个可以用的  代码如下:

(PS:这个方法写的还有些瑕疵 不过已经足够正常使用了,有机会的话 会写的好看一点。。。转发要标注出处哦)

这是.h 

 1 //
 2 //  AES128CBC_No_Padding_Unit.h
 3 // 
 4 //
 5 //  Created by apple on 14-5-13.
 6 //  Copyright (c) 2014年 akforsure. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface AES128CBC_No_Padding_Unit : NSObject
12 /**
13  *  AES128加密
14  *
15  *  @param plainText 原文
16  *
17  *  @return 加密好的字符串
18  */
19 + (NSString *)AES128Encrypt:(NSString *)plainText;
20 /**
21  *  AES128解密
22  *
23  *  @param encryptText 密文
24  *
25  *  @return 明文
26  */
27 + (NSString *)AES128Decrypt:(NSString *)encryptText;
28 @end

这是.m

  1 //
  2 //  AES128CBC_No_Padding_Unit.m
  3 // 
  4 //
  5 //  Created by apple on 14-5-13.
  6 //  Copyright (c) 2014年 akforsure. All rights reserved.
  7 //
  8 
  9 #import "AES128CBC_No_Padding_Unit.h"
 10 #import <CommonCrypto/CommonCryptor.h>
 11 #import "GTMBase64.h"
 12 @implementation AES128CBC_No_Padding_Unit
 13 + (NSString *)AES128Encrypt:(NSString *)plainText
 14 {
 15     char keyPtr[kCCKeySizeAES128+1];
 16     memset(keyPtr, 0, sizeof(keyPtr));
 17     [AESKEY getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
 18     
 19     char ivPtr[kCCBlockSizeAES128+1];
 20     memset(ivPtr, 0, sizeof(ivPtr));
 21     [AESIV getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
 22     
 23     NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
 24     NSUInteger dataLength = [data length];
 25     
 26     int diff = kCCKeySizeAES128 - (dataLength % kCCKeySizeAES128);
 27     int newSize = 0;
 28     
 29     if(diff > 0)
 30     {
 31         newSize = dataLength + diff;
 32     }
 33     
 34     char dataPtr[newSize];
 35     memcpy(dataPtr, [data bytes], [data length]);
 36     for(int i = 0; i < diff; i++)
 37     {
 38         dataPtr[i + dataLength] = 0x00;
 39     }
 40     
 41     size_t bufferSize = newSize + kCCBlockSizeAES128;
 42     void *buffer = malloc(bufferSize);
 43     memset(buffer, 0, bufferSize);
 44     
 45     size_t numBytesCrypted = 0;
 46     
 47     CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
 48                                           kCCAlgorithmAES128,
 49                                           0x0000,   //这里用的 NoPadding的填充方式
 50                                                     //除此以外还有 kCCOptionPKCS7Padding 和 kCCOptionECBMode
 51                                           keyPtr,
 52                                           kCCKeySizeAES128,
 53                                           ivPtr,
 54                                           dataPtr,
 55                                           sizeof(dataPtr),
 56                                           buffer,
 57                                           bufferSize,
 58                                           &numBytesCrypted);
 59     
 60     if (cryptStatus == kCCSuccess) {
 61         NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
 62         return [GTMBase64 stringByEncodingData:resultData];
 63     }
 64     free(buffer);
 65     return nil;
 66 }
 67 
 68 + (NSString *)AES128Decrypt:(NSString *)encryptText
 69 {
 70     char keyPtr[kCCKeySizeAES128 + 1];
 71     memset(keyPtr, 0, sizeof(keyPtr));
 72     [AESKEY getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
 73     
 74     char ivPtr[kCCBlockSizeAES128 + 1];
 75     memset(ivPtr, 0, sizeof(ivPtr));
 76     [AESIV getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
 77     
 78     NSData *data = [GTMBase64 decodeData:[encryptText dataUsingEncoding:NSUTF8StringEncoding]];
 79     NSUInteger dataLength = [data length];
 80     size_t bufferSize = dataLength + kCCBlockSizeAES128;
 81     void *buffer = malloc(bufferSize);
 82     
 83     size_t numBytesCrypted = 0;
 84     CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
 85                                           kCCAlgorithmAES128,
 86                                           0x0000,
 87                                           keyPtr,
 88                                           kCCBlockSizeAES128,
 89                                           ivPtr,
 90                                           [data bytes],
 91                                           dataLength,
 92                                           buffer,
 93                                           bufferSize,
 94                                           &numBytesCrypted);
 95     if (cryptStatus == kCCSuccess) {
 96         NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
 97         return [[[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding] autorelease];
 98     }
 99     free(buffer);
100     return nil;
101 }
102 
103 @end

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。