I want to write an AES encryption method that should be equivalent to mysql aes_encrypt.
I try to write but it's not correct, because mysql is nor giving proper data.
What should I do to get it correct?
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encryptedTextBytes = cipher.doFinal(message .getBytes("UTF-8"));
String k = new String(encryptedTextBytes);
System.out.println("KKKKK"+k);
解决方案
MySQL's implementation of AES gives headaches to a lot of people. It'a mainly because of how MySQL processes the encryption key. The encryption key gets broken into 16-byte blocks and MySQL will XOR the bytes from one block with the bytes in the preceding block. If the user provided key happens to be less than 16 bytes in length then the key is essentially padded with null bytes to get up to 16 bytes. That's how the key is processed by MySQL's aes_encrypt().
The value which is to be encrypted is also processed, by padding the data using PKCS7. You can learn all about PKCS7 @ http://en.wikipedia.org/wiki/Padding_%28cryptography%29#PKCS7 but all it does is pad the input data so it's in 16-byte blocks. The byte the data gets padded with is equivalent to the number of padding bytes that will be added.
Long story short you need process the encryption key the way MySQL does and also pad your input data using PKCS7.