我有兩個java程序,一個是打開文本文件的gui。還有一種使用MD5加密數據。我怎麼能結合這兩個,所以我的GUI會顯示文件中的文本和文本的加密版本。使用文件測試MD5的Java測試
GUI
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Editor extends JFrame implements WindowListener, ActionListener {
JTextField fileName;
JTextArea fileBuffer;
JButton load, save, quit;
/** Creates a new instance of Editor */
public Editor() {
this.setLayout(null);
// this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
JLabel label=new JLabel("File Name: ");
label.setBounds(10,30,300,20);
label.setBackground(Color.LIGHT_GRAY);
this.add(label);
fileName=new JTextField();
fileName.setBounds(10,50,290,20);
this.add(fileName);
load=new JButton("Load");
load.setBounds(10,80,80,20);
this.add(load);
save=new JButton("Save");
save.setBounds(110,80,80,20);
this.add(save);
quit=new JButton("Quit");
quit.setBounds(210,80,80,20);
this.add(quit);
fileBuffer=new JTextArea("",10,20);
JScrollPane p=new JScrollPane(fileBuffer);
JPanel panel=new JPanel();
// panel.setLayout(new FlowLayout(FlowLayout.CENTER));
panel.add(p);
panel.setBounds(15, 110,275,210);
this.getContentPane().add(panel);
this.addWindowListener(this);
load.addActionListener(this);
save.addActionListener(this);
quit.addActionListener(this);
}//Constructor Editor
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
if (command.equals("Quit")) dispose();
else if (command.equals("Load")) load();
else if (command.equals("Save")) save();
}
public void windowClosing(WindowEvent e){dispose();}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
void load(){
try{
RandomAccessFile input=new RandomAccessFile(fileName.getText() ,"r");
byte buffer[]=new byte [(int) input.length()];
input.read(buffer);
input.close();
fileBuffer.setText(new String(buffer));
}
catch(IOException e) {System.out.println(e);}
}
void save(){
try{
FileWriter output= new FileWriter(fileName.getText());
output.write(fileBuffer.getText());
output.close();
}
catch(IOException e) {System.out.println(e);}
}
public static void main(String [] args){
Editor edit=new Editor();
edit.setSize(320,320);
edit.setBackground(Color.LIGHT_GRAY);
edit.setTitle("Editor de Texto SWING");
edit.setVisible(true);
}
}
MD5計劃:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class TestMD5
{
private static final char[] CONSTS_HEX = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
public static String encryptionMD5(String token)
{
try
{
MessageDigest msgd = MessageDigest.getInstance("MD5");
byte[] bytes = msgd.digest(token.getBytes());
StringBuilder strbMD5 = new StringBuilder(2 * bytes.length);
for (int i = 0; i < bytes.length; i++)
{
int low = (int)(bytes[i] & 0x0f);
int high = (int)((bytes[i] & 0xf0) >> 4);
strbMD5.append(CONSTS_HEX[high]);
strbMD5.append(CONSTS_HEX[low]);
}
return strbMD5.toString();
}catch (NoSuchAlgorithmException e) {
return null;
}
}
public static void main(String args[])
{
String msg01=new String("12345678910");
String msg02=new String("12345678910 ");
System.out.println("\n\nMD5 Encryption of" +msg01+": "+encryptionMD5(msg01));
System.out.println("MD5 Encryption of "+msg02+":"+encryptionMD5(msg02));
}
}
謝謝,你的建議工作 – Jessica
@Jessica你的歡迎。玩得開心編碼:) – Sanjeev