<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>稻壳阅读器</title>
<script type="text/javascript" src="${res!}/jquery/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="${res!}/aes/aes.js"></script>
<script type="text/javascript" src="${res!}/aes/pad-zeropadding.js"></script>
<script>
/*$(function () {
var data = $("#data").val();
var key = CryptoJS.enc.Latin1.parse('abcdef0123456789');
var iv = CryptoJS.enc.Latin1.parse('0123456789abcdef');
//加密
var encrypted = CryptoJS.AES.encrypt(data, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
});
$("#encrypt").val(encrypted.toString());
console.log(encrypted.toString())
//解密
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv: iv, padding: CryptoJS.pad.ZeroPadding});
console.log(decrypted.toString(CryptoJS.enc.Utf8));
$("#p2").text("解密结果:"+decrypted.toString(CryptoJS.enc.Utf8));
})*/
var key = CryptoJS.enc.Latin1.parse('abcdef0123456789');
var iv = CryptoJS.enc.Latin1.parse('0123456789abcdef');
function encryptData(){
var data = $("#data").val();
var encrypted = CryptoJS.AES.encrypt(data, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
});
console.log("加密结果:" + encrypted.toString());
$("#encrypt").val(encrypted);
}
function decryptData(){
var encrypted = $("#encrypt").val();
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv: iv, padding: CryptoJS.pad.ZeroPadding});
console.log("解密结果:" + decrypted.toString(CryptoJS.enc.Utf8));
$("#decrypt").val(decrypted.toString(CryptoJS.enc.Utf8));
}
</script>
</head>
<body class="layui-layout-body" style="background-color: #f2f2f2">
<div class="layui-row">
<div class="layui-col-md12">
<input style="width: 500px;" type="text" id="data" value="doc阅读加密"/>
<button class="layui-btn">测试数据</button>
</div>
</div>
<div class="layui-row">
<div class="layui-col-md12">
<input style="width: 500px;" type="text" id="encrypt" value=""/>
<button class="layui-btn" onclick="encryptData()">加密数据</button>
</div>
</div>
<div class="layui-row">
<div class="layui-col-md5">
<input style="width: 500px;" type="text" id="decrypt" value=""/>
<button class="layui-btn" onclick="decryptData()">解密数据</button>
</div>
</div>
<div class="layui-row">
<div class="layui-col-md5">
<input style="width: 500px;" type="text" id="upload" value=""/>
<button class="layui-btn" onclick="updateEncryptData()">上传加密数据</button>
</div>
</div>
</body>
</html>
Java语言实现
public class AESTest {
//static String data = "123456RWEQR";
static String key = "abcdef0123456789"; //16位
static String iv = "0123456789abcdef"; //16位
public static void main(String args[]) throws Exception {
System.out.println(encryptAES("123456"));
System.out.println(decryptAES(encryptAES("123456")));
}
public static String encryptAES(String data) throws Exception {
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);
return new sun.misc.BASE64Encoder().encode(encrypted);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String decryptAES(String data) throws Exception {
try {
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);
return originalString;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
AES三中数据填充方式
PKCS7Padding
Java不支持此方式
PKCS7Padding是缺几个字节就补几个字节的0
PKCS5Padding
JS不支持此方式
PKCS5Padding是缺几个字节就补充几个字节的几,例如缺8个字节,就补充8个字节的8
NOPadding
不补充字节