AES加密方法相当于MySQL aes_encrypt()函数 [英] AES encryption method equivalent to MySQL aes_encrypt() function

查看:938
本文介绍了AES加密方法相当于MySQL aes_encrypt()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个AES加密方法,应该等于mysql aes_encrypt



我尝试写,但它是不正确的,因为mysql也没有提供正确的数据。



我应该怎么做才能正确?



密码密码= 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的AES实现给很多人带来了麻烦。这主要是因为MySQL如何处理加密密钥。加密密钥被分解成16字节的块,MySQL将使来自前一个块中的字节的来自一个块的字节异或。如果用户提供的密钥长度小于16字节,则密钥基本上用空字节填充,最多可以获得16个字节。这是MySQL的aes_encrypt()处理的关键。



还将通过使用PKCS7填充数据来处理要加密的值。您可以了解所有关于PKCS7 @ http://en.wikipedia.org/wiki/ Padding_%28cryptography%29#PKCS7 ,但它所做的只是填充输入数据,因此它是16字节的块。数据被填充的字节等于要添加的填充字节数。



长篇短说,您需要以MySQL的方式处理加密密钥,并使用PKCS7填充输入数据。



请参阅Michael Simmons发布的以下Java代码:
http://info.michael-simons.eu/2011/07/18/mysql-compatible-aes -encryption-decrypt-in-java /


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.

Refer to the following post by Michael Simmons for example code in Java: http://info.michael-simons.eu/2011/07/18/mysql-compatible-aes-encryption-decryption-in-java/

这篇关于AES加密方法相当于MySQL aes_encrypt()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆