import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
import javax.crypto.KeyGenerator;
import java.security.SecureRandom;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

public class TestJavaDESCipher {
    
    public TestJavaDESCipher() throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        KeyGenerator keyGen = KeyGenerator.getInstance("DES");
        SecureRandom secRnd = new SecureRandom();
        keyGen.init(secRnd);
        SecretKey secretKey = keyGen.generateKey();
        System.out.println(cipher.getBlockSize()+"");
        System.out.println(cipher.getProvider()+"");
        System.out.println(cipher.getAlgorithm()+"");
        System.out.println(secretKey);
        ObjectOutputStream keyOut = new ObjectOutputStream(new FileOutputStream("BillsSecretKey"));
        keyOut.writeObject(secretKey);
        while( true ) {
            String fileName = JOptionPane.showInputDialog("Enter filename","TestJavaDESCipher.java");
            if( fileName == null ) break;
            String message = "";
            FileInputStream in = new FileInputStream(fileName);
            int next;
            while( (next = in.read()) != -1 ) {
                byte nextByte = (byte)next;
                message += (char)nextByte;
            } // end while
            in.close();
            cipher.init(Cipher.ENCRYPT_MODE,secretKey);
            byte[] messageBytes = message.getBytes();
            byte[] code = cipher.doFinal(messageBytes);
            String codeout = "";
            int count = 0;
            for( int i = 0; i < code.length; i++ ) {
                codeout += convertToHex(code[i]) + " ";
                count++;
                if( count%40 == 0 ) codeout += "\n";
            } // end for
            // Get Secret Key
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream("BillsSecretKey"));
            secretKey = (SecretKey)keyIn.readObject();
            cipher.init(Cipher.DECRYPT_MODE,secretKey);
            String decodeout = "";
            byte[] decode = cipher.doFinal(code);
            for( int i = 0; i < decode.length; i++ ) decodeout += (char)decode[i];
            String out = "Message =\n" + message + "\n\nCoded message =\n"+codeout+ "\n\nDecoded message = \n"+decodeout;
            JTextArea outArea = new JTextArea(out,40,90);
            JOptionPane.showMessageDialog(null,new JScrollPane(outArea));
        } // end while
    }// end JavaDESCipher
    
    public static void main(String[] a) throws Exception {
        new TestJavaDESCipher();
        System.exit(0);
    } // end main
    
    public String convertToHex(byte b) {
        String hex = Integer.toString(b&0xFF,16).toUpperCase();
        if( hex.length() == 1 ) hex = "0" + hex;
        return hex;
    } // end convertToHex
    
} //TestJavaDESCipher
