Base64
简介
Base64是基于64个可打印字符(a-z,A-Z,0-9,+、/)来显示2进制数据,它用于传输8Bit字节代码,并在网络传输中广泛应用或者本地存储字节数组。不是加密和解密
使用示例
public class Base64Test {
public static final String UTF= StandardCharsets.UTF_8.name();
@Test
public void test1() throws UnsupportedEncodingException {
String message = "法律a";
byte[] encode = Base64.getEncoder().encode(message.getBytes(UTF));
System.out.println(Arrays.toString(encode));
String enCodeStr = new String(encode, UTF);
System.out.println(enCodeStr);
byte[] decode = Base64.getDecoder().decode(enCodeStr.getBytes(UTF));
String deCodeStr = new String(decode, UTF);
System.out.println(deCodeStr);
}
@Test
public void test2() throws UnsupportedEncodingException {
String message = "法律a";
byte[] encode = org.apache.commons.codec.binary.Base64.encodeBase64(message.getBytes(UTF));
String enCodeStr = new String(encode, UTF);
System.out.println(enCodeStr);
byte[] decode = org.apache.commons.codec.binary.Base64.decodeBase64(enCodeStr.getBytes(UTF));
String deCodeStr = new String(decode, UTF);
System.out.println(deCodeStr);
}
}
URL编码
简介
当 URL 路径或者查询参数中,带有中文或者特殊字符的时候,就需要对 URL 进行编码(采用十六进制编码格式)。URL 编码的原则是使用安全字符去表示那些不安全的字符
使用示例
public class UrlCodeTest {
public static final String UTF= StandardCharsets.UTF_8.name();
public static final String DEMO_STR= "法律";
@Test
public void test1() throws UnsupportedEncodingException {
String encodeStr = URLEncoder.encode(DEMO_STR, UTF);
System.out.println("url encode result is :" + encodeStr);
String decodeStr = URLDecoder.decode(encodeStr, UTF);
System.out.println("url decode result is :" + decodeStr);
}
}
摘要算法
简介
又叫Hash算法、散列函数、数字摘要、消息摘要,是一种单向算法(不可逆),可以根据该算法对目标信息生成固定长度的唯一hash值,不能逆向计算(不可通过计算的hash值反推目标信息)。其中较为常用的:
MD5、SHA-256、SHA-512
HmacMD5、HmacSHA256、HmacSHA512
使用示例
MD5
D5算法将字符串转换成128位-->16个字节,转换成16进制字符创表示,一个16进制数字占4位,所以最后的MD5值都是用32个16进制数字表示
public class Md5Test {
public static final String UTF= StandardCharsets.UTF_8.name();
public static final String MD_NAME= "MD5";
public static final String DEMO_STR= "法律";
/**
* jdk原始
*/
@Test
public void test1() throws NoSuchAlgorithmException, UnsupportedEncodingException {
String digest = MessageDigestUtil.digest(DEMO_STR, MD_NAME);
// c47d7a21cdb9a974df82eef105477bec
System.out.println(digest);
}
/**
* codec原始
*/
@Test
public void test2() throws NoSuchAlgorithmException, UnsupportedEncodingException {
String s = DigestUtils.md5Hex(DEMO_STR);
// c47d7a21cdb9a974df82eef105477bec
// c47d7a21cdb9a974df82eef105477bec
System.out.println(s);
}
}
SHA-256
SHA-256和MD5算法差不多,消息摘要算法,不可逆。不管字符串长度多少,生成一个固定长度的字符串,SHA-256生成一个256位--也就是32个字节,转换成16进制字符之后是62个字符长度的字符串
/**
* SHA-256和MD5算法差不多
*
* 消息摘要算法,不可逆。不管字符串长度多少,生成一个固定长度的字符串
*
* SHA-256生成一个256位--也就是32个字节
*
* 转换成16进制字符之后是62个字符长度的字符串
*/
public class Sha256Test {
public static final String ALGORITHM_NAME= "SHA-256"; // 算法名称
public static final String DEMO_STR= "法律";
/**
* jdk原生的SHA-256
*/
@Test
public void test1() {
String digest = MessageDigestUtil.digest(DEMO_STR, ALGORITHM_NAME);
// 32bc0a3372fcf9cb2699e5d46eb51d73081d65bb18d82af3929001ee2240af6a
System.out.println(digest);
}
/**
* codec包的SHA-256
*/
@Test
public void test2() {
String digest = DigestUtils.sha256Hex(DEMO_STR);
// 32bc0a3372fcf9cb2699e5d46eb51d73081d65bb18d82af3929001ee2240af6a (jdk原生)
// 32bc0a3372fcf9cb2699e5d46eb51d73081d65bb18d82af3929001ee2240af6a (codec)
System.out.println(digest);
}
}
SHA-512
HA-512、SHA-256和MD5算法差不多
消息摘要算法,不可逆。不管字符串长度多少,生成一个固定长度的字符串,SHA-512生成一个512位--也就是64个字节,转换成16进制字符之后是128个字符长度的字符串
/**
* SHA-512、SHA-256和MD5算法差不多
*
* 消息摘要算法,不可逆。不管字符串长度多少,生成一个固定长度的字符串
*
* SHA-512生成一个512位--也就是64个字节
*
* 转换成16进制字符之后是128个字符长度的字符串
*/
public class Sha512Test {
public static final String ALGORITHM_NAME= "SHA-512"; // 算法名称
public static final String DEMO_STR= "法律";
/**
* jdk原生的SHA-512
*/
@Test
public void test1() {
String digest = MessageDigestUtil.digest(DEMO_STR, ALGORITHM_NAME);
// 2001a3d435c3180120e9b748faf08bebae6a51c717ea21575771e68561da76fb8e4cdb48691aafc1a8b062ba54ab9680d656b9b28beedc948d01bd24d83082da
System.out.println(digest);
}
/**
* codec包的SHA-512
*/
@Test
public void test2() {
String digest = DigestUtils.sha512Hex(DEMO_STR);
// 2001a3d435c3180120e9b748faf08bebae6a51c717ea21575771e68561da76fb8e4cdb48691aafc1a8b062ba54ab9680d656b9b28beedc948d01bd24d83082da (jdk原生)
// 2001a3d435c3180120e9b748faf08bebae6a51c717ea21575771e68561da76fb8e4cdb48691aafc1a8b062ba54ab9680d656b9b28beedc948d01bd24d83082da (codec)
System.out.println(digest);
}
}
public class MessageDigestUtil {
public static final String UTF8= StandardCharsets.UTF_8.name();
/**
* 摘要算法
*
* @param sourceStr 原始字符串
* @param algorithm 算法名称如:MD5、SHA-256、SHA-512
* @return 16进制字符串
*/
public static String digest(String sourceStr,String algorithm) {
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
// 结果字节数组
byte[] digest = md.digest(sourceStr.getBytes(UTF8));
// 转换成16进制字符串
return HexUtil.transBytes2HexStr(digest);
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**
* mac签名算法
*
* @param sourceStr 原始字符串
* @param key 秘钥
* @param algorithm 算法名称,如:HmacMD5 HmacSha-256、HmacSha-512
* @return 16进制字符串
*/
public static String macDigest(String sourceStr,String key,String algorithm) {
try {
// mac实例
Mac mac = Mac.getInstance(algorithm);
// 创建秘钥对
SecretKey secretKey = new SecretKeySpec(key.getBytes(UTF8),algorithm);
// 初始化mac
mac.init(secretKey);
// 原始字符串字节数组
byte[] originalBytes = sourceStr.getBytes(UTF8);
// 结果字节数组
byte[] bytes = mac.doFinal(originalBytes);
// 转换成16进制字符串
return HexUtil.transBytes2HexStr(bytes);
} catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
public class HexUtil {
/**
* 字节数组转字16进制符串
*
* @param digest 字节数组
* @return 16进制字符串
*/
public static String transBytes2HexStr(byte[] digest) {
StringBuilder builder = new StringBuilder();
for (byte b : digest) {
String s = Integer.toHexString(((int) b) & 0xff);
if(s.length() == 1) {
s = "0" + s;
}
builder.append(s);
}
return builder.toString();
}
/**
* 16进制字符串转byte数组
*
* @param hexStr 16进制字符串
* @return 字节数组
*/
public static byte[] transHexStr2Bytes(String hexStr) {
int length = hexStr.length();
if (length % 2!=0) {
throw new RuntimeException("hexStr length must be even number!");
}
byte[] bytes = new byte[length/2];
for (int i = 0;i<bytes.length;i++) {
int high4 = Integer.parseInt(hexStr.substring(i*2,i*2 +1),16);
int low4 = Integer.parseInt(hexStr.substring(i*2+1,i*2 + 2),16);
bytes[i] = (byte)(high4 * 16 + low4);
}
return bytes;
}
}
mac摘要
Message Authentication Code 消息验证码,比摘要算法安全性更高,加入了一个秘钥
HmacMd5对应md5摘要算法,生成一个128位数字--16个字节,转换成16进制数字字符之后是32个字符
HmacSha256对应SHA-256摘要算法,生成一个256位数字--32个字节,转换成16进制数字字符之后是64个字符
HmacSha512对应SHA-512摘要算法,生成一个512位数字--64个字节,转换成16进制数字字符之后是128个字符
/**
* mac类摘要算法
* Message Authentication Code 消息验证码
*
* 比摘要算法安全性更高,加入了一个秘钥
*
* HmacMd5对应md5摘要算法,生成一个128位数字--16个字节,转换成16进制数字字符之后是32个字符
*
* HmacSha256对应SHA-256摘要算法,生成一个256位数字--32个字节,转换成16进制数字字符之后是64个字符
*
* HmacSha512对应SHA-512摘要算法,生成一个512位数字--64个字节,转换成16进制数字字符之后是128个字符
*/
public class HmacTest {
public static final String ALGORITHM_NAME= "SHA-256"; // 算法名称
public static final String DEMO_STR= "法律";
/**
* jdk原生mac摘要算法
*/
@Test
public void test1() {
String key = "123";
String hmacMD5Str = MessageDigestUtil.macDigest(DEMO_STR, key, "HmacMD5");
// 2c50de960b42c1f86b3ba22e9de9e49f
System.out.println("hmacMD5Str:" + hmacMD5Str);
String hmacSHA256 = MessageDigestUtil.macDigest(DEMO_STR, key, "HmacSHA256");
// 2128ed9d2e29c971995bf65d06c1671f3b77344631da122f4e463d07b9257e61
System.out.println("hmacSHA256:" + hmacSHA256);
String hmacSHA512 = MessageDigestUtil.macDigest(DEMO_STR, key, "HmacSHA512");
// 381586ca8518b05799fe603805393afdac07e54af11fc9b04b8df6b644b77dfa37a6b0691f67d7b5a4ef1989d7751328a1d045c6ef26608062f51eac84bd6efa
System.out.println("hmacSHA512:" + hmacSHA512);
}
/**
* codec的mac摘要算法
*/
@Test
public void test2() {
String key = "123";
String hmacMd5Str = new HmacUtils(HmacAlgorithms.HMAC_MD5, key).hmacHex(DEMO_STR);
// 2c50de960b42c1f86b3ba22e9de9e49f(jdk原生)
// 2c50de960b42c1f86b3ba22e9de9e49f(codec)
System.out.println("hmacMd5Str:" + hmacMd5Str);
String hmacSha255Str = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, key).hmacHex(DEMO_STR);
// 2128ed9d2e29c971995bf65d06c1671f3b77344631da122f4e463d07b9257e61(jdk原生)
// 2128ed9d2e29c971995bf65d06c1671f3b77344631da122f4e463d07b9257e61(codec)
System.out.println("hmacSha255Str:" + hmacSha255Str);
String hmacSha512Str = new HmacUtils(HmacAlgorithms.HMAC_SHA_512, key).hmacHex(DEMO_STR);
// 381586ca8518b05799fe603805393afdac07e54af11fc9b04b8df6b644b77dfa37a6b0691f67d7b5a4ef1989d7751328a1d045c6ef26608062f51eac84bd6efa(jdk原生)
// 381586ca8518b05799fe603805393afdac07e54af11fc9b04b8df6b644b77dfa37a6b0691f67d7b5a4ef1989d7751328a1d045c6ef26608062f51eac84bd6efa(codec)
System.out.println("hmacSha512Str:" + hmacSha512Str);
}
}