Konda.eu

Java AES Decryption

No comments

Java AES Decryption


Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

A while ago I needed to do some AES decryption in Java.In order to use 128-bit AES key and IV should be exactly 16 characters long \(\) where 8 is a size of UTF-8 character).

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class AESDemo {

    private static final String key = "Ma5(;8F:;2u%X[YW";
    private static final String IV = "*Z^L_=-**d^jbR*=";
    
    private static final String decrypt = "";
    
    public static void main(String[] args) throws Exception {
        SecretKeySpec s = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        IvParameterSpec iv = new IvParameterSpec(IV.getBytes("UTF-8"));
        
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, s, iv);
        
        byte[] original = cipher.doFinal(hexStringToByteArray(decrypt));
        System.out.println(new String(original));
    }
    
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
}

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.