有没有办法获得与MySQL相同的结果
SELECT AES_ENCRYPT("text", "key")
使用Java函数?
如果可能的话,模拟AES_DECRYPT的另一个功能是什么。
答案 0 :(得分:1)
好的,我已经设法让它像这样工作。
MySQL查询:
SELECT HEX(aes_encrypt("password", "0123456789012345"));
Java函数:
public static String aes_encrypt(String password, String strKey) {
try {
byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);
SecretKey key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cleartext = password.getBytes("UTF-8");
byte[] ciphertextBytes = cipher.doFinal(cleartext);
return new String(Hex.encodeHex(ciphertextBytes));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} return null;
}
答案 1 :(得分:1)
如果你需要解密算法的代码就在这里JAVA
public static String aes_decrypt(String passwordhex, String strKey) throws Exception {
try {
byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);
SecretKey key = new SecretKeySpec(keyBytes, "AES");
Cipher decipher = Cipher.getInstance("AES");
decipher.init(Cipher.DECRYPT_MODE, key);
char[] cleartext = passwordhex.toCharArray();
byte[] decodeHex = Hex.decodeHex(cleartext);
byte[] ciphertextBytes = decipher.doFinal(decodeHex);
return new String(ciphertextBytes);
} catch (Exception e) {
e.getMessage();
}
return null;
}
它收到一个标准的十六进制格式字符串但是变量并返回密码。用主方法
进行测试 System.out.println(aes_encrypt("your_string_password", "your_string_key"));
System.out.println(aes_decrypt("standard_hex_format_string ", "your_string_key"));
仅用加密进行第一次测试,然后只用解密。 顺便说一下,你必须安装' commons-codec-1.6.jar'所以你可以使用Hex类 http://commons.apache.org/proper/commons-codec/download_codec.cgi
来自厄瓜多尔的问候,Ibarra