Giovanni Palusa
Giovanni Palusa

Reputation: 1247

Different AES encryptors give me different results... why?

I have tried using three different libraries to AES-encrypt a string.

When I use the tool found here I get the following results:

Input: "Test" 
key: "MyEncryptionKey1MyEncryptionKey1" (256 Bit)
ECB mode.

this gives me the output Cidor8Ph7pZqPw0x2AwIKw==

But when i'm using the libraries in Swift I get different results.

Using RNCryptor

When i'm using RNcryptor i'm using the following code:

class func encryptMessage(message: String) throws -> String {
    guard let messageData = message.data(using: .utf8) else { return message }
    let cipherData = RNCryptor.encrypt(data: messageData, withPassword: key)
    return cipherData.base64EncodedString()
}

output: AwF8a+HziYkO4iHdcI3jY8p9QAY461DVgkjkYUFMkuh4A2a8FCfa4RgS9Z37QhJGxIL0Q20RE3BL4nmLQVFOfZmBpj8l0wj9YZgqZmrkxRFYQQ==

Using AESCrypt

When i'm using RNcryptor i'm using the following code:

class func encryptMessageAES(message: String) -> String{
    guard let encryptedData = AESCrypt.encrypt(message, password: key) else { return  message }
    return encryptedData
}

Output: T5/mR8UT/EXeUobPTLhcFA==

Also if i'm using CryptoSwift i'm getting a third result. My co-worker who does Android always gets the same result - matching the web tool.

I am totally new to encryption and I see that i'm doing something wrong. But I can't really realize what. I should also mention that this encryption is only used to not have chat messages in raw strings showing in Firebase, for those who have access to the database.

Upvotes: 1

Views: 435

Answers (2)

Ebbe M. Pedersen
Ebbe M. Pedersen

Reputation: 7488

The definition of AES is quite precise and when things don't work between different implementations it's often due various things build on top of AES. The AES algorithm itself always operates on binary data. The data you encrypt needs to be binary. The key you use to encrypt with, needs to be binary and If an IV is in play, it also needs to be binary.

In all implementations where you provide data to the implementation that are not binary, a choice have been made on how that data is transformed into a format that can be used with AES. Sometimes these transformations are just simple data conversions like hex or base64 decoding, but other times whole new concepts are in play, like deriving encryption keys from passwords.

All of your three examples uses text as input for the Key, and each implementation have made some choice on how to support that.

The first page you link to has chosen to just interpret an ASCII string as a binary key. This is a terrible choice as it (in addition to being incompatible with everything else) effectively eliminates 1-2 bits per bytes of the key, reducing the strength considerable.

In the RNCryptor example you specify the key with withPassword: key. Here the RNCryptor team have chosen to use a PBKDF2 key deriving function to make an actual AES key. This solves a different usecase, where you have an potential weak password that needs stretching to be secure for encryption. If you have an actual key, this is not the way to go.

In the case of AESCrypt you also seems to be providing a password as input. It's not clear how that would be transformed to an actual key.

Upvotes: 2

Dhaval Dobariya
Dhaval Dobariya

Reputation: 473

There is one more value which you’ll have to set in AES which is iv. So try to find that iv in all three libraries. And also try to set same value for iv. And then you may be able to get same results from all libraries.

Upvotes: 0

Related Questions