Implementation generates MD5 value 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 ("Cannot use MD5 algorithm, this may be too low for your Java Virtual machine version"); StringBuilder filenotfound=new StringBuilder ("Could not find the file, please relocate the file path"); StringBuilder ioerror=new StringBuilder ("File input stream error"), try {messagedigest md5=messagedigest.getinstance ("MD5");// Generate an instance of the MD5 class file File = new file (path); Create a file instance, set the path to the method parameter FileInputStream fs = new FileInputStream (file); Bufferedinputstream bi = new Bufferedinputstream (FS); Bytearrayoutputstream bo = new bytearrayouTputstream (); Byte[] B = new byte[bi.available ()]; Defines the byte array b size as the number of unblocked accessible bytes of the file int i; Read the file in byte to array B while ((i = bi.read (b, 0, b.length))! =-1) {} md5.update (b);//execute MD5 algorithm for (Byte By:md5.digest ()) {Sb.ap Pend (String.Format ("%02x", by));//Convert the generated byte MD5 value into a string}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;//returns MD5 value}}
Generate Form Class main class 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 ("Please enter the source MD5 value here", 36); JButton choicefile=new JButton ("Select File"); JButton createmd5=new JButton ("Generate MD5"); JButton judgement=new JButton ("contrast"); JPanel Panel; JFileChooser filechooser=new jfilechooser ();p ublic MainFrame () {//TODO auto-generated constructor Stubsuper ("MD5 tools") ;//Set the look and feel of the main form as metal appearance try {uimanager.setlookandfeel (New Metallookandfeel ());} catch (Unsupportedlookandfeelexception e) {/ /TODO auto-generated catch Blocke.printstacktrace ();} The form layout uses gridbaglayoutgridbaglayout gbl=new gridbaglayout (); Gridbagconstraints GBC=new gridbagconstraints ();p anel=new JPanel (GBL);p Anel.setborder (Borderfactory.createtitledborder ("Xiaohb ' s MD5 Check tool ")); Gbc.fill=gridbagconstraints.both;gbc.weightx=1.0;gbc.weighty=1.0;gbl.setconstraints (FileSource, GBC);p Anel.add (Filesource); Gbc.weightx=0.0;gbc.gridwidth=gridbagconstraints.remainder;gbl.setconstraints ( Choicefile, GBC);p Anel.add (Choicefile), gbc.gridwidth=1;gbl.setconstraints (produceMD5, GBC);p Anel.add (produceMD5) ; Gbc.weightx=0.0;gbc.gridwidth=gridbagconstraints.remainder;gbl.setconstraints (createMD5, GBC);p Anel.add ( CREATEMD5); Gbc.gridwidth=1;gbl.setconstraints (showequal, GBC);p Anel.add (showequal); gbc.weightx=0.0; Gbc.gridwidth=gridbagconstraints.remainder;gbl.setconstraints (judgement, GBC);p Anel.add (judgement); Add (panel); /Add Registrar showequal.addmouselistener (this) to the button, 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 (n); 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 ());//Displays the selected file name}else if (E.getsource () ==createmd5) { Producemd5.settext ((Digest.check (Filesource.gettext ())). ToString ());//The generated MD5 value is displayed in the text area}else if (e.getsource () = = Judgement) {//Determine if the MD5 value is the same if (Producemd5.gettext (). Equalsignorecase (Showequal.gettext ())) {Prompt.showmessagedialog (This, "two MD5 values are the same, file security! ");} Else{prompt.showmessagedialog (This, "two MD5 values are different, files may be tampered with, please check!") ");}}} 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 met Hod stub}public void mousereleased (MouseEvent arg0) {//TODO auto-generated method stub}}
Java generated string MD5 function class (Javase)