// 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.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class SpecialSerDeser {
    public static void main(String[] args) throws Exception {
        SensitiveUnprotected o1 = new SensitiveUnprotected("arne", "hemmeligt");
        ObjectOutputStream oosup = new ObjectOutputStream(new FileOutputStream("C:\\ser_up.dat"));
        oosup.writeObject(o1);
        oosup.close();
        ObjectInputStream oisup = new ObjectInputStream(new FileInputStream("C:\\ser_up.dat"));
        SensitiveUnprotected o2 = (SensitiveUnprotected)oisup.readObject();
        oisup.close();
        System.out.println(o1 + " -> " + o2);
        SensitiveProtected.setKey("hemmelighemmelig");
        SensitiveProtected o3 = new SensitiveProtected("arne", "hemmeligt");
        ObjectOutputStream oosp = new ObjectOutputStream(new FileOutputStream("C:\\ser_p.dat"));
        oosp.writeObject(o3);
        oosp.close();
        ObjectInputStream oisp = new ObjectInputStream(new FileInputStream("C:\\ser_p.dat"));
        SensitiveProtected o4 = (SensitiveProtected)oisp.readObject();
        oisp.close();
        System.out.println(o3 + " -> " + o4);
    }
}

class SensitiveUnprotected implements Serializable {
    private String un;
    private String pw;
    public SensitiveUnprotected(String un, String pw) {
        this.un = un;
        this.pw = pw;
    }
    public String getUn() {
        return un;
    }
    public String getPw() {
        return pw;
    }
    public String toString() {
        return "[" + un + "," + pw + "]";
    }
}

class SensitiveProtected implements Serializable {
    private transient String un;
    private transient String pw;
    public SensitiveProtected(String un, String pw) {
        this.un = un;
        this.pw = pw;
    }
    public String getUn() {
        return un;
    }
    public String getPw() {
        return pw;
    }
    public String toString() {
        return "[" + un + "," + pw + "]";
    }
    private void writeObject(ObjectOutputStream oos) throws IOException {
        oos.writeObject(un);
        oos.writeObject(encrypt(pw.getBytes()));
    }
    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        un = (String)ois.readObject();
        pw = new String(decrypt((byte[])ois.readObject()));
    }
    private static String key;
    public static void setKey(String newkey) {
        key = newkey;
    }
    private static byte[] encrypt(byte[] plaindata) {
        try {
            Cipher aes = Cipher.getInstance("AES");
            SecretKey aeskey = new SecretKeySpec(key.getBytes(), "AES");
            aes.init(Cipher.ENCRYPT_MODE, aeskey);
            return aes.doFinal(plaindata);
        } catch (NoSuchAlgorithmException e) {
            return null;
        } catch (NoSuchPaddingException e) {
            return null;
        } catch (InvalidKeyException e) {
            return null;
        } catch (IllegalBlockSizeException e) {
            return null;
        } catch (BadPaddingException e) {
            return null;
        }
    }
    private static byte[] decrypt(byte[] cipherdata) {
        try {
            Cipher aes = Cipher.getInstance("AES");
            SecretKey aeskey = new SecretKeySpec(key.getBytes(), "AES");
            aes = Cipher.getInstance("AES");
            aeskey = new SecretKeySpec(key.getBytes(), "AES");
            aes.init(Cipher.DECRYPT_MODE, aeskey);
            return aes.doFinal(cipherdata);
        } catch (InvalidKeyException e) {
            return null;
        } catch (NoSuchAlgorithmException e) {
            return null;
        } catch (NoSuchPaddingException e) {
            return null;
        } catch (IllegalBlockSizeException e) {
            return null;
        } catch (BadPaddingException e) {
            return null;
        }
    }
}
