- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- import java.applet.Applet;
- public class Encrypt extends Applet implements ActionListener {
- JTextField plaintext, encode, decode, multiplier, modulus;
- JTextField[] privatekey, publickey;
- JButton record, encodebutton, decodebutton;
- Color bck = new Color(255,255,223);
- Font fnt = new Font("TimesRoman", Font.BOLD, 16);
- static final long serialVersionUID = 11L;
- JLabel l1;
- public void init() {
- privatekey = new JTextField[8];
- setLayout(new GridLayout(14,1,10,10));
- setBackground(bck);
- add(l1 = new JLabel("1. Enter Private Key"));
- //l1.setFont(fnt);
- JPanel p = new JPanel();
- p.setLayout(new GridLayout(1,8));
- p.setBackground(bck);
- p.add(privatekey[0] = new JTextField("3"));
- p.add(privatekey[1] = new JTextField("5"));
- p.add(privatekey[2] = new JTextField("9"));
- p.add(privatekey[3] = new JTextField("18"));
- p.add(privatekey[4] = new JTextField("40"));
- p.add(privatekey[5] = new JTextField("83"));
- p.add(privatekey[6] = new JTextField("157"));
- p.add(privatekey[7] = new JTextField("317"));
- add(p);
- add(new JLabel("2. Enter Multiplier and Modulus and Press Record"));
- JPanel q = new JPanel();
- q.setLayout(new FlowLayout());
- q.setBackground(bck);
- q.add (new JLabel ("Multiplier"));
- q.add (multiplier = new JTextField("4571"));
- q.add (new JLabel ("Modulus"));
- q.add (modulus = new JTextField("7187"));
- q.add (new JLabel (""));
- q.add (record = new JButton("Record"));
- record.addActionListener(this);
- add(q);
- add (new JLabel("Public Key"));
- JPanel z = new JPanel();
- z.setLayout(new GridLayout(1,8));
- z.setBackground(bck);
- publickey = new JTextField[8];
- for (int i=0 ; i<8 ; i++)
- z.add(publickey[i] = new JTextField(""));
- add(z);
- add(new JLabel("3. Enter Text Message"));
- add(plaintext=new JTextField(""));
- JPanel r = new JPanel();
- r.setLayout(new GridLayout(1,4));
- r.setBackground(bck);
- r.add(new JLabel(""));
- r.add(new JLabel("4. Press Encrypt"));
- r.add(new JLabel(""));
- r.add(encodebutton = new JButton("Encrypt"));
- encodebutton.addActionListener(this);
- add(r);
- add(new JLabel("Encoded Message"));
- add(encode = new JTextField(""));
- JPanel t = new JPanel();
- t.setLayout(new GridLayout(1,4));
- t.setBackground(bck);
- t.add(new JLabel(""));
- t.add(new JLabel("5. Press Decrypt"));
- t.add(new JLabel(""));
- t.add(decodebutton = new JButton("Decrypt"));
- decodebutton.addActionListener(this);
- add(t);
- add(new JLabel("Decoded Message"));
- add(decode = new JTextField(""));
- }
- public int getInt(JTextField t) { return Integer.parseInt(t.getText()); }
- public void setInt(int i, JTextField t) { t.setText(String.valueOf(i)); }
- public int modulo (int i, int j) { return i - (i/j)*j; }
- public void superIncrease() {
- int sum = getInt(privatekey[0]);
- for (int i=1 ; i < 8 ; i++) {
- if (sum >= getInt(privatekey[i])) setInt(sum+1, privatekey[i]);
- sum += getInt(privatekey[i]);
- }
- if (getInt(modulus) <= getInt(privatekey[7]))
- setInt(getInt(privatekey[7])+1, modulus);
- }
- public void computePublicKey() {
- for (int i=0 ; i < 8 ; i++)
- setInt(modulo(getInt(privatekey[i])*getInt(multiplier),
- getInt(modulus)), publickey[i]);
- }
- public int translateChar(char x, JTextField[] k) {
- int sum=0;
- for (int i=0; i < 8; i++) {
- if ((x % 2) == 1) sum += getInt(k[i]);
- x /=2;
- }
- return sum;
- }
- public void computeEncodedText() {
- String s = new String("");
- String m = new String(plaintext.getText());
- int len = m.length();
- if (len <= 0) return;
- s = String.valueOf(translateChar(m.charAt(0), publickey));
- for (int i=1 ; i < len ; i++) {
- s += "+" + String.valueOf(translateChar(m.charAt(i), publickey));
- }
- encode.setText(s);
- }
- public int xlateBack(int x) {
- int m=128;
- int ch=0;
- for (int i=7 ; i >= 0 && x > 0 ; i--) {
- if (getInt(privatekey[i]) <= x) {
- x -= getInt(privatekey[i]);
- ch += m;
- }
- m /= 2;
- }
- return ch;
- }
- public int inverse() {
- int f;
- int a = getInt(multiplier);
- int b = getInt(modulus);
- for (int k=1 ; k < b ; k++) {
- f = 0;
- for (int j=0 ; j < b ; j++)
- if (modulo(k*modulo(a*j,b), b) == j) f += 1; else break;
- if (f == b) return k;
- }
- return 0;
- }
- public void computeDecodeText(String s) {
- String str = new String("");
- int tok;
- int inv = inverse();
- try {
- StringTokenizer t = new StringTokenizer(s, "+");
- while (true) {
- tok = Integer.parseInt(t.nextToken());
- str += (char)xlateBack(modulo(inv*tok, getInt(modulus)));
- }
- }
- catch (NoSuchElementException e) {}
- decode.setText(str);
- }
- public void actionPerformed (ActionEvent evt) {
- for (int i=0 ; i < 8 ; i++) {
- if (evt.getSource() == privatekey[i]) {
- superIncrease();
- return;
- }
- }
- if (evt.getSource() == record) computePublicKey();
- else if (evt.getSource() == encodebutton) computeEncodedText();
- else if (evt.getSource() == decodebutton)
- computeDecodeText(encode.getText());
- }
- public void processEvent (AWTEvent evt) {
- if (((WindowEvent)evt).getNewState() == WindowEvent.WINDOW_CLOSED)
- System.out.println("Yikes");
- }
- }
Raw Paste