- 
      
- 
        Save matsuda/9204276 to your computer and use it in GitHub Desktop. 
| /** | |
| http://mythosil.hatenablog.com/entry/20111017/1318873155 | |
| http://blog.dealforest.net/2012/03/ios-android-per-aes-crypt-connection/ | |
| */ | |
| @interface NSData (AES) | |
| - (NSData *)AES128EncryptedDataWithKey:(NSString *)key; | |
| - (NSData *)AES128DecryptedDataWithKey:(NSString *)key; | |
| - (NSData *)AES128EncryptedDataWithKey:(NSString *)key iv:(NSString *)iv; | |
| - (NSData *)AES128DecryptedDataWithKey:(NSString *)key iv:(NSString *)iv; | |
| @end | 
| #import "NSData+AES.h" | |
| #import <CommonCrypto/CommonCryptor.h> | |
| @implementation NSData (AES) | |
| - (NSData *)AES128EncryptedDataWithKey:(NSString *)key | |
| { | |
| return [self AES128EncryptedDataWithKey:key iv:nil]; | |
| } | |
| - (NSData *)AES128DecryptedDataWithKey:(NSString *)key | |
| { | |
| return [self AES128DecryptedDataWithKey:key iv:nil]; | |
| } | |
| - (NSData *)AES128EncryptedDataWithKey:(NSString *)key iv:(NSString *)iv | |
| { | |
| return [self AES128Operation:kCCEncrypt key:key iv:iv]; | |
| } | |
| - (NSData *)AES128DecryptedDataWithKey:(NSString *)key iv:(NSString *)iv | |
| { | |
| return [self AES128Operation:kCCDecrypt key:key iv:iv]; | |
| } | |
| - (NSData *)AES128Operation:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv | |
| { | |
| char keyPtr[kCCKeySizeAES128 + 1]; | |
| bzero(keyPtr, sizeof(keyPtr)); | |
| [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; | |
| char ivPtr[kCCBlockSizeAES128 + 1]; | |
| bzero(ivPtr, sizeof(ivPtr)); | |
| if (iv) { | |
| [iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding]; | |
| } | |
| NSUInteger dataLength = [self length]; | |
| size_t bufferSize = dataLength + kCCBlockSizeAES128; | |
| void *buffer = malloc(bufferSize); | |
| size_t numBytesEncrypted = 0; | |
| CCCryptorStatus cryptStatus = CCCrypt(operation, | |
| kCCAlgorithmAES128, | |
| kCCOptionPKCS7Padding | kCCOptionECBMode, | |
| keyPtr, | |
| kCCBlockSizeAES128, | |
| ivPtr, | |
| [self bytes], | |
| dataLength, | |
| buffer, | |
| bufferSize, | |
| &numBytesEncrypted); | |
| if (cryptStatus == kCCSuccess) { | |
| return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; | |
| } | |
| free(buffer); | |
| return nil; | |
| } | |
| @end | 
dataWithBytesNoCopy means the NSData object will take ownership of the memory location so you don't have to free it afterwards.
Please note that the AES-ECB mode of encryption used here is not secure in any way!
See here for details: http://crypto.stackexchange.com/questions/20941/why-shouldnt-i-use-ecb-encryption
It can be fixed by deleting the kCCOptionECBModekeyword in the CCCrypt(...) call. CCCrypt will then use CBC which is its default mode.
how to use
Add #import<UIKit/UiKit.h> in header file.
Hello after decryption it is giving null
here the data is decrypted
theJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&theJsonError];
NSLog(@"RESPONSE_JSON :: %@",(NSString *)theJson);
LOGS :   RESPONSE_JSON :: null
Any chance you can make this available to the rest of us? Apache / MIT license? By default this is copyrighted.
Abhishek9634 have you solved the issue am facing similar can you help me
Hi, can anyone tell me how do i need to connect this AES encryption to my Segue view controller?
This is my coding:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showdetails"]){
ImageDetailViewController *imagedetailviewcontroller = [segue destinationViewController];
}