简体   繁体   中英

AES Encryption and Decryption (JAVA)

I want to check my result of encryption and decryption with this website https://8gwifi.org/CipherFunctions.jsp How do I input in the secret key on the website?

在此处输入图片说明

This is my java code.

public static void main(String[] args) throws Exception {
   final String secretKey = "PASSWORD";

   String originalString = "Java Web Developer";
   String encryptedString = AESUtils.encrypt(originalString, secretKey) ;
   String decryptedString = AESUtils.decrypt(encryptedString, secretKey) ;

   System.out.println("Original : " + originalString);  //Original : Java Web Developer
   System.out.println("Encryption : " + encryptedString);  //Encryption : 43Ak2VHK368eandGGYWX2WvbcimGGAEsPdCSjEatjR0=
   System.out.println("Decryption : " + decryptedString);  //Decryption : Java Web Developer

}



public class AESUtils {
private static SecretKeySpec secretKey;
private static byte[] key;

public static void setKey(String myKey)
{
    MessageDigest sha = null;
    try {
        key = myKey.getBytes("UTF-8");
        sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16);
        secretKey = new SecretKeySpec(key, "AES");

        byte encoded[] = secretKey.getEncoded();

        /*
         * Encodes the specified byte array into a String using Base64 encoding
         * scheme
         */
        String encodedKey = Base64.getEncoder().encodeToString(encoded);
        System.out.println("SecretKey: " + encodedKey);  //SecretKey: ESu3kTBHkd3PaS4p/VzxSQ==

    }
    catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

public static String encrypt(String strToEncrypt, String secret)
{
    try
    {
        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)
    {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

public static String decrypt(String strToDecrypt, String secret)
{
    try
    {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    }
    catch (Exception e)
    {
        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}


}

I tried inputting the secret key with "ESu3kTBHkd3PaS4p/VzxSQ==", But the result of encryption does not match the result of encryption from the java program.

My Java program retured encryption to be 43Ak2VHK368eandGGYWX2WvbcimGGAEsPdCSjEatjR0=

在此处输入图片说明

AES with ECB mode and PKCS5Padding,

public static String encryptAES(String toEncrypt, final String key1, final String key2) throws Exception {
            try {
    
                SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
                KeySpec keySpec = new PBEKeySpec(key1.toCharArray(), key2.getBytes(), 65536, 256);
                SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
                SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    
                return Base64.getEncoder().encodeToString(cipher.doFinal(toEncrypt.getBytes(StandardCharsets.UTF_8)));
            } catch (Exception ex) {
                throw new Exception(ex);
            }
        }
    
        public static String decryptAES(String toDecrypt, final String key1, final String key2) throws Exception {
            try {
    
                SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
                KeySpec keySpec = new PBEKeySpec(key1.toCharArray(), key2.getBytes(), 65536, 256);
                SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
                SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    
                return new String(cipher.doFinal(Base64.getDecoder().decode(toDecrypt)));
            } catch (Exception ex) {
                throw new Exception(ex);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM