?? secret.java
字號(hào):
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("文件加密和解密系統(tǒng)"); 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("數(shù)據(jù)加密"); 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,"請(qǐng)選擇或直接輸入數(shù)據(jù)"); 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,"請(qǐng)選擇或直接輸入數(shù)據(jù)"); 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("關(guān)于"); JMenuItem aboutsecret=new JMenuItem("關(guān)于…"); JMenuItem open =new JMenuItem("打開(kāi)"); 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("打開(kāi)")){ 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+"時(shí)出錯(cuò)!");} 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, "寫(xiě)"+fileName+"時(shí)出錯(cuò)!");} } } if(cmd.equals("關(guān)于…")){ JOptionPane.showMessageDialog(d, "版權(quán)所有@防冒必究"+" "+"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("數(shù)據(jù)解密"); 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,"請(qǐng)選擇或直接輸入密鑰"); 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,"請(qǐng)選擇或直接輸入密鑰"); 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("關(guān)于"); JMenuItem aboutsecret=new JMenuItem("關(guān)于…"); JMenuItem open =new JMenuItem("打開(kāi)"); 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, "無(wú)解密逆元,無(wú)法解密!!"); } 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("打開(kāi)")){ 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+"時(shí)出錯(cuò)!");} 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, "寫(xiě)"+fileName+"時(shí)出錯(cuò)!");} } } if(cmd.equals("關(guān)于…")){ JOptionPane.showMessageDialog(d, "版權(quán)所有@防冒必究"+" "+"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("無(wú)逆元");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("歡迎使用本加密、解密系統(tǒng)!"); JLabel j2=new JLabel("下面我簡(jiǎn)單介紹一下本系統(tǒng):"); JLabel j3=new JLabel(" 本系統(tǒng)主要能對(duì)文件進(jìn)行加密和解密,你可以采取打開(kāi)文件"); JLabel j4=new JLabel("的方式或自己書(shū)寫(xiě)的方式進(jìn)行進(jìn)行加密或解密,但是本系統(tǒng)只能"); JLabel j5=new JLabel("英文字母加密,對(duì)于其他文字或字符,將不做任何操作"); JLabel j6=new JLabel("(注意:第一個(gè)加密密鑰必須要和26互為素?cái)?shù))"); 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); }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -