// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.FileInputStream;
import java.io.InputStream;

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

public class ReadAES {
    public static void main(String[] args) throws Exception {
        byte[] cipher = new byte[10000];
        InputStream is = new FileInputStream("C:\\aes.dat");
        int cipherlen = is.read(cipher);
        is.close();
        String key = "Top hemmelig key";
        byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
        String data = "Top hemmelige data";
        Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
        aes.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("UTF-8"), "AES"), new IvParameterSpec(iv));
        String newdata = new String(aes.doFinal(cipher, 0, cipherlen), "UTF-8");
        if(!newdata.equals(data)) {
            System.out.println("Oops");
        }
    }
}
