密码学课程设计作业

Caesar

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CaeserText{
    static JTextArea text1;
    static JTextField text2;

       public static void main(String[] args){
           JFrame f = new JFrame("Text");
           f.setBounds(400, 400, 400, 300);
           f.setLayout(new GridLayout(5,1));
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

           JPanel p1 = new JPanel();
           JPanel p2 = new JPanel();
           JPanel p3 = new JPanel();
           JPanel p4 = new JPanel();
           JPanel p5 = new JPanel();

           p1.setLayout(new FlowLayout());
           p2.setLayout(new FlowLayout());

           JLabel l1 = new JLabel("明/密文");
           p1.add(l1);
               text1 = new JTextArea(3,10);
           text1.setLineWrap(true);
           p1.add(text1);

           JLabel l2 = new JLabel("    密钥");
           p2.add(l2);
           text2 = new JTextField(10);
           p2.add(text2);

           JButton b = new JButton("加密");
           p3.add(b);
           b.addActionListener(new Encode());

           JButton a = new JButton("清空");
           p3.add(a);
           a.addActionListener(new Empty());

           JButton c = new JButton("解密");
           p3.add(c);
           c.addActionListener(new Decode());

           f.getContentPane().add(p4);
           f.getContentPane().add(p1);
           f.getContentPane().add(p2);
           f.getContentPane().add(p5);
           f.getContentPane().add(p3);
      
           f.setVisible(true);

        }
static class Decode implements ActionListener{
    public void actionPerformed(ActionEvent e){
        char[] table = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
                String c = text1.getText();
                String key = text2.getText();
                int num = Integer.parseInt(key);
                String m = "";
                for(int i=0;i<c.length();i++){
            int temp = (int)c.charAt(i);
            if(temp>=97&&temp<=122){
                m+=table[(temp-71-num)%26];
            }
            else
                m+=(char)temp;
                }
                if(m!=null){
                        JOptionPane.showMessageDialog(null,"明文:"+m);
                }
        }
}
static class Empty implements ActionListener{
    public void actionPerformed(ActionEvent e){
        text1.setText("");
        text2.setText("");    
    }
}

static class Encode implements ActionListener{
    public void actionPerformed(ActionEvent e){
        char[] table = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        String m = text1.getText();
        String key = text2.getText();
        int num = Integer.parseInt(key);
        String c = "";
        for(int i=0;i<m.length();i++){
            int temp=(int)m.charAt(i);
            if(temp<=122&&temp>=97){
                c+=table[(temp-97+num)%26];
            }
            else
                c+=(char)temp;
        }
        if(c!=null){
            JOptionPane.showMessageDialog(null,"密文:"+c);
        }
    }
}
}

RC4

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RC4Text{
    static JTextArea text1;
    static JTextField text2;

       public static void main(String[] args){
           JFrame f = new JFrame("Text");
           f.setBounds(400, 400, 400, 300);
           f.setLayout(new GridLayout(5,1));
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

           JPanel p1 = new JPanel();
           JPanel p2 = new JPanel();
           JPanel p3 = new JPanel();
           JPanel p4 = new JPanel();
           JPanel p5 = new JPanel();

           p1.setLayout(new FlowLayout());
           p2.setLayout(new FlowLayout());

           JLabel l1 = new JLabel("明/密文");
           p1.add(l1);
               text1 = new JTextArea(3,10);
           text1.setLineWrap(true);
           p1.add(text1);

           JLabel l2 = new JLabel("    密钥");
           p2.add(l2);
           text2 = new JTextField(10);
           p2.add(text2);

           JButton b = new JButton("加密");
           p3.add(b);
           b.addActionListener(new EDcode());

           JButton a = new JButton("清空");
           p3.add(a);
           a.addActionListener(new Empty());

           JButton c = new JButton("解密");
           p3.add(c);
           c.addActionListener(new EDcode());

           f.getContentPane().add(p4);
           f.getContentPane().add(p1);
           f.getContentPane().add(p2);
           f.getContentPane().add(p5);
           f.getContentPane().add(p3);
      
           f.setVisible(true);

        }
static class EDcode implements ActionListener{
    public void actionPerformed(ActionEvent e){
        int[] s = new int[256];
        for(int i=0;i<256;i++){
            s[i]=i;
        }
        int j=0;
        int temp=0;
                String in = text1.getText();
                String key = text2.getText();
        String out = "";
        for(int i=0;i<256;i++){
            j=(j+s[i]+(int)key.charAt(i%key.length()))%256;
            temp=s[i];
            s[i]=s[j];
            s[j]=temp;
        }
        int i=0,t=0;j=0;
        for(int k=0;k<in.length();k++){
            i=(i+1)%256;
            j=(j+s[i])%256;
            temp=s[i];
            s[i]=s[j];
            s[j]=temp;
            t=(s[i]+s[j])%256;
            out+=(char)(s[t]^(int)in.charAt(k));
        }
                if(out!=null){
                        JOptionPane.showMessageDialog(null,out);
                }
        }
}
static class Empty implements ActionListener{
    public void actionPerformed(ActionEvent e){
        text1.setText("");
        text2.setText("");    
    }
}
}

 

猜你喜欢

转载自www.cnblogs.com/harmonica11/p/12409080.html
今日推荐