//以下是SHA-1加密和MD5加密的代码
//在需要使用的地方直接调用即可,例如在servlet或SSH里的action里
package tool;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
public class Encrypt {
public static String SHAencrypt(String oldStr){
if(!(oldStr.equals(""))){
byte[] oldBytes = oldStr.getBytes();
MessageDigest md;
try {
md=MessageDigest.getInstance("SHA-1"); //SHA-1加密
byte[] newBytes=md.digest(oldBytes);
BASE64Encoder encoder=new BASE64Encoder();
String newStr=encoder.encode(newBytes);
return newStr;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.out.println(oldStr+" 加密失败!");
return "NULL";
}
}else{
return "NULL";
}
}
public static String MD5encrypt(String oldStr){
if(!(oldStr.equals(""))){
byte[] oldBytes=oldStr.getBytes();
MessageDigest md;
try {
md=MessageDigest.getInstance("MD5"); //MD-5加密
byte[] newBytes=md.digest(oldBytes);
BASE64Encoder encoder=new BASE64Encoder();
String newStr=encoder.encode(newBytes);
return newStr;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.out.println("加密失败!");
return oldStr;
}
}else{
return "wrongPassword";
}
}
//end
}