package com.iteye.lindows.test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author Lindows
*/
public class CopyOfMD5Test {
/*
* public static byte[] getKeyedDigest(byte[] buffer, byte[] key) { try {
* MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(buffer);
* return md5.digest(key); } catch (NoSuchAlgorithmException e) { } return
* null; }
*/
public static String getKeyedDigest(String strSrc, String key) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(strSrc.getBytes("UTF8"));
String result = "";
byte[] temp;
temp = md5.digest(key.getBytes("UTF8"));
for (int i = 0; i < temp.length; i++) {
result += Integer.toHexString(
(0x000000ff & temp[i]) | 0xffffff00).substring(6);
}
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String md5Write(String md5key, File file) {
String MD5Created = CopyOfMD5Test.getKeyedDigest(md5key, "");
FileWriter fw = null;
try {
fw = new FileWriter(file, true);
fw.write("\n"+md5key+" , "+MD5Created);
System.out.println(md5key+" , "+MD5Created+"写入成功!");
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
try {
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "aa";
}
public static void main(String[] args) {
String skey = "01100002";
File file=new File("D://test3.text");
System.out.println("是否成功:"+md5Write(skey,file));
}
}
end