Code Monkey home page Code Monkey logo

Comments (3)

Rahulgupta-cdnsol avatar Rahulgupta-cdnsol commented on August 11, 2024

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.

from aescrypt-android.

ninjatrench avatar ninjatrench commented on August 11, 2024

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

from aescrypt-android.

Rahulgupta-cdnsol avatar Rahulgupta-cdnsol commented on August 11, 2024

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};

from aescrypt-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.