2

I am trying to decrypt the data from last couple of days. I decrypt the data but the result is not same as original. Please, can anyone suggest how I can do this. I am trying to decrypt the following data:

Encrypted Data : "Mmb9tEkmW82oFPJb3vNhHA=="

Encrypt Key : "91860F52E5C3A09BA3B827F28070E08D"

Original data: "Marco"

Please help. Thanks

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
Mitesh Khatri
  • 3,935
  • 4
  • 44
  • 67
  • You might want to give some details where the encrypted string come from and how are you initializing the iPhone crypto library. Meanwhile, this SO Q might give you some ideas on what might be going wrong - http://stackoverflow.com/questions/538435/aes-interoperability-between-net-and-iphone – Franci Penov Jul 07 '10 at 06:48
  • 1
    The first step is to base64-decode the data. – President James K. Polk Jul 07 '10 at 11:10

1 Answers1

3

Here is how I used AES128

- (NSData *)cipherData:(NSData *)data {
    return [self aesOperation:kCCEncrypt OnData:data];
}

- (NSData *)decipherData:(NSData *)data {
    return [self aesOperation:kCCDecrypt OnData:data];
}


- (NSData *)aesOperation:(CCOperation)op OnData:(NSData *)data {
    NSData *outData = nil;

    // Data in parameters   
    const void *key = cipherKey.bytes;
    const void *dataIn = data.bytes;
    size_t dataInLength = data.length;  
    // Data out parameters
    size_t outMoved = 0;

    // Init out buffer
    unsigned char outBuffer[BUFFER_SIZE];
    memset(outBuffer, 0, BUFFER_SIZE);
    CCCryptorStatus status = -1;

    status = CCCrypt(op, kCCAlgorithmAES128, kCCOptionPKCS7Padding, key, kCCKeySizeAES256, NULL,
                 dataIn, dataInLength, &outBuffer, BUFFER_SIZE, &outMoved);

    if(status == kCCSuccess) {  
        outData = [NSData dataWithBytes:outBuffer length:outMoved];
    } else if(status == kCCBufferTooSmall) {
        // Resize the out buffer
        size_t newsSize = outMoved;
        void *dynOutBuffer = malloc(newsSize);
        memset(dynOutBuffer, 0, newsSize);
        outMoved = 0;

        status = CCCrypt(op, kCCAlgorithmAES128, kCCOptionPKCS7Padding, key, kCCKeySizeAES256, NULL,
                     dataIn, dataInLength, &outBuffer, BUFFER_SIZE, &outMoved);

    if(status == kCCSuccess) {  
        outData = [NSData dataWithBytes:outBuffer length:outMoved];
    }
}

return outData; 
}
Charter
  • 783
  • 4
  • 6