Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

import java.awt.*; import java.awt.event.*; import java.applet.

*; public class HillCipher extends Applet implements ActionListener { public void init() { setLayout(new BorderLayout()); Panel p1 = new Panel(); p1.setLayout(new GridLayout(4, 4)); x0y0 = new TextField("1", 2); p1.add(x0y0); x0y1 = new TextField("0", 2); p1.add(x0y1); x0y2 = new TextField("0", 2); p1.add(x0y2); x0y3 = new TextField("0", 2); p1.add(x0y3); x1y0 = new TextField("0", 2); p1.add(x1y0); x1y1 = new TextField("1", 2); p1.add(x1y1); x1y2 = new TextField("0", 2); p1.add(x1y2); x1y3 = new TextField("0", 2); p1.add(x1y3); x2y0 = new TextField("0", 2); p1.add(x2y0); x2y1 = new TextField("0", 2); p1.add(x2y1); x2y2 = new TextField("1", 2); p1.add(x2y2); x2y3 = new TextField("0", 2); p1.add(x2y3); x3y0 = new TextField("0", 2); p1.add(x3y0); x3y1 = new TextField("0", 2); p1.add(x3y1); x3y2 = new TextField("0", 2); p1.add(x3y2); x3y3 = new TextField("1", 2); p1.add(x3y3); add("North", p1); Panel p2 = new Panel(); p2.setLayout(new FlowLayout()); input = new TextArea(6, 40); input.setBackground(Color.yellow); p2.add(input); output = new TextArea(6, 40); output.setBackground(Color.green); output.setEditable(false); p2.add(output); add("Center", p2); Panel p3 = new Panel(); p3.setLayout(new FlowLayout()); Button load = new Button("Load");

// defaults to identity matrix

p3.add(load); load.addActionListener(this); Button encrypt = new Button("Encrypt"); p3.add(encrypt); encrypt.addActionListener(this); Button decrypt = new Button("Decrypt"); p3.add(decrypt); decrypt.addActionListener(this); Button clear = new Button("Clear"); p3.add(clear); clear.addActionListener(this); Button clearkey = new Button("Clear Key"); p3.add(clearkey); clearkey.addActionListener(this); add("South", p3); m = new matrix(); inStr = new String(); msg_frag = new int[4]; msg_out = new int[4]; outBuffer = new int[40]; outLength = 0; outMark = 0; loaded = false; } public void actionPerformed(ActionEvent evt) { String s = evt.getActionCommand(); if (s.equals("Load")) { if (outLength > 0) { input.setText(""); for (int ld = 0; ld < outLength; ld++) { input.appendText(outBuffer[ld] + " "); } } loaded = true; output.setText(""); } else if (s.equals("Encrypt")) { m.clear(); m.add(Integer.parseInt(x0y0.getText())); m.add(Integer.parseInt(x0y1.getText())); m.add(Integer.parseInt(x0y2.getText())); m.add(Integer.parseInt(x0y3.getText())); m.add(Integer.parseInt(x1y0.getText())); m.add(Integer.parseInt(x1y1.getText())); m.add(Integer.parseInt(x1y2.getText())); m.add(Integer.parseInt(x1y3.getText())); m.add(Integer.parseInt(x2y0.getText())); m.add(Integer.parseInt(x2y1.getText())); m.add(Integer.parseInt(x2y2.getText())); m.add(Integer.parseInt(x2y3.getText())); m.add(Integer.parseInt(x3y0.getText()));

m.add(Integer.parseInt(x3y1.getText())); m.add(Integer.parseInt(x3y2.getText())); m.add(Integer.parseInt(x3y3.getText())); encrypt(); //debug } else if (s.equals("Decrypt")) { m.clear(); m.add(Integer.parseInt(x0y0.getText())); m.add(Integer.parseInt(x0y1.getText())); m.add(Integer.parseInt(x0y2.getText())); m.add(Integer.parseInt(x0y3.getText())); m.add(Integer.parseInt(x1y0.getText())); m.add(Integer.parseInt(x1y1.getText())); m.add(Integer.parseInt(x1y2.getText())); m.add(Integer.parseInt(x1y3.getText())); m.add(Integer.parseInt(x2y0.getText())); m.add(Integer.parseInt(x2y1.getText())); m.add(Integer.parseInt(x2y2.getText())); m.add(Integer.parseInt(x2y3.getText())); m.add(Integer.parseInt(x3y0.getText())); m.add(Integer.parseInt(x3y1.getText())); m.add(Integer.parseInt(x3y2.getText())); m.add(Integer.parseInt(x3y3.getText())); m.det(); m.inverse(); decrypt(); } else if (s.equals("Clear Key")) { x0y0.setText("0"); x0y1.setText("0"); x0y2.setText("0"); x0y3.setText("0"); x1y0.setText("0"); x1y1.setText("0"); x1y2.setText("0"); x1y3.setText("0"); x2y0.setText("0"); x2y1.setText("0"); x2y2.setText("0"); x2y3.setText("0"); x3y0.setText("0"); x3y1.setText("0"); x3y2.setText("0"); x3y3.setText("0"); m.clear(); loaded = false; } else if (s.equals("Clear")) { input.setText(""); output.setText(""); for (int zap = 0; zap < 40; zap++) { outBuffer[zap] = 0; } outLength = 0; }

} public void encrypt() { inStr = input.getText(); inStr.toUpperCase(); // this don't work right; not implemented yet in this jdk distro inStr.trim(); output.setText(""); char c = (char)(-1); int pos, fragcount, str_len; pos = fragcount = 0; //padding string w/ Q's if needed str_len = inStr.length(); int pad = str_len % 4; if (pad != 0) { for (int k = 0; k < 4-pad; k++) { inStr = inStr + "Q"; } } for (int j = 0; j < inStr.length(); j++) { c = inStr.charAt(pos++); msg_frag[fragcount++] = convert(c); if (fragcount == 4) // got another chunk to encrypt { msg_out = m.mult(msg_frag); // multiply the input chunk by the key for (int i = 0; i < 4; i++) { outBuffer[outLength++] = msg_out[i]; output.appendText(deconvert(msg_out[i]%26) + ""); } fragcount = 0; } // end if } // end for-loop } // end function public void decrypt() { int i, numSize, inputInt, temp; i = inputInt = numSize = temp = 0; boolean isNeg = false; boolean watcher = false; if (loaded == false) { inStr = input.getText(); String oneInt = new String(""); output.setText(""); char c; while (i < inStr.length()) { c = inStr.charAt(i++); if (c == '-' && watcher == false) { isNeg = watcher = true; }

if (c != ' ') oneInt = oneInt + c; // keep concatenating chars to make input # if (c == ' ' || (i == inStr.length())) // got another number, or hit last on e { watcher = false; try { inputInt = Integer.parseInt(oneInt); } catch (NumberFormatException e) {} if (isNeg) // supposed to convert to -ve numbers, but it don't { temp = (-1) * inputInt; inputInt = temp; isNeg = false; } oneInt = ""; //ok, now enter it as an input array msg_frag[numSize++] = inputInt; if (numSize == 4) // ok, we got another 4 numbers to decrypt { msg_out = m.dcr_mult(msg_frag); for (int ndcr = 0; ndcr < 4; ndcr++) { output.appendText(deconvert(msg_out[ndcr]%26) + ""); } numSize = 0; } // end numSize=4 if } // end c=' ' if } // end while loop } // end loaded-if else // copy from int buffer { while (outMark < outLength) { msg_frag[numSize++] = outBuffer[outMark++]; if(numSize == 4) // ok, got another 4-int chunk to process { numSize = 0; msg_out = m.dcr_mult(msg_frag); for (int cdcr = 0; cdcr < 4; cdcr++) { output.appendText(deconvert(msg_out[cdcr]) + ""); } }

} outMark = 0; } // end loaded-else } // end decrypt function public int convert(char c) { int retval = (int)c - 65; if (retval >= 32) return (retval - 32); else if (retval < 0) return 0; else return retval; } public char deconvert(int i) { return (char)(i + 65); } private private private private private private private private private private private private private private private private TextField TextField TextField TextField TextField TextField TextField TextField TextField TextField TextField TextField TextField TextField TextField TextField x0y0; x0y1; x0y2; x0y3; x1y0; x1y1; x1y2; x1y3; x2y0; x2y1; x2y2; x2y3; x3y0; x3y1; x3y2; x3y3;

private TextArea input; private String inStr; private TextArea output; private private private private private private } class matrix { public matrix() { m_array = new int[4][4]; inv = new double[4][4]; mult = new int[4]; arr3 = new int[3][3]; clear(); } public void clear() { matrix m; int msg_frag[]; int msg_out[]; int outBuffer[]; int outLength, outMark; boolean loaded;

for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { m_array[i][j] = 0; inv[i][j] = 0; } } lasti = lastj = determinant = 0; } public void add(int entry) { m_array[lasti][lastj] = entry; lastj++; if (lastj == 4) { lastj = 0; lasti++; if (lasti == 4) {} } } public int[] mult(int[] factor) { int m_factor = 0; for (int cl = 0; cl < 4; cl++) mult[cl] = 0; // clear out matrix for (int i = 0; i < 4; i ++) { m_factor = 0; for (int j = 0; j < 4; j++) m_factor += (m_array[j][i] * factor[j]); mult[i] = m_factor; } return mult; } public int[] dcr_mult(int[] factor) { double m_factor = 0.0; for (int cl = 0; cl < 4; cl++) mult[cl] = 0; // clear out matrix for (int i = 0; i < 4; i ++) { m_factor = 0; for (int j = 0; j < 4; j++) m_factor += (inv[j][i] * (double)factor[j]); mult[i] = (int)(m_factor + 0.5); } return mult; } public void inverse() { // cofactor matrix has been constructed already; is in inv[][] transpose();

double inverse_var = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (determinant != 0.0) inverse_var = inv[i][j] / (double)determinant; inv[i][j] = inverse_var; } } } public void det() { cofactor(); for (int i = 0; i < 4; i++) { determinant += m_array[i][1] * (int)(inv[i][1]); } } public void transpose() { double x, y; x = y = 0; int c = 0; for (c = 1; c < 4; c++) { x = inv[0][c]; y = inv[c][0]; inv[0][c] = y; inv[c][0] = x; } for (c = 2; c < 4; c++) { x = inv[1][c]; y = inv[c][1]; inv[1][c] = y; inv[c][1] = x; } x = inv[2][3]; y = inv[3][2]; inv[2][3] = y; inv[3][2] = x; } public void cofactor() { double cof = 0; int power = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { minor(i, j); if ((i + j) % 2 == 0) power = 1;

else power = -1; cof = (double)(power * det3()); inv[i][j] = cof; } } } public void minor(int i, int j) { int pi, pj; pi = pj = 0; for (int x = 0; x < 4; x++) // row # { if (x != i) { for (int y = 0; y < 4; y++) // col # { if (y != j) arr3[pi][pj++] = m_array[x][y]; } pj = 0; pi++; } } } public int det3() // hard-coded algorithm >:) { int x, y, det; x = (arr3[0][0] * arr3[1][1] * arr3[2][2]) + (arr3[0][1] * arr3[1][2] * arr3[2][0]) + (arr3[0][2] * arr3[1][0] * arr3[2][1]); y = (arr3[2][0] * arr3[1][1] * arr3[0][2]) + (arr3[2][1] * arr3[1][2] * arr3[0][0]) + (arr3[2][2] * arr3[1][0] * arr3[0][1]); return (x - y); } private private private private private } int m_array[][]; double inv[][]; int arr3[][]; int mult[]; int lasti, lastj, determinant;

You might also like