Advertisement
thieumao

MD5 Encrypter - MD5GUI.java

Jul 7th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package md5;
  6.  
  7. /**
  8.  *
  9.  * @author thieumao
  10.  */
  11. import java.awt.Button;
  12. import java.awt.Component;
  13. import java.awt.Desktop;
  14. import java.awt.GridBagConstraints;
  15. import java.awt.GridBagLayout;
  16. import java.awt.Label;
  17. import java.awt.TextField;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.WindowAdapter;
  21. import java.awt.event.WindowEvent;
  22. import java.math.BigInteger;
  23. import java.net.URI;
  24. import java.security.MessageDigest;
  25. import java.security.NoSuchAlgorithmException;
  26. import javax.swing.JFrame;
  27. import javax.swing.JMenu;
  28. import javax.swing.JMenuBar;
  29. import javax.swing.JMenuItem;
  30. import javax.swing.JOptionPane;
  31.  
  32. public class MD5GUI extends JFrame implements ActionListener {
  33.  
  34.     private GridBagLayout gb;
  35.     private GridBagConstraints gbc;
  36.     private Label lbForm, lbUser, lbPass;
  37.     private TextField tfUser, tfPass;
  38.     private Button btRegister, btCancel;
  39.     JMenuBar mymbar;
  40.     JMenu jmFile, jmHelp;
  41.  
  42.     public static String encryptMD5(String input) {
  43.         try {
  44.             MessageDigest md = MessageDigest.getInstance("MD5");
  45.             byte[] messageDigest = md.digest(input.getBytes());
  46.             BigInteger number = new BigInteger(1, messageDigest);
  47.             String hashtext = number.toString(16);
  48.             while (hashtext.length() < 32) {
  49.                 hashtext = "0" + hashtext;
  50.             }
  51.             return hashtext;
  52.         } catch (NoSuchAlgorithmException e) {
  53.             throw new RuntimeException(e);
  54.         }
  55.     }
  56.  
  57.     public void Them(Component comp, int x, int y, int rong, int cao) {
  58.         gbc = new GridBagConstraints();
  59.         gbc.gridx = x;
  60.         gbc.gridy = y;
  61.         gbc.gridwidth = rong;
  62.         gbc.gridheight = cao;
  63.         gb.setConstraints(comp, gbc);
  64.         this.add(comp);
  65.     }
  66.  
  67.     public MD5GUI(String title) {
  68.         super(title);
  69.         this.addWindowListener(new WindowAdapter() {
  70.             @Override
  71.             public void windowClosing(WindowEvent we) {
  72.                 System.exit(0);
  73.             }
  74.         });
  75.  
  76.         mymbar = new JMenuBar();
  77.         jmFile = new JMenu("File");
  78.         JMenuItem exit = new JMenuItem("Exit");
  79.         exit.addActionListener(new ActionListener() {
  80.             @Override
  81.             public void actionPerformed(ActionEvent e) {
  82.                 System.exit(0);
  83.             }
  84.         });
  85.         jmFile.add(exit);
  86.         mymbar.add(jmFile);
  87.  
  88.         jmHelp = new JMenu("Help");
  89.         JMenuItem creator = new JMenuItem("Author");
  90.         creator.addActionListener(new ActionListener() {
  91.             @Override
  92.             public void actionPerformed(ActionEvent e) {
  93.                 JOptionPane.showMessageDialog(null, "Author: Thieu Mao - KSEC");
  94.             }
  95.         });
  96.         JMenuItem instruction = new JMenuItem("Website");
  97.         instruction.addActionListener(new ActionListener() {
  98.             @Override
  99.             public void actionPerformed(ActionEvent e) {
  100.                 Desktop desktop = Desktop.getDesktop();
  101.                 try {
  102.                     desktop.browse(new URI("http://thieumao.blogspot.com/"));
  103.                 } catch (Exception ex) {
  104.                     System.out.println("Error!");
  105.                 }
  106.             }
  107.         });
  108.         jmHelp.add(creator);
  109.         jmHelp.addSeparator();
  110.         jmHelp.add(instruction);
  111.         mymbar.add(jmHelp);
  112.  
  113.         setJMenuBar(mymbar);
  114.  
  115.  
  116.         lbForm = new Label("MD5 Encrypter");
  117.         lbUser = new Label("Text:");
  118.         lbPass = new Label("MD5:");
  119.         tfUser = new TextField(35);
  120.         tfPass = new TextField(35);
  121.         tfPass.disable();
  122.         btRegister = new Button("Calculate");
  123.         btCancel = new Button("Clear");
  124.  
  125.         gb = new GridBagLayout();
  126.         this.setLayout(gb);
  127.         Them(lbForm, 1, 0, 2, 1);
  128.         Them(lbUser, 0, 1, 1, 1);
  129.         Them(tfUser, 1, 1, 2, 1);
  130.         Them(lbPass, 0, 2, 1, 1);
  131.         Them(tfPass, 1, 2, 2, 1);
  132.         Them(btRegister, 1, 3, 1, 1);
  133.         Them(btCancel, 2, 3, 1, 1);
  134.  
  135.         btRegister.addActionListener(this);
  136.         btCancel.addActionListener(this);
  137.     }
  138.  
  139.     public static void main(String[] args) throws Exception {
  140.         MD5GUI fr = new MD5GUI("MD5 Encrypter - Thieu Mao");
  141.         fr.setSize(440, 200);
  142.         fr.setVisible(true);
  143.     }
  144.  
  145.     @Override
  146.     public void actionPerformed(ActionEvent ae) {
  147.         if (ae.getSource() == btRegister) {
  148.             tfPass.setText(encryptMD5(tfUser.getText()));
  149.         }
  150.         if (ae.getSource() == btCancel) {
  151.             tfUser.setText("");
  152.             tfPass.setText("");
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement