⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 secret.java

📁 放射密码java实现
💻 JAVA
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.border.*;import java.io.*;import java.math.*;import java.awt.Dialog;public class secret implements ActionListener{static JFrame f=null;       JDialog d;     // JPasswordField passwd1, passwd2, passwd3;    JButton b1, b2;    public secret(){        f=new JFrame("文件加密和解密系统");         Icon i1 = new ImageIcon("images/1.gif");         Container contentPane = f.getContentPane();         JPanel p1 = new JPanel();        JLabel j = new JLabel(i1);        p1.add(j);        JPanel p2 = new JPanel();        p2.setLayout(new GridLayout(1, 2));        b1 = new JButton("加密");        b1.addActionListener(this);        b2 = new JButton("解密");        b2.addActionListener(this);        p2.add(b1);p2.add(b2);        contentPane.add(p1, BorderLayout.NORTH);        contentPane.add(p2, BorderLayout.CENTER);        f.setSize(340,165);        f.setVisible(true);       f.addWindowListener(new WindowAdapter() {           public void windowClosing(WindowEvent evt) {               // f.dispose();               System.exit(0);           }       });    }    public void actionPerformed(ActionEvent e) {        String cmd = e.getActionCommand();        if (cmd.equals("加密")) {new jiami();}        else new jiemi();    }}class jiami implements ActionListener{    JDialog d;    JFrame f;  JComboBox combo1,combo2;   JTextArea area1;  JScrollPane jsp;    JButton b3,b4;    public jiami(){    f=new JFrame("数据加密");        Container contentPane = f.getContentPane();        JPanel p1 = new JPanel();       area1=new JTextArea(5,36);       jsp = new JScrollPane(area1);       p1.add(jsp);       p1.setBorder(BorderFactory.createTitledBorder(               BorderFactory.createLineBorder(Color.blue, 2), "明文加密",               TitledBorder.CENTER, TitledBorder.TOP));       JPanel p2 = new JPanel();       p2.setLayout(new GridLayout(2, 2));     String[] data1={"1","3","5","7","9"};      combo1=new JComboBox(data1);      combo1.setEditable(true);      ComboBoxEditor editor1=combo1.getEditor();      combo1.configureEditor(editor1,"请选择或直接输入数据");      String[] data2={"0","1","2","3","4","5","6","7","8","9"};      combo2=new JComboBox(data2);      combo2.setEditable(true);      ComboBoxEditor editor2=combo2.getEditor();      combo2.configureEditor(editor2,"请选择或直接输入数据");      JLabel label1=new JLabel("key1:",JLabel.CENTER);      JLabel label2=new JLabel("key2:",JLabel.CENTER);      p2.add(label1);      p2.add(combo1);      p2.add(label2);      p2.add(combo2);      p2.setBorder(BorderFactory.createTitledBorder(             BorderFactory.createLineBorder(Color.blue, 2), "密钥",             TitledBorder.CENTER, TitledBorder.TOP));       JPanel p3=new JPanel();       p2.setLayout(new GridLayout(1, 2));       b3=new JButton("确定");       b3.addActionListener(this);       p3.add(b3);       b4=new JButton("退出");       b4.addActionListener(this);       p3.add(b4);        contentPane.add(p1, BorderLayout.NORTH);        contentPane.add(p2, BorderLayout.CENTER);         contentPane.add(p3, BorderLayout.SOUTH);       f.getRootPane().setDefaultButton(b3);       JMenuBar bar=new JMenuBar();           JMenu file=new JMenu("文件");           JMenu about=new JMenu("关于");           JMenuItem aboutsecret=new JMenuItem("关于…");           JMenuItem open =new JMenuItem("打开");           JMenuItem save =new JMenuItem("保存");           file.add(open);           file.add(save);           about.add(aboutsecret);           bar.add(file);           bar.add(about);           f.setJMenuBar(bar);           open.addActionListener(this);           save.addActionListener(this);           aboutsecret.addActionListener(this);       f.setSize(435,280);            f.setVisible(true);    }    public void actionPerformed(ActionEvent e) {        String cmd = e.getActionCommand();        String string=new String();        String string2=new String();        String string3=new String();        String string4=new String();        String string5=new String();        if (cmd.equals("确定")){            int a = Integer.parseInt(combo1.getSelectedItem().toString());            int b = Integer.parseInt(combo2.getSelectedItem().toString());            String c = area1.getText();            char s[] = new char[40];            int num[] = new int[40];            s = c.toCharArray();                for (int i =0, j = 0,k=0; i < s.length; i++) {                    if (s[i] >= 'a' && s[i] <= 'z') {                        num[i] = convertchar(s[i]);                        num[i] = (num[i] * a + b) % 26;                        s[i] = (char)convertnum(num[i]);                    }                    if (s[i] >= 'A' && s[i] <= 'Z') {                        num[i] = convertCHAR(s[i]);                        num[i] = (num[i] * a + b) % 26;                        s[i] = (char)convertNUM(num[i]);                    }                }                for (int n = 0; n < s.length; n++) {                        string = string +s[n];                }                area1.setText(string);            }     if (cmd.equals("打开")){        FileDialog fd=new FileDialog(f,"open",FileDialog.LOAD);        fd.setVisible(true);        string2=fd.getDirectory()+fd.getFile();        if (fd.getDirectory()==null){        area1.setText("");        }        else {            String fileName=string2;            String line;            try{            BufferedReader in=new BufferedReader(new FileReader(fileName));            line=in.readLine();            while(line!=null){            string4=string4+line;            line=in.readLine();            }            in.close();            }        catch(IOException iox){JOptionPane.showMessageDialog(d, "读取"+fileName+"时出错!");}        area1.setText(string4);        }        }    if (cmd.equals("保存")){        FileDialog fd=new FileDialog(f,"open",FileDialog.SAVE);        fd.setVisible(true);        string3=fd.getDirectory()+fd.getFile();        if (fd.getDirectory()==null){     //  area1.setText("");       }       else {           String fileName = string3;           string5=area1.getText();           try{               BufferedWriter out = new BufferedWriter(new FileWriter(fileName));               out.write(string5);               out.close();           }           catch(IOException iox){JOptionPane.showMessageDialog(d, "写"+fileName+"时出错!");}       }        }   if(cmd.equals("关于…")){   JOptionPane.showMessageDialog(d, "版权所有@防冒必究"+"  "+"2007");   }        if (cmd.equals("退出")) f.dispose();    }public int convertnum(int a){        int b=a+97;        return b;}public int convertchar(char b){        int a=(int)b-97;            return a;    }public static void main(String args[]) {        new secret();    }public int convertCHAR(char b){    int a=(int)b-65;    return a;    }public int convertNUM(int a){    int b=(char)(a+65);          return b;}} class jiemi implements ActionListener{     JDialog d;       JFrame f;     JComboBox combo1,combo2;      JTextArea area1;      JScrollPane jsp;       JButton b3,b4;       public jiemi(){       f=new JFrame("数据解密");           Container contentPane = f.getContentPane();           JPanel p1 = new JPanel();          area1=new JTextArea(5,36);            jsp = new JScrollPane(area1);          p1.add(jsp);          p1.setBorder(BorderFactory.createTitledBorder(                  BorderFactory.createLineBorder(Color.blue, 2), "密文解密",                  TitledBorder.CENTER, TitledBorder.TOP));          JPanel p2 = new JPanel();          p2.setLayout(new GridLayout(2, 2));         String[] data1={"1","3","5","7","9"};         combo1=new JComboBox(data1);         combo1.setEditable(true);         ComboBoxEditor editor1=combo1.getEditor();         combo1.configureEditor(editor1,"请选择或直接输入密钥");         String[] data2={"0","1","2","3","4","5","6","7","8","9"};         combo2=new JComboBox(data2);         combo2.setEditable(true);         ComboBoxEditor editor2=combo2.getEditor();         combo2.configureEditor(editor2,"请选择或直接输入密钥");         JLabel label1=new JLabel("key1:",JLabel.CENTER);         JLabel label2=new JLabel("key2:",JLabel.CENTER);         p2.add(label1);         p2.add(combo1);         p2.add(label2);         p2.add(combo2);         p2.setBorder(BorderFactory.createTitledBorder(                BorderFactory.createLineBorder(Color.blue, 2), "密钥",                TitledBorder.CENTER, TitledBorder.TOP));          JPanel p3=new JPanel();          p2.setLayout(new GridLayout(1, 2));          b3=new JButton("确定");          b3.addActionListener(this);          p3.add(b3);          b4=new JButton("退出");          b4.addActionListener(this);          p3.add(b4);           contentPane.add(p1, BorderLayout.NORTH);           contentPane.add(p2, BorderLayout.CENTER);            contentPane.add(p3, BorderLayout.SOUTH);            JMenuBar bar=new JMenuBar();            JMenu file=new JMenu("文件");            JMenu about=new JMenu("关于");            JMenuItem aboutsecret=new JMenuItem("关于…");            JMenuItem open =new JMenuItem("打开");            JMenuItem save =new JMenuItem("保存");            file.add(open);            file.add(save);            about.add(aboutsecret);            bar.add(file);            bar.add(about);            f.setJMenuBar(bar);            open.addActionListener(this);            save.addActionListener(this);            aboutsecret.addActionListener(this);          f.getRootPane().setDefaultButton(b3);          f.setSize(435,280);               f.setVisible(true);       }public void actionPerformed(ActionEvent e) {    String cmd = e.getActionCommand();    String string = new String();    String string2=new String();    String string3=new String();    String string4=new String();    String string5=new String();    if (cmd.equals("确定")) {        int a = Integer.parseInt(combo1.getSelectedItem().toString());        int b = Integer.parseInt(combo2.getSelectedItem().toString());        int q = function(a, 26);        if (q == 0) {            JOptionPane.showMessageDialog(d, "无解密逆元,无法解密!!");        } else {            String c = area1.getText();            char s[] = new char[40];            int num[] = new int[40],f[]=new int[40];            s = c.toCharArray();            for (int i = 0; i < s.length; i++) {                if (s[i] >= 'a' && s[i] <= 'z') {                    num[i] = convertchar(s[i]);                    f[i] = ((num[i]-b)*q)%26;                    s[i] = (char) convertnum(f[i]);                }                if (s[i] >= 'A' && s[i] <= 'Z') {                    num[i] = convertCHAR(s[i]);                    f[i] = ((num[i]-b)*q)%26;                    s[i] = (char) convertNUM(f[i]);                }            }                for (int n =0; n<s.length;n++) {                    string=string+s[n];                }                area1.setText(string);        }    }  if (cmd.equals("打开")){   FileDialog fd=new FileDialog(f,"open",FileDialog.LOAD);   fd.setVisible(true);   string2=fd.getDirectory()+fd.getFile();   if (fd.getDirectory()==null){   area1.setText("");   }   else {       String fileName=string2;       String line;       try{       BufferedReader in=new BufferedReader(new FileReader(fileName));       line=in.readLine();       while(line!=null){       string4=string4+line;       line=in.readLine();       }       in.close();       }   catch(IOException iox){JOptionPane.showMessageDialog(d, "读取"+fileName+"时出错!");}   area1.setText(string4);   }   }if (cmd.equals("保存")){   FileDialog fd=new FileDialog(f,"open",FileDialog.SAVE);   fd.setVisible(true);   string3=fd.getDirectory()+fd.getFile();   if (fd.getDirectory()==null){//  area1.setText("");  }  else {      String fileName = string3;      string5=area1.getText();      try{          BufferedWriter out = new BufferedWriter(new FileWriter(fileName));          out.write(string5);          out.close();      }      catch(IOException iox){JOptionPane.showMessageDialog(d, "写"+fileName+"时出错!");}  }   }   if(cmd.equals("关于…")){  JOptionPane.showMessageDialog(d, "版权所有@防冒必究"+"  "+"2007");  }    if (cmd.equals("退出")) f.dispose();}public int convertnum(int a){        int b=a+97;        return b;}public int convertchar(char b){        int a=(int)b-97;            return a;    }public int convertCHAR(char b){    int a=(int)b-65;    return a;    }public int convertNUM(int a){        int b=a+65;              return b;}public int function (int d,int f ){              int x1=1,x2=0,y1=0,y2=1,x3=f,y3=d,q=0;              int t1,t2,t3,temp;              for(; ;){                      temp=x3;                      if (y3==0) {y2=0;System.out.println("无逆元");break;}                      if (y3==1) break;                      while(x3>=y3){                      q++;                      x3=x3-y3;                      }                      x3=temp;              t1=x1-q*y1;t2=x2-q*y2;t3=x3-q*y3;              x1=y1;x2=y2;x3=y3;              y1=t1;y2=t2;y3=t3;              q=0;              }              if (y2>0) {y2=y2%f;}              if (y2<0) {y2=y2+f;}              System.out.println(y2);          return y2;} }class help {    JFrame f=null; public help(){ f=new JFrame("帮助"); Container contentPane = f.getContentPane(); JPanel p1 = new JPanel(); JLabel j1=new JLabel("欢迎使用本加密、解密系统!"); JLabel j2=new JLabel("下面我简单介绍一下本系统:"); JLabel j3=new JLabel("   本系统主要能对文件进行加密和解密,你可以采取打开文件"); JLabel j4=new JLabel("的方式或自己书写的方式进行进行加密或解密,但是本系统只能"); JLabel j5=new JLabel("英文字母加密,对于其他文字或字符,将不做任何操作"); JLabel j6=new JLabel("(注意:第一个加密密钥必须要和26互为素数)"); JLabel j7=new JLabel("合作愉快"); p1.add(j1);  p1.add(j2);   p1.add(j3);    p1.add(j4);     p1.add(j5);      p1.add(j6);       p1.add(j7);       contentPane.add(p1,BorderLayout.CENTER);       f.setSize(300,200);        f.setVisible(true); }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -