Base64解密 加密
第一步:导包
commons-codec-1.6.jar
第二步:
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
public class MD5Test
{
static final String TARGET = "12345678910";
/*
* 不可逆算法 MD5
*/
public static void Md5()
{
String str = DigestUtils.md5Hex(TARGET);
System.out.println("md5Hex: " + str);
}
/*
* 不可逆算法 SHA1
*/
public static void Sha1()
{
String str = DigestUtils.shaHex(TARGET);
System.out.println("shaHex: " + str);
str = DigestUtils.sha256Hex(TARGET);
System.out.println("sha256Hex: " + str);
str = DigestUtils.sha384Hex(TARGET);
System.out.println("sha384Hex: " + str);
str = DigestUtils.sha512Hex(TARGET);
System.out.println("sha512Hex: " + str);
}
/*
* 可逆算法 BASE64
*/
public static void Base64()
{
// 加密
byte[] b = Base64.encodeBase64(TARGET.getBytes(), true);
String str = new String(b);
System.out.println("BASE64: " + str);
// 解密
byte[] b1 = Base64.decodeBase64(str);
System.out.println("解密之后内容为: " + new String(b1));
}
public static void main(String[] args)
{
Md5();
Sha1();
Base64();
}
}
执行结果:
md5Hex: 432f45b44c432414d2f97df0e5743818
shaHex: 9048ead9080d9b27d6b2b6ed363cbf8cce795f7f
sha256Hex: 63640264849a87c90356129d99ea165e37aa5fabc1fea46906df1a7ca50db492
sha384Hex: 0614329c6f82af724bb52d7e29d69c7651ec3fe705385a9f7e00c39bea61281b49ecd2e2236898666076e869043c1a9c
sha512Hex: 16d4074e1a1f081538a09b801586da6881e547def93e643e4bac5195d9ef14ecb45d636f34c7cd166408de6cb2ed987d3e53212e3ad12a597cac49e5b64197ab
BASE64: MTIzNDU2Nzg5MTA=
解密之后内容为: 12345678910