java生成字符串md5函数类(javaSE)

//实现生成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 stub
		StringBuilder 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 block
			return noAlgorithm;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			return fileNotFound;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return 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 stub
		super("MD5工具");
		//设置主窗体的观感为金属外观
			try {
				UIManager.setLookAndFeel(new MetalLookAndFeel());
			} catch (UnsupportedLookAndFeelException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		//窗体布局使用GridBagLayout
		GridBagLayout 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 stub
MainFrame 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 stub
		Digest 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 stub
		if(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

	}
}

  

时间: 2024-10-22 17:21:14

java生成字符串md5函数类(javaSE)的相关文章

java生成字符串md5函数类

import java.security.MessageDigest; /** * Md5 工具 */ public class Md5Util { private static MessageDigest md5 = null; static { try { md5 = MessageDigest.getInstance("MD5"); } catch (Exception e) { System.out.println(e.getMessage()); } } /** * 用于获取

java生成字符串的MD5值

下面的代码实现了MD5值的生成: public class MD5Test2 { public static void main(String[] args) { System.out.println(MD5Test2.MD5Operation("hello")); System.out.println(MD5Test2.getMD5("hello")); } //通过java.math包的BigInteger类实现十六进制的转换 public final stat

java生成excel文件工具类实例

import java.io.File; import java.io.IOException; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; import org.

QT中生成字符串md5的方法

QT提供了QCryptographicHash类,想转md5或者sda1都很方便 QString fileText = original_Image.text(); QString fileCut = fileText.remove(256,fileText.count() - 256); fileCut += QTime::currentTime().toString();//加上时间戳再生成 QByteArray ba; ba =QCryptographicHash::hash(fileCu

db2使用Java存储过程实现MD5函数

1.数据库版本 2.Java脚本 import java.security.MessageDigest; import COM.ibm.db2.app.UDF; public class MD5UDF extends UDF { public static String MD5(String s) { String s1 = new String(""); char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',

Java 获取字符串Hash值

Java 生成字符串的Hash值: /** * A hashing method that changes a string (like a URL) into a hash suitable for using as a * disk filename. */ public static String hashKeyForDisk(String key) { String cacheKey; try { final MessageDigest mDigest = MessageDigest.g

JAVA 生成 MD5摘要 和SHA1摘要 及MD5的彩虹表破解

MD5  和SHA1 这两个摘要算法,使用很普遍,几乎每个项目我们都会用这两个算法来骗自己和骗用户,看啊,我们保存的用户密码是加密的.对,存的不是明文,是密文.然而MD5真的安全么? 上干货 JAVA生成MD5摘要的代码: MessageDigest md5 =MessageDigest.getInstance("MD5");         String messageStr="123456";         md5.update(messageStr.getB

Java MD5加密类

1 /************************************************* 2 md5 类实现了RSA Data Security, Inc.在提交给IETF 3 的RFC1321中的MD5 message-digest 算法. 4 *************************************************/ 5 public class MD5 { 6 /* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define

java生成MD5校验码

在Java中,java.security.MessageDigest (rt.jar中)已经定义了 MD5 的计算,所以我们只需要简单地调用即可得到 MD5 的128 位整数.然后将此 128 位计 16 个字节转换成 16 进制表示即可. 下面是一个可生成字符串或文件MD5校验码的例子,测试过,可当做工具类直接使用,其中最主要的是getMD5String(String s)和getFileMD5String(File file)两个方法,分别用于生成字符串的md5校验值和生成文件的md5校验值