MD5简介:
MD5的全称:Message-Digest Algorithm 5(信息-摘要算法5)
MD5的加密方式是一种哈希加密。一些主流的编程语言都已经实现了MD5的加密,所以如果你的程序或是系统涉及到在多种语言之间的校验,那么MD5可以是备选之一。不过因为MD5是采用哈希函数来进行的加密,所以它无关密钥,也就是说在确定了明文的情况下,MD5就可以加密。不过MD5是不可逆的,只能加密,不能解密。
MD5加密字符串:
加密字符串
import java.io.UnsupportedEncodingException;
import java.;
import java.;
public class OutterMD5 {
/**
*MD5 加密
*
* @param data 需要加密的内容
* @param encoding 编码
* @return
* @throws UnsupportedencodingException
*/
public static String encrypt(String data, String encoding) throws UnsupportedEncodingException {
MessageDigest messageDigest = null;
try {
messageDigest = Me("MD5");
me();
me(encoding));
} catch (NoSuchAlgorithmException e) {
Sy("NoSuchAlgorithmException caught!");
Sy(-1);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = me();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < by; i++) {
if (0xFF & byteArray[i]).length() == 1)
md5S("0").append(0xFF & byteArray[i]));
else
md5S(0xFF & byteArray[i]));
}
return md5S().toUpperCase();
}
/**
* @param args
*/
public static void main(String[] args) {
String data = "中国";
// 加密
try {
String temp=Ou(data, "GBK");
Sy ("temp===="+temp);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
MD5加密文件:
在讲解MD5加密文件之前,首先请大家明确的是:加密文件的实质就是加密文件中的内容,与文件名称或是结构其他因素无关。
上源码
MD5加密文件
import java.io.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.ma;
import java.nio.MappedByteBuffer;
import java.nio.c;
import java.;
/**
* 加密文件2017年4月18日 10:02:22
*/
public static String getMd5ByFile(File file) {
String value = null;
FileInputStream in = null;
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
MappedByteBuffer byteBuffer = in.getChannel().ma, 0, ());
MessageDigest md5 = Me("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;
}
感谢大家关注
“点读微讯”