开发者

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

开发者 https://www.devze.com 2023-04-06 21:43 出处:网络
I have many snippets of code, which encrypt the data with AES128 (If you provide your working implementation I will be very thankfull) For example this one:

I have many snippets of code, which encrypt the data with AES128 (If you provide your working implementation I will be very thankfull) For example this one:

- (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's 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 it the data is base64 encoded, with online tool I save it to data.bin

The thing I want to do is to decrypt this data with OpenSSl. But, when I call

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

It tolds me bad magic number

In case I use

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

It tolds me bad decrypt

开发者_如何学JAVA

Please help.


There are several problems here. First, you're encrypting with CBC mode (which is the default for CCCrypt) but decrypting in ECB mode. There is very seldom reason to use ECB mode.

You're encrypting with a string (I assume "0123456789123456") as the key, not the password. These are different things. I'm not certain how openssl translates a password into a key. I don't see an explanation of that on the enc(1) page. I assume it uses PBKDF2, but it's not clear (and the number of iterations isn't given). You should be passing the actual key with the -K option. In that case, you also need to pass the IV explicitly. You're not correctly generating an IV, or a salt. You should be, and you then should be passing them to openssl.

To understand how to encrypt this correctly, see Properly encrypting with AES with CommonCrypto. Once you have something properly encrypted, you should then have a proper key, a salt, and an IV. Hand all of these to enc, using aes-128-cbc (assuming 128-bit AES), and it should work.

EDIT

It's worth stating the obvious here: Encryption/decryption is much easier if you use the same toolkit on both sides. To do what you're trying to do, you really do have to understand the nuts and bolts of both CCCrypt() and OpenSSL, which is why I'm discussing them. Even if you find something that "seems to work," the security can easily be very poor without you realizing it. AES128EncryptWithKey: is an example of this; it looks fine and it "works," but it has several security problems. If possible, I'd either use OpenSSL on both sides, or CCCrypt on both sides.

0

精彩评论

暂无评论...
验证码 换一张
取 消