一、示例说明
Web前端基于CryptoJS加密解密JS库,配置CryptoJS.mode.ECB模式和CryptoJS.pad.Pkcs7方式,采用aes对称密钥实现数据加解密。java后端采用相同匹配的AES/ECB/PKCS7Padding模式,基于前后端加解密方式一致情况下,可以实现后端aes加密前端解密的应用场景。
二、代码示例
1. 前端html代码示例如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">@b@<html xmlns="http://www.w3.org/1999/xhtml">@b@<head> @b@<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> @b@<script src="http://www.xwood.net/js/jquery-1.6.2.min.js"></script> @b@<script src="http://www.xwood.net/js/crypto-js.min.js"></script> @b@@b@<script type="text/javascript"> @b@ var aseKey = "123456" //秘钥必须为:8/16/32位@b@ var message = "我是明文!";@b@ //加密@b@ var encrypt = CryptoJS.AES.encrypt(message, CryptoJS.enc.Utf8.parse(aseKey), {@b@ mode: CryptoJS.mode.ECB,@b@ padding: CryptoJS.pad.Pkcs7@b@ }).toString();@b@ alert(encrypt); //5mWpefLiNuqYeCAUg5Gchg==@b@ @b@ @b@ //解密@b@ var decrypt = CryptoJS.AES.decrypt(encrypt, CryptoJS.enc.Utf8.parse(aseKey), {@b@ mode: CryptoJS.mode.ECB,@b@ padding: CryptoJS.pad.Pkcs7@b@ }).toString(CryptoJS.enc.Utf8);@b@ alert(decrypt); //我是明文! @b@@b@</script> @b@@b@@b@@b@</head>@b@<body >@b@ @b@ @b@</body>@b@</html>
2. java后端代码部分(bcprov-jdk16依赖包)
<dependency>@b@ <groupId>org.bouncycastle</groupId>@b@ <artifactId>bcprov-jdk16</artifactId>@b@ <version>1.46</version>@b@ </dependency>
import java.security.Security;@b@@b@import javax.crypto.Cipher;@b@import javax.crypto.spec.SecretKeySpec;@b@ @b@import org.apache.commons.codec.binary.Base64;@b@import org.apache.commons.codec.binary.Hex; @b@ @b@ @b@public class AESUtil {@b@ @b@ //add new bouncycastle ciphers@b@ static {@b@ Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());@b@ }@b@ @b@ public static void main(String[] args) {@b@ @b@ // encryption key should be multiple of 16 character long@b@ String key = "abcdefghijklmnop";@b@ String data = "0123456789";@b@ @b@ String encrypted = AESUtil.encrypt(data,key);@b@ System.out.println("加密后数据: "+encrypted);@b@ String decrypted = AESUtil.decrypt(AESUtil.encrypt(data, key), key);@b@ System.out.println("解密后数据: "+decrypted);@b@ }@b@ @b@ /**@b@ * encrypt input text@b@ *@b@ * @param input@b@ * @param key@b@ * @return@b@ */@b@ public static String encrypt(String input, String key) {@b@ byte[] crypted = null;@b@ try {@b@ @b@ SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES"); @b@ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");@b@ cipher.init(Cipher.ENCRYPT_MODE, skey);@b@ crypted = cipher.doFinal(input.getBytes());@b@ } catch (Exception e) {@b@ System.out.println(e.toString());@b@ e.printStackTrace();@b@ }@b@ @b@ return new String(Base64.encodeBase64(crypted));@b@// return new String(Hex.encodeHex(crypted)).toUpperCase();@b@// return new String(Base64.encodeBase64(crypted));@b@ }@b@ @b@ /**@b@ * decrypt input text@b@ *@b@ * @param input@b@ * @param key@b@ * @return@b@ */@b@ public static String decrypt(String input, String key) {@b@ byte[] output = null;@b@ try {@b@ SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");@b@ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");@b@ cipher.init(Cipher.DECRYPT_MODE, skey);@b@ output = cipher.doFinal(Base64.decodeBase64(input));@b@ output = cipher.doFinal(Hex.decodeHex(input.toCharArray()));@b@ } catch (Exception e) {@b@ System.out.println(e.toString());@b@ }@b@ return new String(output);@b@ }@b@ @b@}
控制台运行
加密后数据: Ny0eP1CmaoqSYcqsfDboog==@b@org.apache.commons.codec.DecoderException: Illegal hexadecimal character N at index 0@b@解密后数据: 0123456789