mysql aes 加密算法_AES加密方法相当于MySQL aes_encrypt()函数

bd96500e110b49cbb3cd949968f18be7.png

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值