import java.security.*;
/*
* TestEncrypt.java
* Author: MKing
* Last Date: 2005-11-21
* Description: A test progrm to encrypt a string using MD5 or SHA-1,etc.
*/
public class TestEncrypt {
public TestEncrypt() {}
public String Encrypt(String strSrc,String encName) {
//parameter strSrc is a string will be encrypted,
//parameter encName is the algorithm name will be used.
//encName dafault to "MD5"
MessageDigest md=null;
String strDes=null;
byte[] bt=strSrc.getBytes();
try {
if (encName==null||encName.equals("")) {
encName="MD5";
}
md=MessageDigest.getInstance(encName);
md.update(bt);
strDes=bytes2Hex(md.digest()); //to HexString
}
catch (NoSuchAlgorithmException e) {
System.out.println("Invalid algorithm.");
return null;
}
return strDes;
}
public String bytes2Hex(byte[]bts) { String des=""; String tmp=null; for (int i=0;i