AESCrypt-Android icon indicating copy to clipboard operation
AESCrypt-Android copied to clipboard

Encryption result is different on Android and IOS

Open CDNRahulSonpaliya opened this issue 8 years ago • 3 comments

Hello,

I am using this library in my android project and its counter part https://github.com/Gurpartap/aescrypt in ios project. I am encrypting same text with same encryption key, but it is giving me different results on ios and android.

I am using this key : 25c35734b1ef623ca2a7f730cf2fea8b790739ba

String to encrypt is : Password

Encrypted String IOS : W3LyAxKq2+QdDBfKUGVgTg== Encrypted String Android : zdgSimKva1jblici7F8DGw==

CDNRahulSonpaliya avatar Nov 02 '16 16:11 CDNRahulSonpaliya

Hi,

I am getting same issue with library as it is not generating same result as iOS.What needs to do to get similar result as iOS.

Rahulgupta-cdnsol avatar Nov 03 '16 05:11 Rahulgupta-cdnsol

Hi, I can look into it. Can you post the IV used by you in Android as well as iOS as iOS directly doesn't take blank IV

ninjatrench avatar Nov 04 '16 08:11 ninjatrench

in IOS I am using below function :

  • (NSData *)doCipher:(NSData *)plainText key:(NSData *)aSymmetricKey context:(CCOperation)encryptOrDecrypt padding:(CCOptions *)pkcs7{ CCCryptorStatus ccStatus = kCCSuccess; // Symmetric crypto reference. CCCryptorRef thisEncipher = NULL; // Cipher Text container. NSData * cipherOrPlainText = nil; // Pointer to output buffer. uint8_t * bufferPtr = NULL; // Total size of the buffer. size_t bufferPtrSize = 0;
    // Remaining bytes to be performed on. size_t remainingBytes = 0; // Number of bytes moved to buffer. size_t movedBytes = 0; // Length of plainText buffer. size_t plainTextBufferSize = 0; // Placeholder for total written. size_t totalBytesWritten = 0; // A friendly helper pointer. uint8_t * ptr;

    // Initialization vector; dummy in this case 0's. uint8_t iv[kChosenCipherBlockSize]; memset((void *) iv, 0x0, (size_t) sizeof(iv));

    // //NSLog(@"doCipher: plaintext: %@", plainText); // //NSLog(@"doCipher: key length: %d", [aSymmetricKey length]);

    plainTextBufferSize = [plainText length];

    if(encryptOrDecrypt == kCCEncrypt) { if(*pkcs7 != kCCOptionECBMode) { if((plainTextBufferSize % kChosenCipherBlockSize) == 0) { *pkcs7 = 0x0000; } else { *pkcs7 = kCCOptionPKCS7Padding; } } } else if(encryptOrDecrypt != kCCDecrypt) { //NSLog(@"Invalid CCOperation parameter [%d] for cipher context.", *pkcs7 ); }

    // Create and Initialize the crypto reference. ccStatus = CCCryptorCreate(encryptOrDecrypt, kCCAlgorithmAES128, *pkcs7, (const void *)[aSymmetricKey bytes], kChosenCipherKeySize, (const void *)iv, &thisEncipher );

    //LOGGING_FACILITY1( ccStatus == kCCSuccess, @"Problem creating the context, ccStatus == %d.", ccStatus );

    // Calculate byte block alignment for all calls through to and including final. bufferPtrSize = CCCryptorGetOutputLength(thisEncipher, plainTextBufferSize, true);

    // Allocate buffer. bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t) );

    // Zero out buffer. memset((void *)bufferPtr, 0x0, bufferPtrSize);

    // Initialize some necessary book keeping.

    ptr = bufferPtr;

    // Set up initial size. remainingBytes = bufferPtrSize;

    // Actually perform the encryption or decryption. ccStatus = CCCryptorUpdate(thisEncipher, (const void *) [plainText bytes], plainTextBufferSize, ptr, remainingBytes, &movedBytes );

    //LOGGING_FACILITY1( ccStatus == kCCSuccess, @"Problem with CCCryptorUpdate, ccStatus == %d.", ccStatus );

    // Handle book keeping. ptr += movedBytes; remainingBytes -= movedBytes; totalBytesWritten += movedBytes;

    /* From CommonCryptor.h:

    @enum CCCryptorStatus @abstract Return values from CommonCryptor operations.

    @constant kCCSuccess Operation completed normally. @constant kCCParamError Illegal parameter value. @constant kCCBufferTooSmall Insufficent buffer provided for specified operation. @constant kCCMemoryFailure Memory allocation failure. @constant kCCAlignmentError Input size was not aligned properly. @constant kCCDecodeError Input data did not decode or decrypt properly. @constant kCCUnimplemented Function not implemented for the current algorithm.

    enum { kCCSuccess = 0, kCCParamError = -4300, kCCBufferTooSmall = -4301, kCCMemoryFailure = -4302, kCCAlignmentError = -4303, kCCDecodeError = -4304, kCCUnimplemented = -4305 }; typedef int32_t CCCryptorStatus; */

    // Finalize everything to the output buffer. ccStatus = CCCryptorFinal(thisEncipher, ptr, remainingBytes, &movedBytes );

    totalBytesWritten += movedBytes;

    if(thisEncipher) { (void) CCCryptorRelease(thisEncipher); thisEncipher = NULL; }

    //LOGGING_FACILITY1( ccStatus == kCCSuccess, @"Problem with encipherment ccStatus == %d", ccStatus );

    if (ccStatus == kCCSuccess) cipherOrPlainText = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)totalBytesWritten]; else cipherOrPlainText = nil;

    if(bufferPtr) free(bufferPtr);

    return cipherOrPlainText;

} Android :- byte[] ivBytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

Rahulgupta-cdnsol avatar Nov 04 '16 10:11 Rahulgupta-cdnsol