gpt4 book ai didi

用于加密/解密的 Java 函数,如 Mysql 的 AES_ENCRYPT 和 AES_DECRYPT

转载 作者:可可西里 更新时间:2023-11-01 07:33:40 25 4
gpt4 key购买 nike

有什么办法可以得到和MySQL一样的结果

SELECT AES_ENCRYPT("text", "key") 

使用 Java 函数?

如果可能的话,模拟 AES_DECRYPT 的另一个函数是什么。

最佳答案

如果需要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;
}

它收到一个标准的十六进制格式字符串但可变并返回密码。用 main 方法测试

    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

厄瓜多尔伊瓦拉的问候

关于用于加密/解密的 Java 函数,如 Mysql 的 AES_ENCRYPT 和 AES_DECRYPT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4888176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com