I need to perform AES 256 decryption in my iphone app. The scenario is that the plain text is base64 encoded and encrypted using AES256 bit (OFB mode) in an online webpage. In my app, I retrieve the encrypted text from that webpage as a query string. Here I have done base64 decoding and AES256 Decryption. But I am getting -4304 status.
I have used kCCOptionPKCS7Padding
. Even if I change the padding also, I am not getting proper Decrypted Plain text. Only unreadable text is displayed.
I have used http://isv.appspot.com/app/enc for checking the AES256 encryption with base64 by setting OFB mode.
Following code has two methods which I used for base64 decoding and AES256 decryption
+ (NSString*)decryptBase64String:(NSString*)encryptedBase64String keyString:(NSString*)keyString
{
NSData* encryptedData = [NSData dataFromBase64String:encryptedBase64String];
NSLog(@"encryptedData %@",encryptedData);
// NSData *strData = [encryptedData subdataWithRange:NSMakeRange(0, [encryptedData length] - 2)];
// NSString* newStr = nil;
NSData* keyData = [keyString dataUsingEncoding:NSUTF8StringEncoding];
NSData* data = [self decryptData:encryptedData
key:keyData
iv:nil];
// newStr = [NSString stringWithCString:[strData bytes] encoding:NSUTF8StringEncoding];
if (data) {
return [[[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding] autorelease];
} else {
return nil;
}
}
+ (NSData*)decryptData:(NSData*)data key:(NSData*)key iv:(NSData*)iv;
{
NSData* result = nil;
// setup key
unsigned char cKey[FBENCRYPT_KEY_SIZE];
bzero(cKey, sizeof(cKey));
[key getBytes:cKey length:FBENCRYPT_KEY_SIZE];
// setup iv
char cIv[FBENCRYPT_BLOCK_SIZE];
bzero(cIv, FBENCRYPT_BLOCK_SIZE);
if (iv) {
[iv getBytes:cIv length:FBENCRYPT_BLOCK_SIZE];
}
// setup output buffer
size_t bufferSize = [data length] + FBENCRYPT_BLOCK_SIZE;
void *buffer = malloc(bufferSize);
// do decrypt
size_t decryptedSize = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
FBENCRYPT_ALGORITHM,
kCCOptionPKCS7Padding,
cKey,
FBENCRYPT_KEY_SIZE,
cIv,
[data bytes],
[data length],
buffer,
bufferSize,
&decryptedSize);
if (cryptStatus == kCCSuccess) {
result = [NSData dataWithBytesNoCopy:buffer length:decryptedSize];
} else {
free(buffer);
NSLog(@"[ERROR] failed to decrypt| CCCryptoStatus: %d", cryptStatus);
}
return result;
}
Could anyone help me out to get the plain text by using this method?