gpt4 book ai didi

Java AES 加密 - 幕后

转载 作者:行者123 更新时间:2023-12-02 01:33:03 29 4
gpt4 key购买 nike

我有以下用于字符串加密和解密的JAVA代码:

public class AES {

private SecretKeySpec setKey(String myKey)
{
try {
byte[] key = myKey.getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
return secretKey;
}
catch (NoSuchAlgorithmException e) {
return null;
}
catch (UnsupportedEncodingException e) {
return null;
}
}

synchronized public String encrypt(String strToEncrypt, String secret)
{
try
{
SecretKeySpec secretKey = setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
}
catch (Exception e)
{
return null;
}
return null;
}

synchronized public String decrypt(String strToDecrypt, String secret) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
{
SecretKeySpec secretKey = setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
}

例如,当我在字符串“test”和 key (“d%D*G-JaXdRgUkXs”)上使用我的类时,我得到:

D+BhlzXKsINiKja6ZsITWQ==

我尝试在在线工具(如https://8gwifi.org/CipherFunctions.jsp)中使用相同的 key 进行相同的加密(AES/ECB/PKCS5Padding) ,但我得到了不同的结果:

Nwha9Dgv9IaN4W39C6c0cQ==

我缺少什么?

最佳答案

试试这个。您使用 SHA-1 算法生成摘要,然后将其分配给 SecretKeySpec 以生成 secret key 。这将为您提供本网站给出的答案。

public class Main {


public static void main(String[] args) {
Main main = new Main();

System.out.println(main.encrypt("test","d%D*G-JaXdRgUkXs"));

}
private SecretKeySpec setKey(String myKey)
{
try {
byte[] key = myKey.getBytes("UTF-8");

key = Arrays.copyOf(key, 16);
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
return secretKey;
}
catch (UnsupportedEncodingException e) {
return null;
}
}

synchronized public String encrypt(String strToEncrypt, String secret)
{
try
{
SecretKeySpec secretKey = setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
}
catch (Exception e)
{
return null;
}
}

synchronized public String decrypt(String strToDecrypt, String secret) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
{
SecretKeySpec secretKey = setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
}
}

如果运行此代码,您将得到以下结果

Nwha9Dgv9IaN4W39C6c0cQ==

关于Java AES 加密 - 幕后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57544735/

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