如题。
package com.app.util.crypto;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @ClassName AES
* @Description AES加解密
*
* - "AES"
* - No KeyGenerator
* - No IV
* - "ECB" instead of "CBC"
*/
public class AES {
/*
* 加解密类型
*/
private static final String ALGORITHM = "AES";
/*
* token是32个字符的Hex编码。其实这个是一个UUID的16字节的byte数组的Hex表示形式
*/
private static final String TOKEN = "";
/*
* 十六进制字符
*/
public static final char[] HEXCHARS = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
public static String encryptBase64(String s, String token) throws Exception {
byte[] key = toBytes(token);
return encryptBase64URLSafe(s, key);
}
public static String decryptBase64(String encrypted, String token) throws Exception{
byte[] key = toBytes(token);
return decryptBase64URLSafe(encrypted, key);
}
public static String encrypt(String s, String token) throws Exception {
byte[] key = toBytes(token);
byte[] encryptedBytes = encryptBytes(s, key);
return asHex(encryptedBytes);
}
public static String decrypt(String encrypted, String token) throws Exception{
byte[] key = toBytes(token);
byte[] encryptedBytes = toBytes(encrypted);
String decrypted = decryptBytes(encryptedBytes,key);
return decrypted;
}
private static byte[] encryptBytes(String s, byte[] key) throws Exception{
if (key == null) {
return null;
}
// 判断Key是否为16位
if (key.length != 16) {
return null;
}
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(s.getBytes("utf-8"));
return encrypted;
}
private static String decryptBytes(byte[] encrypted, byte[] key) throws Exception{
// 判断Key是否正确
if (key == null) {
return null;
}
// 判断Key是否为16位
if (key.length != 16) {
return null;
}
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(encrypted);
String originalString = new String(original, "utf-8");
return originalString;
}
private static String encryptBase64URLSafe(String s, byte[] key) throws Exception{
byte[] encryptedBytes = encryptBytes(s, key);
return Base64.getUrlEncoder().withoutPadding().encodeToString(encryptedBytes);
}
private static String decryptBase64URLSafe(String encrypted, byte[] key) throws Exception {
byte[] encryptedBytes = Base64.getUrlDecoder().decode(encrypted);
String decrypted = decryptBytes(encryptedBytes,key);
return decrypted;
}
public static byte[] toBytes(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
public static String asHex(byte[] bytes) {
char chars[] = new char[bytes.length * 2];
for (int i = 0; i < chars.length; i = i + 2) {
byte b = bytes[i / 2];
chars[i] = HEXCHARS[(b >>> 0x4) & 0xf];
chars[i + 1] = HEXCHARS[b & 0xf];
}
return new String(chars);
}
}
建议给代码加上高亮
– dudu 6年前