需要导入jar包:
commons-codec.jar
MD5
String str = "abc";
DigestUtils.md5Hex(str);
SHA1
String str = "abc";
DigestUtils.shaHex(str);
BASE64
//加密
String str= "abc"; // abc为要加密的字符串
byte[] b = Base64.encodeBase64(str.getBytes(), true);
System.out.println(new String(b));
//解密
String str = "YWJj"; // YWJj为要解密的字符串
byte[] b = Base64.decodeBase64(str.getBytes());
System.out.println(new String(b));
本文介绍了几种常用的加密算法实现示例,包括MD5、SHA1和BASE64的加密及解密过程。通过具体的Java代码展示了如何使用commons-codec库进行操作。
1295

被折叠的 条评论
为什么被折叠?



