常采用的是AES的加密解密方法,再稍微掺杂点随机数
JS端
//需要引入 aes.js
/**
* 非规则的aes加密,自己加了字符串
* @param s
* @returns
*/
function aesEncode(s) {
var key = CryptoJS.enc.Latin1.parse('abcdef0123456789');
var iv = CryptoJS.enc.Latin1.parse('0123456789abcdef');
// 加密
var encrypted = CryptoJS.AES.encrypt(s, key, {
iv : iv,
mode : CryptoJS.mode.CBC,
padding : CryptoJS.pad.ZeroPadding
});
var enc=encrypted.toString();
var ran=randomWord(false,9);
return ran+enc;
}
/**
* 非规则的aes解密自己加了字符串
* @param s
* @returns
*/
function aesDecode(s) {
var d=s.substr(9);
var key = CryptoJS.enc.Latin1.parse('abcdef0123456789');
var iv = CryptoJS.enc.Latin1.parse('0123456789abcdef');
var decrypted = CryptoJS.AES.decrypt(d, key, {
iv : iv,
padding : CryptoJS.pad.ZeroPadding
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
/**
* 生成3-32位随机串:randomWord(true, 3, 32)
* 生成43位随机串:randomWord(false, 43)
* randomWord 产生任意长度随机字母数字组合
* randomFlag-是否任意长度 min-任意长度最小位[固定位数] max-任意长度最大位
* xuanfeng 2014-08-28
*/
function randomWord(randomFlag, min, max){
var str = "",
range = min,
arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
// 随机产生
if(randomFlag){
range = Math.round(Math.random() * (max-min)) + min;
}
for(var i=0; i<range; i++){
pos = Math.round(Math.random() * (arr.length-1));
str += arr[pos];
}
return str;
}
JAVA端
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
/**
* 高级加密标准(英语:Advanced Encryption Standard
* 稍微扩展了下
*
* @author Norton Lai
* @created 2019-5-14 上午9:18:02
*/
public class AesExpand {
/**
* 日志记录器
* @Fields LOG
*/
private static Logger LOG=LoggerFactory.getLogger(AesExpand.class);
// static String data = "123456RWEQR";
static String key = "abcdef0123456789"; // 16位 秘钥 使用时候用genvi方法重新生成过
static String iv = "0123456789abcdef"; // 16位 偏移量 使用时候用genvi方法重新生成过
static String orig = "0123456789abcdef"; // 16位
public static void main(String args[]) throws Exception {
System.out.println(encryptAESExpand("admin"));
System.out.println(decryptAESExpand(encryptAESExpand("admin")));
}
public static String genIv() {
char[] chars = orig.toCharArray();
StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int i = 0; i < 16; i++) {
int idx = r.nextInt(16);
sb.append(chars[idx]);
}
return sb.toString();
}
/**
* 加密
* @param data
* @return
* @throws Exception
* @author Norton Lai
* @created 2019-5-14 上午10:54:50
*/
public static String encryptAESExpand(String data){
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NOPadding"); // 参数分别代表 算法名称/加密模式/数据填充方式
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
String encode = new sun.misc.BASE64Encoder().encode(encrypted);
//如果数据过长base64会自动添加换行符
encode=encode.replaceAll(System.lineSeparator(), "");
return getChar(9)+encode;
} catch (Exception e) {
LOG.error("加密错误"+e.getMessage(),e);
return null;
}
}
/**
* 随机固定数目字符串
* @param length
* @return
* @author Norton Lai
* @created 2019-5-14 上午10:57:56
*/
public static String getChar(int length) {
char[] ss = new char[length];
int i=0;
while(i<length) {
int f = (int) (Math.random()*3);
if(f==0)
ss[i] = (char) ('A'+Math.random()*26);
else if(f==1)
ss[i] = (char) ('a'+Math.random()*26);
else
ss[i] = (char) ('0'+Math.random()*10);
i++;
}
String str=new String(ss);
return str;
}
/**
* 解密
* @param data
* @return
* @throws Exception
* @author Norton Lai
* @created 2019-5-14 上午10:54:33
*/
public static String decryptAESExpand(String data){
try {
data=data.substring(9);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data);
Cipher cipher = Cipher.getInstance("AES/CBC/NOPadding");
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original).trim();
return originalString;
} catch (Exception e) {
LOG.error("解密错误"+e.getMessage(),e);
return null;
}
}
}