AES Encryption with CBC #972
Replies: 3 comments
-
You can't overcome the cipher and block mode requirements. IV have to be 16 bytes for AES. If happen your key is 16 bytes as well that would work, otherwise you have to transform it to keep the AES and block mode requirements. |
Beta Was this translation helpful? Give feedback.
-
One option is to use PBKDF to derive key from password, like this: let key = try! PKCS5.PBKDF2(password: "secret".bytes, salt: iv.bytes, iterations: 4096, keyLength: 16, variant: .md5).calculate() |
Beta Was this translation helpful? Give feedback.
-
If anyone else is facing the same issue. try with below code. func aesEncrypt(value: String, key: String) -> String { do { let encryptedData = Data(encrypted) } catch { |
Beta Was this translation helpful? Give feedback.
-
Description
Hey, I just had a quick question regarding AES encryption. So I get my key from the backend along with the padding. And I need to pass the data I need to encrypt with the key & padding from backend. I use the following code in Android:
So, let's say:
And when I try to encrypt:
This is the corresponding function in JS:
Steps to reproduce
Now my requirement is that I need to pass the key as iv. But I keep getting
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: CryptoSwift.BlockMode.Error.invalidInitializationVector exception.
I understand that the iv needs to be kept within the AES bound size - 16bytes. But not sure how to overcome this.
Sample code to reproduce the issue:
Beta Was this translation helpful? Give feedback.
All reactions