Search code examples
javaencryptionaes

Java AES Encryption - behind the scenes


I have the following JAVA code for string encryption and decryption:

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)));
    }

When I use my class on the string "test" and a secret key ("d%D*G-JaXdRgUkXs") for example, I get:

D+BhlzXKsINiKja6ZsITWQ==

I have tried to make the same encryption (AES/ECB/PKCS5Padding) with the same secret key in an online tool such as https://8gwifi.org/CipherFunctions.jsp, but I get a different result:

Nwha9Dgv9IaN4W39C6c0cQ==

What I am missing?


Solution

  • Try this. You are using SHA-1 algorithm to generate digest and then assigning to it to SecretKeySpec to generate secrete key. this will give you the answer that this website gives.

    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)));
        }
    }
    

    If you run this code you will get below result

    Nwha9Dgv9IaN4W39C6c0cQ==