import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class TestEncryptionDecryption implements Runnable {

    private Cipher encryptCipherAES;
    private Cipher decryptCipherAES;
    private String paddingAES = "AES/ECB/PKCS5Padding";
    
    public TestEncryptionDecryption() {
        super();
        init();
    }
    
    private void init() {
        byte[] secretKeyBytes = new byte[] {62, 124, -2, -15, 86, -25, 18, -112, 110, 31, 96, 59, 89, 70, 60, 103};
        try {
            SecretKey aesSecretKey = new SecretKeySpec(secretKeyBytes, "AES");
            
            encryptCipherAES = Cipher.getInstance(paddingAES);
            encryptCipherAES.init(Cipher.ENCRYPT_MODE, aesSecretKey);
            
            decryptCipherAES = Cipher.getInstance(paddingAES);
            decryptCipherAES.init(Cipher.DECRYPT_MODE, aesSecretKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        new Thread(new TestEncryptionDecryption()).start();
    }
    
    public void run() {
        System.out.println(getClass().getName());
        
        String passwordToEncrypt = "abcd1234";
        System.out.println("passwordToEncrypt: " + passwordToEncrypt);
        
        System.out.println("Encrypting...");
        byte[] encryptedPassword = encryptPassword(passwordToEncrypt);
        
        System.out.println("Decrypting...");
        String passwordDecrypted = decryptPassword(encryptedPassword);
        System.out.println("Decrypted: " + passwordDecrypted);
        
        System.out.println("Same? " + passwordToEncrypt.equals(passwordDecrypted));
    }
    
    public synchronized byte[] encryptPassword(String password) {
        byte[] encryptedBytes = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            CipherOutputStream cos = new CipherOutputStream(baos, encryptCipherAES);
            ObjectOutputStream oos = new ObjectOutputStream(cos);
            oos.writeObject(password);
            oos.flush();
            oos.close();
            encryptedBytes = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedBytes;
    }

    public synchronized String decryptPassword(byte[] bytePassword) {
        String password = "";
        ObjectInputStream ois = null;
        
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytePassword);
            CipherInputStream cis = new CipherInputStream(bais, decryptCipherAES);
            ois = new ObjectInputStream(cis);

            password = (String)ois.readObject();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException ioexception) {
                // swallow it
            }
        }

        return password;
    }
    
}