日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java产生字符函数_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

}

}

總結

以上是生活随笔為你收集整理的java产生字符函数_java生成字符串md5函数类(javaSE)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。