標籤:
//實現產生MD5值import java.io.BufferedInputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class Digest {public Digest() {// TODO Auto-generated constructor stub}/** * @param args */public static StringBuilder check(String path) {// TODO Auto-generated method stubStringBuilder sb = new StringBuilder();byte[] size = null;StringBuilder noAlgorithm=new StringBuilder("無法使用MD5演算法,這可能是你的JAVA虛擬機器版本太低");StringBuilder fileNotFound=new StringBuilder("未能找到檔案,請重新置放檔案路徑");StringBuilder IOerror=new StringBuilder("檔案輸入資料流錯誤");try {MessageDigest md5=MessageDigest.getInstance("MD5");//產生MD5類的執行個體File file = new File(path); //建立檔案執行個體,設定路徑為方法參數FileInputStream fs = new FileInputStream(file); BufferedInputStream bi = new BufferedInputStream(fs); ByteArrayOutputStream bo = new ByteArrayOutputStream(); byte[] b = new byte[bi.available()]; //定義位元組數組b大小為檔案的不受阻塞的可訪問位元組數int i; //將檔案以位元組方式讀到數組b中while ((i = bi.read(b, 0, b.length)) != -1) { } md5.update(b);//執行MD5演算法for (byte by : md5.digest()) {sb.append(String.format("%02X", by));//將產生的位元組MD5值轉換成字串}bo.close();bi.close();} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blockreturn noAlgorithm;} catch (FileNotFoundException e) {// TODO Auto-generated catch blockreturn fileNotFound;} catch (IOException e) {// TODO Auto-generated catch blockreturn IOerror;} return sb;//返回MD5值}}
//產生表單類為主類import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.*;import javax.swing.plaf.metal.MetalLookAndFeel;public class MainFrame extends JFrame implements ActionListener,MouseListener{ JTextField fileSource=new JTextField(36); JTextField produceMD5=new JTextField(36); JTextField showEqual=new JTextField("請在此處輸入源MD5值",36); JButton choiceFile=new JButton("選擇檔案"); JButton createMD5=new JButton("產生MD5"); JButton judgement=new JButton("對比"); JPanel panel; JFileChooser fileChooser=new JFileChooser();public MainFrame() {// TODO Auto-generated constructor stubsuper("MD5工具");//設定主表單的觀感為金屬外觀try {UIManager.setLookAndFeel(new MetalLookAndFeel());} catch (UnsupportedLookAndFeelException e) {// TODO Auto-generated catch blocke.printStackTrace();}//表單布局使用GridBagLayoutGridBagLayout gbl=new GridBagLayout();GridBagConstraints gbc=new GridBagConstraints();panel=new JPanel(gbl);panel.setBorder(BorderFactory.createTitledBorder("xiaohb‘s MD5 check tool"));gbc.fill=GridBagConstraints.BOTH;gbc.weightx=1.0;gbc.weighty=1.0;gbl.setConstraints(fileSource, gbc);panel.add(fileSource);gbc.weightx=0.0;gbc.gridwidth=GridBagConstraints.REMAINDER;gbl.setConstraints(choiceFile, gbc);panel.add(choiceFile);gbc.gridwidth=1;gbl.setConstraints(produceMD5, gbc);panel.add(produceMD5);gbc.weightx=0.0;gbc.gridwidth=GridBagConstraints.REMAINDER;gbl.setConstraints(createMD5, gbc);panel.add(createMD5);gbc.gridwidth=1;gbl.setConstraints(showEqual, gbc);panel.add(showEqual);gbc.weightx=0.0;gbc.gridwidth=GridBagConstraints.REMAINDER;gbl.setConstraints(judgement, gbc);panel.add(judgement);add(panel);//給按鈕添加註冊器showEqual.addMouseListener(this);choiceFile.addActionListener(this);createMD5.addActionListener(this);judgement.addActionListener(this);}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubMainFrame frame=new MainFrame();frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);frame.setSize(350, 200);frame.setResizable(false);frame.setVisible(true);}public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubDigest digest=new Digest();JOptionPane prompt=new JOptionPane();if(e.getSource()==choiceFile){fileChooser.showOpenDialog(this);fileSource.setText(fileChooser.getSelectedFile().toString());//顯示選擇的檔案名稱}else if(e.getSource()==createMD5){produceMD5.setText((digest.check(fileSource.getText())).toString());//產生的MD5值顯示在文本地區內}else if(e.getSource()==judgement){//判斷MD5值是否相同if(produceMD5.getText().equalsIgnoreCase(showEqual.getText())){prompt.showMessageDialog(this, "兩個MD5值相同,檔案安全!");}else{prompt.showMessageDialog(this, "兩個MD5值不同,檔案可能被篡改,請檢查!");}}}public void mouseClicked(MouseEvent e) {// TODO Auto-generated method stubif(e.getSource()==showEqual){showEqual.setText("");}}public void mouseEntered(MouseEvent arg0) {// TODO Auto-generated method stub}public void mouseExited(MouseEvent arg0) {// TODO Auto-generated method stub}public void mousePressed(MouseEvent arg0) {// TODO Auto-generated method stub}public void mouseReleased(MouseEvent arg0) {// TODO Auto-generated method stub}}
java產生字串md5函數類(javaSE)