Java AES 加密 - 幕后

Java AES Encryption - behind the scenes

我有以下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”和密钥(“d%D*G-JaXdRgUkXs") 例如,我得到:

D+BhlzXKsINiKja6ZsITWQ==

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

Nwha9Dgv9IaN4W39C6c0cQ==

我错过了什么?

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

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==