How to decrypt data using Openssl tool encrypted using AES128 in iOS

I have many pieces of code that encrypt data using AES128 (if you provide your working implementation, I will be very grateful) For example, this:

- (NSData*)AES128EncryptWithKey:(NSString*)key {
    // 'key' should be 16 bytes for AES128, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES128 + 1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [self length];

    //See the doc: For block ciphers, the output size will always be less than or
    //equal to the input size plus the size of one block.
    //That why we need to add the size of one block here
    size_t bufferSize           = dataLength + kCCBlockSizeAES128;
    void* buffer                = malloc(bufferSize);

    size_t numBytesEncrypted    = 0;

    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode + kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES128,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);

    if (cryptStatus == kCCSuccess)
    {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    free(buffer); //free the buffer;
    return nil;
}

After that, the data is encoded by base64, using the online tool I save it in data.bin

What I want to do is decrypt this data using OpenSSl. But when I call

openssl enc -aes-128-ecb -in data.bin -out out.bin -d -pass pass:0123456789123456

He told me a bad magic number

If i use

openssl enc -aes-128-ecb -in data.bin -out out.bin -d -pass pass:0123456789123456 -nosalt

He told me bad to decrypt

Please, help.

+5
source share
1 answer

. -, CBC ( CCCrypt), ECB. ECB.

( "0123456789123456" ) , . . , openssl . enc(1). , PBKDF2, ( ). -K. IV . IV . , openssl.

, , . AES CommonCrypto. - , , IV. enc, aes-128-cbc ( 128- AES), .

: / , . , , CCCrypt(), OpenSSL, . -, " ", , . AES128EncryptWithKey: - ; , "", . , OpenSSL , CCCrypt .

+5

All Articles