Created
February 25, 2014 07:14
-
-
Save matsuda/9204276 to your computer and use it in GitHub Desktop.
Objective-C code for encrypt and decrypt by AES-128 encryption.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
kCCOptionECBMode
keyword in theCCCrypt(...)
call.CCCrypt
will then use CBC which is its default mode.