I am implementing aes encryption in mysql and same thing I am doing using my java code. Below is my mysql script to encrypt the data. This AES_ENCRYPT
function takes column_name as 1st parameter, encryption_key as 2nd and ivparam as 3rd parameter. It doesn't take any salt bytes.
SET session block_encryption_mode = 'aes-128-cbc';
SET @key_str = LEFT(UNHEX(SHA2('My secret passphrase',256)),16);
AES_ENCRYPT(COLUMN_NAME,@key_str,LEFT(UNHEX(SHA2(IV,256)),16))
When I try to do same thing via java
code, PBEKeySpec
constructor has mandatory saltbytes parameter, which results in different cipher text from mysql and java. I am trying to generate same cipher using mysql and java
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALT.getBytes(), 2, 128);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);