import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Test {
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.md5Test("a"));
System.out.println(t.md5Test("a"));
System.out.println(t.shaTest("a"));
System.out.println(t.shaTest("a"));
}
/**
* md5加密
* @param source
* @return
*/
public String md5Test(String source) {
MessageDigest md;
try {
md = MessageDigest.getInstance("md5");
md.update(source.getBytes());
return md.digest().toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "error";
}
}
/**
* sha加密
* @param source
* @return
*/
public String shaTest(String source) {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA");
md.update(source.getBytes());
return md.digest().toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "SHA";
}
}
}
java加密
最新推荐文章于 2024-08-12 16:25:56 发布