真郁闷,我的电脑老是中病毒
我想建个字典,把系统文件登个记.上次去卡卡社区,坛友告诉我,比较文件md5值挺准的.
--------------
java有查看系统文件md5值的方法吗?(就直接拿来用的)
--------------
我现在,加密解密算法还没学呢,刚学数据结构
就想先编点玩具小程序,过过瘾
--------------
前辈们,满足我这个愿望吧.

解决方案 »

  1.   

    请参考算法
    http://www.java2000.net/viewthread.jsp?tid=199import java.security.MessageDigest; public class StrUtil {   private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };   /** 
       * 转换字节数组为16进制字串 
       *  
       * @param b 字节数组 
       * @return 16进制字串 
       */   public static String byteArrayToHexString(byte[] b) { 
        StringBuffer resultSb = new StringBuffer(); 
        for (int i = 0; i < b.length; i++) { 
          resultSb.append(byteToHexString(b[i])); 
        } 
        return resultSb.toString(); 
      }   private static String byteToHexString(byte b) { 
        int n = b; 
        if (n < 0) 
          n = 256 + n; 
        int d1 = n / 16; 
        int d2 = n % 16; 
        return hexDigits[d1] + hexDigits[d2]; 
      }   public static String MD5Encode(String origin) { 
        String resultString = null;     try { 
          resultString = new String(origin); 
          MessageDigest md = MessageDigest.getInstance("MD5"); 
          resultString = byteArrayToHexString(md.digest(resultString.getBytes())); 
        } catch (Exception ex) {     } 
        return resultString; 
      }   public static void main(String[] args) { 
        System.err.println(MD5Encode("a")); 
      } 
    }
      

  2.   

    请参考算法
    http://www.java2000.net/viewthread.jsp?tid=199import java.security.MessageDigest; public class StrUtil {   private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };   /** 
       * 转换字节数组为16进制字串 
       *  
       * @param b 字节数组 
       * @return 16进制字串 
       */   public static String byteArrayToHexString(byte[] b) { 
        StringBuffer resultSb = new StringBuffer(); 
        for (int i = 0; i < b.length; i++) { 
          resultSb.append(byteToHexString(b[i])); 
        } 
        return resultSb.toString(); 
      }   private static String byteToHexString(byte b) { 
        int n = b; 
        if (n < 0) 
          n = 256 + n; 
        int d1 = n / 16; 
        int d2 = n % 16; 
        return hexDigits[d1] + hexDigits[d2]; 
      }   public static String MD5Encode(String origin) { 
        String resultString = null;     try { 
          resultString = new String(origin); 
          MessageDigest md = MessageDigest.getInstance("MD5"); 
          resultString = byteArrayToHexString(md.digest(resultString.getBytes())); 
        } catch (Exception ex) {     } 
        return resultString; 
      }   public static void main(String[] args) { 
        System.err.println(MD5Encode("a")); 
      } 
    }
      

  3.   

    呵呵,在网上抄了一段,目前还看不懂.拿来先用了
    我还想问问,j2se写服务器保护程序是不是挺重要的,做桌面软件,一点优势都没有.
    我看java就写网站挺占优势的呢
    前辈们,我说的对吗?
    import java.io.*;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.logging.Level;
    import java.util.logging.Logger;import com.sun.corba.se.impl.javax.rmi.CORBA.Util;public class MD5 { public static String getMD5(File file) {
    System.out.println("FileName: " + file.getName() + " Path: "
    + file.getPath());
    FileInputStream fis = null;
    try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    fis = new FileInputStream(file);
    byte[] buffer = new byte[8192];
    int length = -1;
    System.out.println("开始算");
    while ((length = fis.read(buffer)) != -1) {
    md.update(buffer, 0, length);
    }
    System.out.println("算完了");
    return bytesToString(md.digest());
    } catch (IOException ex) {
    Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    return null;
    } catch (NoSuchAlgorithmException ex) {
    Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    return null;
    } finally {
    try {
    fis.close();
    } catch (IOException ex) {
    Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null,
    ex);
    }
    }
    } public static String bytesToString(byte[] data) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f' };
    char[] temp = new char[data.length * 2];
    for (int i = 0; i < data.length; i++) {
    byte b = data[i];
    temp[i * 2] = hexDigits[b >>> 4 & 0x0f];
    temp[i * 2 + 1] = hexDigits[b & 0x0f];
    }
    return new String(temp); } public static void main(String[] args) {
    MD5 m = new MD5();
    File f = new File("C:\\WINDOWS\\dialer.exe");
    System.out.println(m.getMD5(f));
    String s1 = "93ee04140d60241002da80756a8eda07";
    String s2 = "93ee04140d60241002da80756a8eda07";
    System.out.println(s1.equals(s2));
    }}
      

  4.   

    呵呵,在网上抄了一段,目前还看不懂.拿来先用了
    我还想问问,j2se写服务器保护程序是不是挺重要的,做桌面软件,一点优势都没有.
    我看java就写网站挺占优势的呢
    前辈们,我说的对吗?
    import java.io.*;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.logging.Level;
    import java.util.logging.Logger;import com.sun.corba.se.impl.javax.rmi.CORBA.Util;public class MD5 { public static String getMD5(File file) {
    System.out.println("FileName: " + file.getName() + " Path: "
    + file.getPath());
    FileInputStream fis = null;
    try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    fis = new FileInputStream(file);
    byte[] buffer = new byte[8192];
    int length = -1;
    System.out.println("开始算");
    while ((length = fis.read(buffer)) != -1) {
    md.update(buffer, 0, length);
    }
    System.out.println("算完了");
    return bytesToString(md.digest());
    } catch (IOException ex) {
    Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    return null;
    } catch (NoSuchAlgorithmException ex) {
    Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    return null;
    } finally {
    try {
    fis.close();
    } catch (IOException ex) {
    Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null,
    ex);
    }
    }
    } public static String bytesToString(byte[] data) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f' };
    char[] temp = new char[data.length * 2];
    for (int i = 0; i < data.length; i++) {
    byte b = data[i];
    temp[i * 2] = hexDigits[b >>> 4 & 0x0f];
    temp[i * 2 + 1] = hexDigits[b & 0x0f];
    }
    return new String(temp); } public static void main(String[] args) {
    MD5 m = new MD5();
    File f = new File("C:\\WINDOWS\\dialer.exe");
    System.out.println(m.getMD5(f));
    String s1 = "93ee04140d60241002da80756a8eda07";
    String s2 = "93ee04140d60241002da80756a8eda07";
    System.out.println(s1.equals(s2));
    }}
      

  5.   

    呵呵,在网上抄了一段,目前还看不懂.拿来先用了
    我还想问问,j2se写服务器保护程序是不是挺重要的,做桌面软件,一点优势都没有.
    我看java就写网站挺占优势的呢
    前辈们,我说的对吗?
    import java.io.*;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.logging.Level;
    import java.util.logging.Logger;import com.sun.corba.se.impl.javax.rmi.CORBA.Util;public class MD5 { public static String getMD5(File file) {
    System.out.println("FileName: " + file.getName() + " Path: "
    + file.getPath());
    FileInputStream fis = null;
    try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    fis = new FileInputStream(file);
    byte[] buffer = new byte[8192];
    int length = -1;
    System.out.println("开始算");
    while ((length = fis.read(buffer)) != -1) {
    md.update(buffer, 0, length);
    }
    System.out.println("算完了");
    return bytesToString(md.digest());
    } catch (IOException ex) {
    Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    return null;
    } catch (NoSuchAlgorithmException ex) {
    Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    return null;
    } finally {
    try {
    fis.close();
    } catch (IOException ex) {
    Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null,
    ex);
    }
    }
    } public static String bytesToString(byte[] data) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f' };
    char[] temp = new char[data.length * 2];
    for (int i = 0; i < data.length; i++) {
    byte b = data[i];
    temp[i * 2] = hexDigits[b >>> 4 & 0x0f];
    temp[i * 2 + 1] = hexDigits[b & 0x0f];
    }
    return new String(temp); } public static void main(String[] args) {
    MD5 m = new MD5();
    File f = new File("C:\\WINDOWS\\dialer.exe");
    System.out.println(m.getMD5(f));
    String s1 = "93ee04140d60241002da80756a8eda07";
    String s2 = "93ee04140d60241002da80756a8eda07";
    System.out.println(s1.equals(s2));
    }}
      

  6.   

    关键还是md5如何算,网上有一大堆的例子,典型的md5如下。 public String getMD5(String s) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f' };
    try {
    byte[] strTemp = s.getBytes();
    MessageDigest mdTemp = MessageDigest.getInstance("MD5");
    mdTemp.update(strTemp);
    byte[] md = mdTemp.digest();
    int j = md.length;
    char str[] = new char[j * 2];
    int k = 0;
    for (int i = 0; i < j; i++) {
    byte byte0 = md[i];
    str[k++] = hexDigits[byte0 >>> 4 & 0xf];
    str[k++] = hexDigits[byte0 & 0xf];
    }
    return new String(str);
    } catch (Exception e) {
    return null;
    }
    }
      

  7.   

    照着修改一下,成了下面的import java.io.*;
    import java.security.*;
    import javax.swing.JFileChooser;public class FileMd5Test {
    public static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7',
    '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /**
     * 打开文件
     * 
     * */
    public static String chooseFileOpen(String title) {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle(title);
    int i = chooser.showOpenDialog(null);
    if (i == JFileChooser.APPROVE_OPTION) {
    return chooser.getSelectedFile().getAbsolutePath();
    } else {
    return "";
    }
    } /**
     * hash计算
     * */
    private String getHash(String fileName, String hashType) throws Exception {
    InputStream fis = new FileInputStream(fileName);
    byte[] buffer = new byte[1024];
    MessageDigest md5 = MessageDigest.getInstance(hashType);
    int i = 0;
    while ((i = fis.read(buffer)) > 0) {
    md5.update(buffer, 0, i);
    }
    fis.close();
    return toHexString(md5.digest());
    } /**
     * 转换16进制
     * */
    private String toHexString(byte[] b) {
    StringBuffer sb = new StringBuffer(b.length * 2);
    for (int i = 0; i < b.length; i++) {
    sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
    sb.append(hexChar[b[i] & 0x0f]);
    }
    return new String(sb);
    }
    /**
     * @param fileName 文件名
     * @param type hash类型
     * @return
     */
    public String showHash(String fileName, String type) {
    try {
    return getHash(fileName, type);
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    } public static void main(String[] args) throws Exception {
    FileMd5Test test = new FileMd5Test();
    String fileName = chooseFileOpen("选择一个文件...");
    String hashType = "MD5"; // 可以是别的什么东西,SHA1等
    System.out.println(fileName + "\n" + hashType + " --> "
    + test.showHash(fileName, hashType));
    }
    }
      

  8.   

    照着修改一下,成了下面的import java.io.*;
    import java.security.*;
    import javax.swing.JFileChooser;public class FileMd5Test {
    public static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7',
    '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /**
     * 打开文件
     * 
     * */
    public static String chooseFileOpen(String title) {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle(title);
    int i = chooser.showOpenDialog(null);
    if (i == JFileChooser.APPROVE_OPTION) {
    return chooser.getSelectedFile().getAbsolutePath();
    } else {
    return "";
    }
    } /**
     * hash计算
     * */
    private String getHash(String fileName, String hashType) throws Exception {
    InputStream fis = new FileInputStream(fileName);
    byte[] buffer = new byte[1024];
    MessageDigest md5 = MessageDigest.getInstance(hashType);
    int i = 0;
    while ((i = fis.read(buffer)) > 0) {
    md5.update(buffer, 0, i);
    }
    fis.close();
    return toHexString(md5.digest());
    } /**
     * 转换16进制
     * */
    private String toHexString(byte[] b) {
    StringBuffer sb = new StringBuffer(b.length * 2);
    for (int i = 0; i < b.length; i++) {
    sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
    sb.append(hexChar[b[i] & 0x0f]);
    }
    return new String(sb);
    }
    /**
     * @param fileName 文件名
     * @param type hash类型
     * @return
     */
    public String showHash(String fileName, String type) {
    try {
    return getHash(fileName, type);
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    } public static void main(String[] args) throws Exception {
    FileMd5Test test = new FileMd5Test();
    String fileName = chooseFileOpen("选择一个文件...");
    String hashType = "MD5"; // 可以是别的什么东西,SHA1等
    System.out.println(fileName + "\n" + hashType + " --> "
    + test.showHash(fileName, hashType));
    }
    }
      

  9.   

    csdn这几天太慢了,刷网页都刷不出来,回复一个就没反应了,
    NND,太打击我的学习积极性了!
      

  10.   

    参考这个是java bean实现:http://www.cn-java.com/www1/?action-viewnews-itemid-3414%27/
      

  11.   

    关键还是md5如何算,网上有一大堆的例子,典型的md5如下。 public String getMD5(String s) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f' };
    try {
    byte[] strTemp = s.getBytes();
    MessageDigest mdTemp = MessageDigest.getInstance("MD5");
    mdTemp.update(strTemp);
    byte[] md = mdTemp.digest();
    int j = md.length;
    char str[] = new char[j * 2];
    int k = 0;
    for (int i = 0; i < j; i++) {
    byte byte0 = md[i];
    str[k++] = hexDigits[byte0 >>> 4 & 0xf];
    str[k++] = hexDigits[byte0 & 0xf];
    }
    return new String(str);
    } catch (Exception e) {
    return null;
    }
    }
      

  12.   

    一个完整一点的实现,根据上面的模板改进了一下,共同学习import java.io.*;
    import java.security.*;
    import javax.swing.*;public class FileMd5Test {
    public static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7',
    '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /**
     * 打开文件
     * 
     * */
    public static String chooseFileOpen(String title) {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle(title);
    int i = chooser.showOpenDialog(null);
    if (i == JFileChooser.APPROVE_OPTION) {
    return chooser.getSelectedFile().getAbsolutePath();
    } else {
    return "";
    }
    } /**
     * hash计算
     * */
    private String getHash(String fileName, String hashType) throws Exception {
    InputStream fis = new FileInputStream(fileName);
    byte[] buffer = new byte[1024];
    MessageDigest md5 = MessageDigest.getInstance(hashType);
    int i = 0;
    while ((i = fis.read(buffer)) != -1) {
    md5.update(buffer, 0, i);
    }
    fis.close();
    return toHexString(md5.digest());
    } /**
     * 转换16进制
     * */
    private String toHexString(byte[] b) {
    StringBuffer sb = new StringBuffer(b.length * 2);
    for (int i = 0; i < b.length; i++) {
    sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
    sb.append(hexChar[b[i] & 0x0f]);
    }
    return new String(sb);
    }
    /**
     * @param fileName 文件名
     * @param type hash类型
     * @return
     */
    public String showHash(String fileName, String type) {
    try {
    return getHash(fileName, type);
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    } public static void main(String[] args) throws Exception {
    FileMd5Test test = new FileMd5Test();
    String fileName = chooseFileOpen("选择一个文件...");
    String hashType = "MD5"; // 可以是别的什么东西,SHA1等
    System.out.println(fileName + "\n" + hashType + " --> "
    + test.showHash(fileName, hashType)); }
    }
      

  13.   

    --------------
    哇哇,前辈的回复我看不到呢
    现在就能看到14 13 12 11楼的帖子
    --------------
    杀毒软件不是一个人能完成的吧,况且现在优秀的杀软也很多。
    我就是想把j2se的重点放在写网站服务器保护程序,网站日志分析这样的程序上
    java不是擅长做网站吗