A. java如何算md5碼
可以利用JDK自帶的MD5來加密。
publicclassMD5Util{
publicfinalstaticStringMD5(Strings){
charhexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
try{
byte[]btInput=s.getBytes();
//獲得MD5摘要演算法的MessageDigest對象
MessageDigestmdInst=MessageDigest.getInstance("MD5");
//使用指定的位元組更新摘要
mdInst.update(btInput);
//獲得密文
byte[]md=mdInst.digest();
//把密文轉換成十六進制的字元串形式
intj=md.length;
charstr[]=newchar[j*2];
intk=0;
for(inti=0;i<j;i++){
bytebyte0=md[i];
str[k++]=hexDigits[byte0>>>4&0xf];
str[k++]=hexDigits[byte0&0xf];
}
returnnewString(str);
}catch(Exceptione){
e.printStackTrace();
returnnull;
}
}
publicstaticvoidmain(String[]args){
System.out.println(MD5Util.MD5("20121221"));
System.out.println(MD5Util.MD5("加密"));
}
}
B. 如何使用Java生成MD5代碼
這是我以前做的一個小項目時用到md5寫的
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//將用戶密碼進行md5加密 並返回加密後的32位十六進制密碼
public class MD5Util {
public static String md5(String password) {
try {
// 獲取md5對象
MessageDigest md = MessageDigest.getInstance("md5");
// 獲取加密後的密碼並返回十進制位元組數組
byte[] bytes = md.digest(password.getBytes());
// 遍歷數組得到每個十進制數並轉換成十六進制
StringBuffer sb = new StringBuffer();
for (byte b : bytes) {
// 把每個數轉成十六進制 存進字元中
sb.append(toHex(b));
}
String finish = sb.toString();
return finish;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
// 十進制轉十六進制方法
private static String toHex(byte b) {
int target = 0;
if (b < 0) {
target = 255 + b;
} else {
target = b;
}
int first = target / 16;
int second = target % 16;
return Hex[first] + Hex[second];
}
static String[] Hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f" };
/*public static void main(String[] args) {
String a = MD5Util.md5("1234");
System.out.println(a);
}*/
}
C. Java MD5如何解密
MD5 不能解密, MD5的破解方式就是 把不同的字元串按MD5加密 然後對比加密後的結果是不是一樣. 在線MD5解密 也是這樣的原理.
D. Java,如何獲取文件的MD5值
package cdm;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import org.apache.commons.codec.digest.*;
import org.apache.commons.io.IOUtils;
public class testMD5 {
public static String getMd5ByFile(File file) throws FileNotFoundException {
String value = null;
FileInputStream in = new FileInputStream(file);
try {
MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(byteBuffer);
BigInteger bi = new BigInteger(1, md5.digest());
value = bi.toString(16);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return value;
}
public static void main(String[] args) throws IOException {
String path="E:\文件.zip";
String v = getMd5ByFile(new File(path));
System.out.println("MD5:"+v.toUpperCase());
FileInputStream fis= new FileInputStream(path);
String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
IOUtils.closeQuietly(fis);
System.out.println("MD5:"+md5);
//System.out.println("MD5:"+DigestUtils.md5Hex("WANGQIUYUN"));
}
}