站点介绍
package md5_sha;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/*
* 消息摘要 :即对任意长度的数据 进行加密,产生一个唯一的指纹。MD5、SHA
*/
public class MessageDigestTest {
public String encryptInfo(String info,String algorithm){
MessageDigest messageDigest=null;
byte[] message=null;
try {
messageDigest=MessageDigest.getInstance(algorithm);//获取消息摘要对象
messageDigest.update(info.getBytes()); // 添加要进行计算摘要的信息
message= messageDigest.digest(); // 得到该摘要
// return byte2Str(message);
// return byte2Hex(message);
return encryptBase64(message);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
public String byte2Str(byte []b){
ByteArrayOutputStream bos=new ByteArrayOutputStream();
try {
OutputStream outputStream=MimeUtility.encode(bos, base64);
outputStream.write(b);
return bos.toString();
} catch (MessagingException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
public String byte2Hex(byte b[]){
String temp=;
String hex=;
for(int i=0;i