I am using AES256 for security and store data in encryption form which is working fine in IOS 6 and below but when i have tested my app in IOS 7, I am not getting my data which was store previously. After debugs, i found decryption is not working is IOS 7 and return blank.
My Code as below:
- (NSData *)AES256DecryptWithKey:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer); //free the buffer;
return nil;
}
Can you please help to get my data again in IOS 7?
Thanks
This project contains the implementation of 256 bit AES encryption which works on all the platforms (C#, iOS, Android). One of the key objective is to make AES work on all the platforms with simple implementation. Platforms Supported: iOS , Android , Windows (C#).
If you want to do AES-ECB or AES-CBC, you’ll need CommonCrypto. If want to do AES-GCM (which is generally what I recommend) you’ll need Apple CryptoKit. Quinn “The Eskimo!” Hi Quinn, Thank you for quick response. I am interested in all three flavours (AES-ECB, AES-CBC, AES-GCM) for AES-256.
The problem is, when OS X or iOS sends a signed email there seems to be no indicator included which tells the receiving mail program what type of encryption method to use. This doesn't matter if the receiving mail program is OS X Mail or iOS Mail. But MS Outlook and Windows Live Mail, for that reason, fall back to a 40-bit RC2 encryption method.
What flavour of AES? If you want to do AES-ECB or AES-CBC, you’ll need CommonCrypto. If want to do AES-GCM (which is generally what I recommend) you’ll need Apple CryptoKit. Quinn “The Eskimo!”
Found the solution on this problem on Apple Devforums.
- (NSData *)encrypt:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
BOOL patchNeeded = ([key length] > kCCKeySizeAES256);
if (patchNeeded) {
key = [key substringToIndex:kCCKeySizeAES256]; // Ensure that the key isn't longer than what's needed (kCCKeySizeAES256)
}
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
if (patchNeeded) {
keyPtr[0] = '\0'; // Previous iOS version than iOS7 set the first char to '\0' if the key was longer than kCCKeySizeAES256
}
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer); //free the buffer;
return nil;
}
Of course, copy paste the same patch for the decrypt method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With