1

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?

jagadesh
  • 3
  • 1
Jagadesh
  • 11
  • 3
  • What does the documentation say about what causes -4304 status? – rossum Jun 13 '12 at 11:47
  • 1
    Have you seen this: http://stackoverflow.com/questions/7408754/unable-to-decrypt-data-encoded-via-openssl-on-iphone – Henrick Hellström Jun 13 '12 at 13:55
  • Out of curiosity, you say that the plain text is encoded in base64. That doesnt necessarily mean the encrypted Ciphertext is, yet you are setting up encryptedData as a base64? – trumpetlicks Jun 13 '12 at 22:21

1 Answers1

0

Based on the facts in your question, the base64 encoded data is encrypted, not the decoded variation of that data. Because of this, you need to decrypt the base64 encoded data and then base64 decode it.

Michael J. Gray
  • 9,784
  • 6
  • 38
  • 67