/* Ciphers.java Title: Ciphers Author: Voiculescu Description: */ package Ciphers; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.Random; public class Ciphers extends Applet { // constants and variables final char ch_line_feed = (char)10; // line feed (for display needs) final String st_line_feed = ""+(char)10; // int[] two = new int[8]; // 2^8, 2^7,....Set of powers of two int position=0; // Caret position in a text area int [][] hillKey = new int[4][4]; int [][] transKey = new int[7][7]; String keyText = new String(); // Encription key String plainText = new String(); // Text to be encrypted / decrypted String cipherText = new String(); // The result of the encryption/decryption operation int method = 0; // Encryption method (choice index) String keyBit = new String(); // Bitstrings: - Key String plainBit = new String(); // - Plain Text String cipherBit = new String(); // - Cipher Text String message = new String(); // message shown in the statistics text area String memKeyText = new String(); // Stored Values - Key String memPlainText = new String(); // - Plain Text String memCipherText = new String(); // - Cipher Text boolean emptyStore = true; boolean modified = false; // ************************** // member declarations java.awt.TextField textKey = new java.awt.TextField(); java.awt.Label labelKey = new java.awt.Label(); java.awt.Choice choiceMethod = new java.awt.Choice(); java.awt.Label labelMethod = new java.awt.Label(); java.awt.Button bttnEncrypt = new java.awt.Button(); java.awt.TextArea txtAreaKey = new java.awt.TextArea(); java.awt.Label labelKeyBit = new java.awt.Label(); java.awt.TextArea txtPlain = new java.awt.TextArea(); java.awt.TextArea txtAreaPlainBit = new java.awt.TextArea(); java.awt.Label labelPlainBit = new java.awt.Label(); java.awt.Label labelPlain = new java.awt.Label(); java.awt.TextArea txtAreaCipher = new java.awt.TextArea(); java.awt.Label labelCipher = new java.awt.Label(); java.awt.TextArea txtAreaCipherBit = new java.awt.TextArea(); java.awt.Label labelCipherBit = new java.awt.Label(); java.awt.TextArea textArea1 = new java.awt.TextArea(); java.awt.Button bttnRunTestKey = new java.awt.Button(); java.awt.Label label1 = new java.awt.Label(); java.awt.TextField textCycKey = new java.awt.TextField(); java.awt.Button bttnRunTestPlain = new java.awt.Button(); java.awt.Label label2 = new java.awt.Label(); java.awt.TextField textCycPlain = new java.awt.TextField(); java.awt.Button bttnStore = new java.awt.Button(); java.awt.Button bttnCompare = new java.awt.Button(); java.awt.Panel panel1 = new java.awt.Panel(); java.awt.TextField hill00 = new java.awt.TextField(); java.awt.TextField hill01 = new java.awt.TextField(); java.awt.TextField hill02 = new java.awt.TextField(); java.awt.TextField hill03 = new java.awt.TextField(); java.awt.TextField hill10 = new java.awt.TextField(); java.awt.TextField hill11 = new java.awt.TextField(); java.awt.TextField hill12 = new java.awt.TextField(); java.awt.TextField hill13 = new java.awt.TextField(); java.awt.TextField hill20 = new java.awt.TextField(); java.awt.TextField hill21 = new java.awt.TextField(); java.awt.TextField hill22 = new java.awt.TextField(); java.awt.TextField hill23 = new java.awt.TextField(); java.awt.TextField hill30 = new java.awt.TextField(); java.awt.TextField hill31 = new java.awt.TextField(); java.awt.TextField hill32 = new java.awt.TextField(); java.awt.TextField hill33 = new java.awt.TextField(); java.awt.Label labelHill = new java.awt.Label(); java.awt.Button bttnHelp = new java.awt.Button(); // ****************** boolean isStandalone = false; public Ciphers() { } // Retrieve the value of an applet parameter public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } // Get info on the applet parameters public String[][] getParameterInfo() { return null; } // Get applet information public String getAppletInfo() { return "Applet Information"; } // Initialize the applet public void init() { try { initComponents(); } catch (Exception e) { e.printStackTrace(); } } public void initComponents() throws Exception { // *************************** // the following code sets the frame's initial state textKey.setText("Type your key here"); textKey.setLocation(new java.awt.Point(20, 30)); textKey.setVisible(true); textKey.setBackground(new java.awt.Color(255, 255, 0)); textKey.setSize(new java.awt.Dimension(260, 20)); labelKey.setText("Key (characters)"); labelKey.setLocation(new java.awt.Point(20, 10)); labelKey.setVisible(true); labelKey.setFont(new java.awt.Font("Dialog", 1, 12)); labelKey.setSize(new java.awt.Dimension(340, 20)); choiceMethod.setLocation(new java.awt.Point(400, 30)); choiceMethod.setVisible(true); choiceMethod.setSize(new java.awt.Dimension(190, 20)); choiceMethod.addItem("Vigenere"); choiceMethod.addItem("Hill"); choiceMethod.addItem("Bit Transposition"); choiceMethod.addItem("Char Transposition"); labelMethod.setText("Method"); labelMethod.setLocation(new java.awt.Point(340, 30)); labelMethod.setVisible(true); labelMethod.setFont(new java.awt.Font("Dialog", 1, 12)); labelMethod.setSize(new java.awt.Dimension(50, 20)); bttnEncrypt.setLocation(new java.awt.Point(630, 30)); bttnEncrypt.setLabel("Encrypt"); bttnEncrypt.setVisible(true); bttnEncrypt.setFont(new java.awt.Font("Dialog", 1, 12)); bttnEncrypt.setSize(new java.awt.Dimension(100, 20)); txtAreaKey.setText(" "); txtAreaKey.setLocation(new java.awt.Point(20, 80)); txtAreaKey.setVisible(true); txtAreaKey.setBackground(new java.awt.Color(255, 255, 0)); txtAreaKey.setSize(new java.awt.Dimension(120, 360)); txtAreaKey.setEditable(false); labelKeyBit.setText("Key (bitstring)"); labelKeyBit.setLocation(new java.awt.Point(10, 55)); labelKeyBit.setAlignment(Label.CENTER); labelKeyBit.setVisible(true); labelKeyBit.setFont(new java.awt.Font("Dialog", 1, 12)); labelKeyBit.setSize(new java.awt.Dimension(138, 15)); txtPlain.setText("Plain text here (you may copy and paste)"); txtPlain.setLocation(new java.awt.Point(150, 80)); txtPlain.setVisible(true); txtPlain.setSize(new java.awt.Dimension(310, 160)); txtAreaPlainBit.setLocation(new java.awt.Point(470, 80)); txtAreaPlainBit.setVisible(true); txtAreaPlainBit.setSize(new java.awt.Dimension(123, 160)); txtAreaPlainBit.setEditable(false); labelPlainBit.setText("Plain text (bitstring)"); labelPlainBit.setLocation(new java.awt.Point(460, 56)); labelPlainBit.setAlignment(Label.CENTER); labelPlainBit.setVisible(true); labelPlainBit.setFont(new java.awt.Font("Dialog", 1, 12)); labelPlainBit.setSize(new java.awt.Dimension(140, 20)); labelPlain.setText("Plain text"); labelPlain.setLocation(new java.awt.Point(161, 55)); labelPlain.setVisible(true); labelPlain.setFont(new java.awt.Font("Dialog", 1, 12)); labelPlain.setSize(new java.awt.Dimension(139, 17)); txtAreaCipher.setText(" "); txtAreaCipher.setLocation(new java.awt.Point(150, 280)); txtAreaCipher.setVisible(true); txtAreaCipher.setBackground(new java.awt.Color(255, 175, 175)); txtAreaCipher.setSize(new java.awt.Dimension(310, 160)); txtAreaCipher.setEditable(false); labelCipher.setText("Cipher text"); labelCipher.setLocation(new java.awt.Point(150, 250)); labelCipher.setVisible(true); labelCipher.setFont(new java.awt.Font("Dialog", 1, 12)); labelCipher.setSize(new java.awt.Dimension(140, 20)); txtAreaCipherBit.setLocation(new java.awt.Point(470, 280)); txtAreaCipherBit.setVisible(true); txtAreaCipherBit.setBackground(new java.awt.Color(255, 175, 175)); txtAreaCipherBit.setSize(new java.awt.Dimension(120, 160)); txtAreaCipherBit.setEditable(false); labelCipherBit.setText("Cipher text (bitstring)"); labelCipherBit.setLocation(new java.awt.Point(470, 250)); labelCipherBit.setVisible(true); labelCipherBit.setFont(new java.awt.Font("Dialog", 1, 12)); labelCipherBit.setSize(new java.awt.Dimension(130, 20)); textArea1.setText("Statistics :"); textArea1.setLocation(new java.awt.Point(20, 450)); textArea1.setVisible(true); textArea1.setSize(new java.awt.Dimension(730, 110)); textArea1.setEditable(false); bttnRunTestKey.setEnabled(false); bttnRunTestKey.setLocation(new java.awt.Point(630, 70)); bttnRunTestKey.setLabel("Run Key Test"); bttnRunTestKey.setVisible(true); bttnRunTestKey.setFont(new java.awt.Font("Dialog", 1, 12)); bttnRunTestKey.setSize(new java.awt.Dimension(100, 20)); label1.setEnabled(false); label1.setText("Cycles (2-1000)"); label1.setLocation(new java.awt.Point(600, 100)); label1.setVisible(true); label1.setFont(new java.awt.Font("Dialog", 1, 12)); label1.setSize(new java.awt.Dimension(90, 20)); textCycKey.setEnabled(false); textCycKey.setText("10"); textCycKey.setLocation(new java.awt.Point(700, 100)); textCycKey.setVisible(true); textCycKey.setSize(new java.awt.Dimension(40, 20)); bttnRunTestPlain.setEnabled(false); bttnRunTestPlain.setLocation(new java.awt.Point(630, 130)); bttnRunTestPlain.setLabel("Run Plain Test"); bttnRunTestPlain.setVisible(true); bttnRunTestPlain.setFont(new java.awt.Font("Dialog", 1, 12)); bttnRunTestPlain.setSize(new java.awt.Dimension(100, 20)); label2.setEnabled(false); label2.setText("Cycles (2-1000)"); label2.setLocation(new java.awt.Point(600, 160)); label2.setVisible(true); label2.setFont(new java.awt.Font("Dialog", 1, 12)); label2.setSize(new java.awt.Dimension(90, 20)); textCycPlain.setEnabled(false); textCycPlain.setText("10"); textCycPlain.setLocation(new java.awt.Point(700, 160)); textCycPlain.setVisible(true); textCycPlain.setSize(new java.awt.Dimension(40, 20)); bttnStore.setEnabled(false); bttnStore.setLocation(new java.awt.Point(630, 190)); bttnStore.setLabel("Store"); bttnStore.setVisible(true); bttnStore.setFont(new java.awt.Font("Dialog", 1, 12)); bttnStore.setSize(new java.awt.Dimension(100, 20)); bttnCompare.setEnabled(false); bttnCompare.setLocation(new java.awt.Point(630, 230)); bttnCompare.setLabel("Compare"); bttnCompare.setVisible(true); bttnCompare.setFont(new java.awt.Font("Dialog", 1, 12)); bttnCompare.setSize(new java.awt.Dimension(100, 20)); panel1.setLocation(new java.awt.Point(600, 300)); panel1.setVisible(false); panel1.setBackground(new java.awt.Color(255, 255, 0)); panel1.setLayout(new java.awt.GridLayout(4, 4, 0, 0)); panel1.setSize(new java.awt.Dimension(150, 110)); panel1.add(hill00); panel1.add(hill01); panel1.add(hill02); panel1.add(hill03); panel1.add(hill10); panel1.add(hill11); panel1.add(hill12); panel1.add(hill13); panel1.add(hill20); panel1.add(hill21); panel1.add(hill22); panel1.add(hill23); panel1.add(hill30); panel1.add(hill31); panel1.add(hill32); panel1.add(hill33); hill00.setText("0"); hill00.setVisible(true); hill00.setBackground(new java.awt.Color(255, 255, 0)); hill00.setEditable(false); hill01.setText("0"); hill01.setVisible(true); hill01.setBackground(new java.awt.Color(255, 255, 0)); hill01.setEditable(false); hill02.setText("0"); hill02.setVisible(true); hill02.setBackground(new java.awt.Color(255, 255, 0)); hill02.setEditable(false); hill03.setText("0"); hill03.setVisible(true); hill03.setBackground(new java.awt.Color(255, 255, 0)); hill03.setEditable(false); hill10.setText("0"); hill10.setVisible(true); hill10.setBackground(new java.awt.Color(255, 255, 0)); hill10.setEditable(false); hill11.setText("0"); hill11.setVisible(true); hill11.setBackground(new java.awt.Color(255, 255, 0)); hill11.setEditable(false); hill12.setText("0"); hill12.setVisible(true); hill12.setBackground(new java.awt.Color(255, 255, 0)); hill12.setEditable(false); hill13.setText("0"); hill13.setVisible(true); hill13.setBackground(new java.awt.Color(255, 255, 0)); hill13.setEditable(false); hill20.setText("0"); hill20.setVisible(true); hill20.setBackground(new java.awt.Color(255, 255, 0)); hill20.setEditable(false); hill21.setText("0"); hill21.setVisible(true); hill21.setBackground(new java.awt.Color(255, 255, 0)); hill21.setEditable(false); hill22.setText("0"); hill22.setVisible(true); hill22.setBackground(new java.awt.Color(255, 255, 0)); hill22.setEditable(false); hill23.setText("0"); hill23.setVisible(true); hill23.setBackground(new java.awt.Color(255, 255, 0)); hill23.setEditable(false); hill30.setText("0"); hill30.setVisible(true); hill30.setBackground(new java.awt.Color(255, 255, 0)); hill30.setEditable(false); hill31.setText("0"); hill31.setVisible(true); hill31.setBackground(new java.awt.Color(255, 255, 0)); hill31.setEditable(false); hill32.setText("0"); hill32.setVisible(true); hill32.setBackground(new java.awt.Color(255, 255, 0)); hill32.setEditable(false); hill33.setText("0"); hill33.setVisible(true); hill33.setBackground(new java.awt.Color(255, 255, 0)); hill33.setEditable(false); labelHill.setText("Hill key array"); labelHill.setLocation(new java.awt.Point(600, 420)); labelHill.setAlignment(Label.CENTER); labelHill.setVisible(false); labelHill.setFont(new java.awt.Font("Dialog", 1, 12)); labelHill.setSize(new java.awt.Dimension(150, 20)); bttnHelp.setLocation(new java.awt.Point(630, 270)); bttnHelp.setLabel("Help"); bttnHelp.setVisible(true); bttnHelp.setFont(new java.awt.Font("Dialog", 1, 12)); bttnHelp.setSize(new java.awt.Dimension(100, 20)); setLocation(new java.awt.Point(0, 0)); setLayout(null); setSize(new java.awt.Dimension(767, 600)); setBackground(Color.white); add(textKey); add(labelKey); add(choiceMethod); add(labelMethod); add(bttnEncrypt); add(txtAreaKey); add(labelKeyBit); add(txtPlain); add(txtAreaPlainBit); add(labelPlainBit); add(labelPlain); add(txtAreaCipher); add(labelCipher); add(txtAreaCipherBit); add(labelCipherBit); add(textArea1); add(bttnRunTestKey); add(label1); add(textCycKey); add(bttnRunTestPlain); add(label2); add(textCycPlain); add(bttnStore); add(bttnCompare); add(panel1); add(labelHill); add(bttnHelp); textKey.addTextListener(new java.awt.event.TextListener() { public void textValueChanged(java.awt.event.TextEvent e) { textKeyTextValueChanged(e); } }); choiceMethod.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent e) { choiceMethodItemStateChanged(e); } }); bttnEncrypt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { bttnEncryptActionPerformed(e); } }); txtAreaKey.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent e) { txtAreaKeyKeyPressed(e); } }); txtAreaKey.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent e) { txtAreaKeyFocusGained(e); } public void focusLost(java.awt.event.FocusEvent e) { txtAreaKeyFocusLost(e); } }); txtPlain.addTextListener(new java.awt.event.TextListener() { public void textValueChanged(java.awt.event.TextEvent e) { txtPlainTextValueChanged(e); } }); txtAreaPlainBit.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent e) { txtAreaPlainBitKeyPressed(e); } }); txtAreaPlainBit.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent e) { txtAreaPlainBitFocusGained(e); } public void focusLost(java.awt.event.FocusEvent e) { txtAreaPlainBitFocusLost(e); } }); bttnRunTestKey.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { bttnRunTestKeyActionPerformed(e); } }); bttnRunTestPlain.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { bttnRunTestPlainActionPerformed(e); } }); bttnStore.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { bttnStoreActionPerformed(e); } }); bttnCompare.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { bttnCompareActionPerformed(e); } }); bttnHelp.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { bttnHelpActionPerformed(e); } }); // ********************* } // **************** METHODS ****************** public void paint(Graphics g) //-------------------------------------- Sets the initial status of the applet window -------------------- // Called every time the applet window gets on top //-------------------------------------------------------------------------------------------------------- { two[7] = 128; // powers of two two[6] = 64; // two[5] = 32; // two[4] = 16; // two[3] = 8; // two[2] = 4; // two[1] = 2; // two[0] = 1; keyText = textKey.getText(); //read the text in the Key Field String bitKey = bitString(keyText); // txtAreaKey.setText(bitKey); //show as binary plainText = txtPlain.getText(); //read the input text String bitPlain = bitString(plainText); // txtAreaPlainBit.setText(bitPlain); //show the bits } String Vigenere(int oper) //------------------------------------- Vigenere Encryption / Decryption ---------------------------------- // int oper - Encrypt if oper = 1 // - Decrypt if oper = -1 // Encryption/Decryption aplies to the Plain Text String using the Key String //--------------------------------------------------------------------------------------------------------- { String vigenereText = new String(); int keyLength = keyText.length(); //number of chars in Key int plainLength = plainText.length(); //number of chars in the input text char[] result = new char[plainLength]; //char array to store the result for (int i=0; i>1); // shift right to get a new bit }//end for k for (int k=0; k<7; k++) //compute the product of that bitstring { // with the transposition array ciphBit[k]=0; for (int j=0; j<7; j++) { //the result is stored in ciphBit vector ciphBit[k]=(byte)(ciphBit[k]+((byte)transKey[k][j])*currBit[j]); }//end for j }//end for k for (int k=0; k<7; k++) //compute the numeric value of ciphBit { sum = sum + ciphBit[k]*two[k]; }//end for k BitTransText = BitTransText + (char)sum; //append to the result }//end for i return BitTransText; } String bitString(String myString) //------------------------------------------ Convert a string into a Bitstring -------------------// // // // return value: String (0-s and/or 1-s, every 9th char in the string is a line feed) // // parameter: String myString (String (text) to be converted) // //------------------------------------------------------------------------------------------------// { byte byteValue, last = 1; //one byte of the input string, byte &H01 char[] bitValue = new char[8]; //array of bits of the above byte String strBit = new String(); //value to be returned int myLength = myString.length(); // char current; // for (int i=0; i>1); // shift right to get a new bit }//end for k String strByte = new String(bitValue); // make a small string (8 zeroes or 1-s) strBit = strBit.concat(strByte); // add the small string to the rezult strBit = strBit.concat(st_line_feed); // add a line feed }//end for return strBit; // return the final value }//end bitString //---------------------------------------------------------------------------------------------------- String compareStrings(String initString, String newString) //------------------------------------------ Bitwise Xor of two Strings --------------------------// // // // return value: String (contains 1-s at the positions where the bits were changed) // // parameters: Strings initString and newString(Strings to be compared) // //------------------------------------------------------------------------------------------------// { String result = new String(); int length = 0, padding = 0; byte initChar, newChar, xored; int initLength = initString.length(); int newLength = newString.length(); if (initLength == newLength) length = initLength; else { length = Math.min(initLength,newLength); padding = Math.abs(initLength - newLength); }//end else for (int i=0; i0) { for (int i=0; i>1); // shift right to get a new bit }//end for k }//end for i return result; } void updateHillArray(String keyString) //------------------------------------------------------------------------------------------------// { hill00.setText(""+(int)keyString.charAt(0)); hill01.setText(""+(int)keyString.charAt(1)); hill02.setText(""+(int)keyString.charAt(2)); hill03.setText(""+(int)keyString.charAt(3)); hill10.setText(""+(int)keyString.charAt(4)); hill11.setText(""+(int)keyString.charAt(5)); hill12.setText(""+(int)keyString.charAt(6)); hill13.setText(""+(int)keyString.charAt(7)); hill20.setText(""+(int)keyString.charAt(8)); hill21.setText(""+(int)keyString.charAt(9)); hill22.setText(""+(int)keyString.charAt(10)); hill23.setText(""+(int)keyString.charAt(11)); hill30.setText(""+(int)keyString.charAt(12)); hill31.setText(""+(int)keyString.charAt(13)); hill32.setText(""+(int)keyString.charAt(14)); hill33.setText(""+(int)keyString.charAt(15)); for (int i=0; i<4; i++) { for(int j=0; j<4; j++) { int k = i*4+j; hillKey[i][j] = (int)keyString.charAt(k); }//end for j }//end for i return; } void updateTransArray(String keyString) //------------------------------------------------------------------------------------------------// { char current = (char)0; byte currByte = 0, last = 1; //byte[] currBit = new byte[7]; for (int i=0; i<7; i++) { current = keyString.charAt(i); currByte = (byte)current; for (int k=0; k<7; k++) // for every bit in the above byte { // bitwise AND to get the last bit byte bit = (byte)(last & currByte); // transKey[i][6-k] = bit; // store that bit (1) or (0) currByte = (byte)(currByte>>1); // shift right to get a new bit }//end for k }//end for i return; } int swapLines(int currentLine, int currentColumn) //------------------------------------------------------------------------------------------------// { int newLine=0, currentElement = transKey[currentLine][currentColumn]; if (currentElement == 1) newLine = (currentLine+1)%7; else { for (int i=0; i<7; i++) { if(transKey[i][currentColumn]==1) newLine = i; }//end for }//end else return newLine; } void buildHillKey() //------------------------------------------------------------------------------------------------// { Random keyElement = new Random(); int currentElement = 0; for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { currentElement = 1+Math.abs(keyElement.nextInt())%127; hillKey[i][j] = currentElement; keyText = keyText+(char)currentElement; }//end for j }//end for i textKey.setText(keyText); updateHillArray(keyText); return; } void addMessage(String myString) //------------------------------------------------------------------------------------------------// { message = message + myString + st_line_feed; textArea1.setText(message); // show message return; } void newMessage(String myString) //------------------------------------------------------------------------------------------------// { message = "STATISTICS:"+st_line_feed+myString+st_line_feed; textArea1.setText(message); // show message return; } // ************ EVENTS ************** public void bttnEncryptActionPerformed(java.awt.event.ActionEvent e) // ---------------------------------------------------------------------------------------------------- // Encrypt button //----------------------------------------------------------------------------------------------------- { int operation = 1; //encryption method = choiceMethod.getSelectedIndex(); //method (Vigenere,Hill...) if (method == 0) { keyText = textKey.getText(); //read the text in the Key Field keyText = keyText.trim(); //remove leading or ending white spaces plainText = txtPlain.getText(); //read the input text cipherText = new String(Vigenere(operation)); //encryp using Vigenere txtAreaCipher.setText(cipherText); //show the result String bitCipher = bitString(cipherText); // txtAreaCipherBit.setText(bitCipher); //show the bits }//end if vigenere else if (method == 1) { plainText = txtPlain.getText(); //read the input text cipherText = new String(Hill()); //encrypt using Hill txtAreaCipher.setText(cipherText); //show the result String bitCipher = bitString(cipherText); // txtAreaCipherBit.setText(bitCipher); //show the bits }//end if hill else if (method == 2) { plainText = txtPlain.getText(); //read the input text cipherText = new String(BitTrans()); //encrypt using Bit Transposition txtAreaCipher.setText(cipherText); //show the result String bitCipher = bitString(cipherText); // txtAreaCipherBit.setText(bitCipher); //show the bits }//end if hill else if (method == 3) { plainText = txtPlain.getText(); //read the input text cipherText = new String(CharTrans()); //encrypt using Bit Transposition txtAreaCipher.setText(cipherText); //show the result String bitCipher = bitString(cipherText); // txtAreaCipherBit.setText(bitCipher); //show the bits } bttnStore.setEnabled(true); if (!emptyStore) { bttnRunTestKey.setEnabled(true); // label1.setEnabled(true); // textCycKey.setEnabled(true); // bttnRunTestPlain.setEnabled(true); // label2.setEnabled(true); // textCycPlain.setEnabled(true); } if (!emptyStore & modified) { bttnCompare.setEnabled(true); } } public void bttnStoreActionPerformed(java.awt.event.ActionEvent e) // ---------------------------------------------------------------------------------------------------- // Store button //----------------------------------------------------------------------------------------------------- { int keyLength=keyText.length(); // int plainLength=plainText.length(); // int cipherLength=cipherText.length(); // memKeyText = new String(keyText); memPlainText = new String(plainText); memCipherText = new String(cipherText); bttnStore.setEnabled(false); bttnRunTestKey.setEnabled(true); // label1.setEnabled(true); // textCycKey.setEnabled(true); // bttnRunTestPlain.setEnabled(true); // label2.setEnabled(true); // textCycPlain.setEnabled(true); emptyStore = false; newMessage("___________STORED VALUES___________________________"); addMessage("Method:= "+choiceMethod.getSelectedItem().toUpperCase()); //method (Vigenere,Hill...) addMessage("Key "+keyLength+"bytes="+memKeyText); // addMessage("Plain Text "+plainLength+"bytes="+memPlainText); // addMessage("Cipher Text "+cipherLength+"bytes="+memCipherText); // return; } public void bttnCompareActionPerformed(java.awt.event.ActionEvent e) // ---------------------------------------------------------------------------------------------------- // Compare button //----------------------------------------------------------------------------------------------------- { keyText = textKey.getText(); //read the text in the Key Field plainText = txtPlain.getText(); //read the input text cipherText = txtAreaCipher.getText(); //read the cipher text String keyDif = new String(compareStrings(memKeyText,keyText)); int keyCount = countOnes(keyDif); addMessage("___________COMPARED VALUES________________________"); addMessage("Nr. of bits changed in Key: = "+keyCount); String plainDif = new String(compareStrings(memPlainText,plainText)); int plainCount = countOnes(plainDif); addMessage("Nr. of bits changed in Plain Text: = "+plainCount); String cipherDif = new String(compareStrings(memCipherText,cipherText)); int cipherCount = countOnes(cipherDif); addMessage("Nr. of bits changed in Cipher Text: = "+cipherCount); bttnCompare.setEnabled(false); String bitCipher = bitString(cipherText).replace((char)10,(char)32); String memBitCipher = bitString(memCipherText).replace((char)10,(char)32); String xored = bitString(cipherDif).replace((char)10,(char)32); addMessage("Stored cipher:="+memBitCipher); addMessage("New cipher:="+bitCipher); addMessage("XORed :="+xored); return; } public void bttnRunTestKeyActionPerformed(java.awt.event.ActionEvent e) // ---------------------------------------------------------------------------------------------------- // Run Key Test button //----------------------------------------------------------------------------------------------------- { Random pickBit = new Random(); Random pickByte = new Random(); int keyLength = 0, bytePoz = 0, bitPoz = 0, currValue = 0, countChange = 0; int sqrCntChange = 0, sqrAve = 0; double deviation = 0; int result = 0, operation = 1, poz1=0, next=0; int max = 0, min = 8*memCipherText.length(); int good = 4*memCipherText.length(); String stResult = new String(); StringBuffer newKey = new StringBuffer(); char current, char1, char2; byte currByte, last = 1; byte[] currBit= new byte[8]; int cycles = Integer.valueOf(textCycKey.getText().trim()).intValue(); if (cycles<0) { cycles = 2; return; }//end if if (cycles>1000) cycles = 1000; for (int i=0; i>1); // shift right to get a new bit }//end for k if (currBit[bitPoz]==1) currBit[bitPoz]=0; else if (currBit[bitPoz]==0) currBit[bitPoz]=1; currValue=0; for (int k=0; k<8; k++) { currValue = currValue + currBit[k]*two[k]; }//end for k current = (char)currValue; newKey = new StringBuffer(keyText); newKey.setCharAt(bytePoz,current); //replace the character in the Text Key keyText = new String(newKey); //update the Text Key if (method == 0) cipherText = new String(Vigenere(operation)); else if (method == 1) { updateHillArray(keyText); cipherText = new String(Hill()); } }//end if method < 2 else { poz1 = i%keyLength; next = (i+1)%keyLength; char1 = keyText.charAt(poz1); char2 = keyText.charAt(next); newKey = new StringBuffer(keyText); newKey.setCharAt(poz1,char2); newKey.setCharAt(next,char1); keyText = new String(newKey); textKey.setText(keyText); updateTransArray(keyText); if (method == 2) cipherText = new String(BitTrans()); else if (method == 3) cipherText = new String(CharTrans()); } txtAreaCipher.setText(cipherText); //show the result String cipherDif = new String(compareStrings(memCipherText,cipherText)); int cipherCount = countOnes(cipherDif); if (cipherCount>max) max = cipherCount; if (cipherCount= good) stResult = "GOOD"; else stResult = "POOR"; addMessage("___________KEY TEST RESULTS________________________"); addMessage("Nr. of cycles: = "+cycles); addMessage("Min bits changed per key bit: = "+min); addMessage("Max bits changed per key bit: = "+max); addMessage("Average bits changed per key bit: = "+result); addMessage("Absolute deviation : = "+intPart+"."+decPart); addMessage("Required for good avalanche: = "+good+" changes / key bit"); addMessage("Key test conclusion: "+ stResult); return; } public void bttnRunTestPlainActionPerformed(java.awt.event.ActionEvent e) // ---------------------------------------------------------------------------------------------------- // Run Plain Test button //----------------------------------------------------------------------------------------------------- { Random pickBit = new Random(); Random pickByte = new Random(); int plainLength = 0, bytePoz = 0, bitPoz = 0, currValue = 0, countChange = 0; int sqrCntChange = 0, sqrAve = 0; double deviation = 0; int result = 0, operation = 1; int max = 0, min = 8*memCipherText.length(); int good = 4*memCipherText.length(); String stResult = new String(); char current; byte currByte, last = 1; byte[] currBit= new byte[8]; int cycles = Integer.valueOf(textCycPlain.getText().trim()).intValue(); if (cycles<0) { cycles = 0; return; }//end if if (cycles>1000) cycles = 1000; for (int i=0; i>1); // shift right to get a new bit }//end for k if (currBit[bitPoz]==1) currBit[bitPoz]=0; else if (currBit[bitPoz]==0) currBit[bitPoz]=1; currValue=0; for (int k=0; k<8; k++) { currValue = currValue + currBit[k]*two[k]; }//end for k current = (char)currValue; StringBuffer newPlain = new StringBuffer(plainText); newPlain.setCharAt(bytePoz,current); //replace the character in the Plain Text plainText = new String(newPlain); //update the Plain Text txtPlain.setText(plainText); //update the Plain Text (characters) area if (method==0) cipherText = new String(Vigenere(operation)); // else if (method == 1) cipherText = new String(Hill()); else if (method == 2) cipherText = new String(BitTrans()); else if (method == 3) cipherText = new String(CharTrans()); txtAreaCipher.setText(cipherText); //show the result String cipherDif = new String(compareStrings(memCipherText,cipherText)); int cipherCount = countOnes(cipherDif); if (cipherCount>max) max = cipherCount; if (cipherCount= good) stResult = "GOOD"; else stResult = "POOR"; addMessage("___________PLAIN TEST RESULTS________________________"); addMessage("Nr. of cycles: = "+cycles); addMessage("Min bits changed per plain text bit: = "+min); addMessage("Max bits changed per plain text bit: = "+max); addMessage("Average bits changed per plain text bit: = "+result); addMessage("Absolute deviation : = "+intPart+"."+decPart); addMessage("Required for good avalanche: = "+good+" changes / plain text bit"); addMessage("Plain test conclusion: "+ stResult); return; } public void textKeyTextValueChanged(java.awt.event.TextEvent e) //---------------------------------------------------------------------------------------------------------- // Change in the Key Text (Characters) // Updates the bitstring representation of the Key //---------------------------------------------------------------------------------------------------------- { keyText = textKey.getText(); //read the text in the Key Field String bitKey = bitString(keyText); // txtAreaKey.setText(bitKey); //show as binary if(method == 1) updateHillArray(keyText); else if (method>1) updateTransArray(keyText); txtAreaKey.setCaretPosition(position); // bttnRunTestKey.setEnabled(false); // label1.setEnabled(false); // textCycKey.setEnabled(false); // bttnRunTestPlain.setEnabled(false); // label2.setEnabled(false); // textCycPlain.setEnabled(false); bttnStore.setEnabled(false); bttnCompare.setEnabled(false); if(!emptyStore) modified = true; return; } public void txtPlainTextValueChanged(java.awt.event.TextEvent e) //---------------------------------------------------------------------------------------------------------- // Change in the Plain Text (Characters) // Updates the bitstring representation of the Plain Text //---------------------------------------------------------------------------------------------------------- { updateTxtPlain(); return; } public void updateTxtPlain() { plainText = txtPlain.getText(); //read the input text String bitPlain = bitString(plainText); // txtAreaPlainBit.setText(bitPlain); //show the bits txtAreaPlainBit.setCaretPosition(position); // bttnRunTestKey.setEnabled(false); // label1.setEnabled(false); // textCycKey.setEnabled(false); // bttnRunTestPlain.setEnabled(false); // label2.setEnabled(false); // textCycPlain.setEnabled(false); // bttnStore.setEnabled(false); bttnCompare.setEnabled(false); if(!emptyStore) modified = true; return; } public void txtAreaKeyKeyPressed(java.awt.event.KeyEvent e) //--------------------------------------------------------------------------------------------------------- // A key was pressed in the Key area (bitstring) // Using the INSERT key, the user can change one bit of the encryption key // Updates the text value of the encryption key //--------------------------------------------------------------------------------------------------------- { int key_pressed = e.getKeyCode(); //get the code of the pressed key StringBuffer newKey = new StringBuffer(); if (key_pressed == e.VK_INSERT) //if the pressed key is INSERT { // int index = txtAreaKey.getCaretPosition(); //get the cursor's position position = index; //store it for later use if ((index+1)%9==0) return; //every nineth char in the string is a line feed keyText = textKey.getText(); //read the text in the Key Field keyBit = txtAreaKey.getText(); //read the bitstring key if (position>=keyBit.length()) // {position = position - 1; //if the cursor is beyound the last character index = position;} // int byteIndex = (int)Math.floor(index/9); //the byte that was changed char current = keyBit.charAt(index); //read the current char (0 or 1) if (method<2){ if (current == (char)48) current = (char)49; //if 0, make it 1 else if (current == (char)49) current = (char)48; //if 1, make it 0 String stCurrent = ""+current; //convert it to string txtAreaKey.replaceRange(stCurrent,index,index+1); //replace it in the text area (bitstring area) keyBit = txtAreaKey.getText(); //read the modified bitstring key int startIndex = byteIndex*9, endIndex=8+byteIndex*9; //start bit and end bit of that byte int currValue=0, k; // for (int i=startIndex; i=plainBit.length()) // {position = position - 1; //if the cursor is beyound the last character index = position;} // char current = plainBit.charAt(index); //read the current char (0 or 1) if (current == (char)48) current = (char)49; //if 0, make it 1 else if (current == (char)49) current = (char)48; //if 1, make it 0 String stCurrent = ""+current; //convert it to string txtAreaPlainBit.replaceRange(stCurrent,index,index+1); //replace it in the text area (bitstring) plainBit = txtAreaPlainBit.getText(); //read the modified bitstring plain text int byteIndex = (int)Math.floor(index/9); //the byte that was changed int startIndex = byteIndex*9, endIndex=8+byteIndex*9; //start bit and end bit of that byte int currValue=0, k; // for (int i=startIndex; i 1) { textKey.setVisible(false); labelKey.setVisible(true); keyText = new String(""); //textKey.setText(keyText); cipherText = new String(""); txtAreaCipher.setText(""); txtAreaCipherBit.setText(""); panel1.setVisible(false); labelHill.setVisible(false); labelKey.setText("Transposition Key Array 7x7"); keyText = new String(""+(char)32+(char)8+(char)64+(char)1+(char)16+(char)4+(char)2); textKey.setText(keyText); }//end if //Show key bttnStore.setEnabled(false); bttnRunTestKey.setEnabled(false); // label1.setEnabled(false); // textCycKey.setEnabled(false); // bttnRunTestPlain.setEnabled(false); // label2.setEnabled(false); // textCycPlain.setEnabled(false); emptyStore = true; modified = false; newMessage(""); return; } //---------------------------------------------------------------------------------------------------------- // Show messages to guide the user //---------------------------------------------------------------------------------------------------------- public void txtAreaKeyFocusGained(java.awt.event.FocusEvent e) { labelKeyBit.setForeground(new java.awt.Color(255, 0, 0)); labelKeyBit.setText("Use INSERT key to edit"); return; } public void txtAreaKeyFocusLost(java.awt.event.FocusEvent e) { labelKeyBit.setForeground(new java.awt.Color(0, 0, 0)); labelKeyBit.setText("Key (bitstring)"); return; } public void txtAreaPlainBitFocusGained(java.awt.event.FocusEvent e) { labelPlainBit.setForeground(new java.awt.Color(255, 0, 0)); labelPlainBit.setText("Use INSERT key to edit"); return; } public void txtAreaPlainBitFocusLost(java.awt.event.FocusEvent e) { labelPlainBit.setForeground(new java.awt.Color(0, 0, 0)); labelPlainBit.setText("Plain text (bitstring)"); return; } public void bttnHelpActionPerformed(java.awt.event.ActionEvent e) { try { Help frame = new Help(); frame.initComponents(); frame.setVisible(true); frame.show(); } catch (Exception e1) { e1.printStackTrace(); } return; } // }// end applet