// 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.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AES {
    private Cipher aes;
    private SecretKey aeskey;
    public AES(String key) {
        try {
            Security.addProvider(new com.sun.crypto.provider.SunJCE());
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            aes = Cipher.getInstance("AES");
            aeskey = new SecretKeySpec(key.getBytes(), "AES");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    private byte[] encrypt(byte[] plaindata) throws Exception {
        aes.init(Cipher.ENCRYPT_MODE, aeskey);
        return aes.doFinal(plaindata);
    }
    private byte[] decrypt(byte[] cipherdata) throws Exception {
        aes.init(Cipher.DECRYPT_MODE, aeskey);
        return aes.doFinal(cipherdata);
    }
    public static void main(String[] args) throws Exception {
        AES ec = new AES("hemmelighemmelig");
        String s1 = "Dette er en test";
        byte[] b1 = ec.encrypt(s1.getBytes());
        byte[] b2 = ec.decrypt(b1);
        String s2 = new String(b2);
        System.out.println(s2);
    }
}
