資源簡介
對稱加密AES算法,實現(xiàn)前后端加密解密,前端使用cryptojs.js實現(xiàn),后端使用java實現(xiàn)

代碼片段和文件信息
package?com.example.common.util;
import?javax.crypto.Cipher;
import?javax.crypto.spec.IvParameterSpec;
import?javax.crypto.spec.SecretKeySpec;
import?java.security.Key;
import?java.security.spec.AlgorithmParameterSpec;
import?java.util.base64;
/**
?*?對稱加密解密AES算法
?*
?*?@author?zhaors
?*/
public?class?AESUtils?{
????private?static?final?String?TRANSFORMATION?=?“AES/CBC/PKCS5Padding“;
????private?static?final?String?ALGORITHM?=?“AES“;
????private?static?final?String?CHARSET?=?“utf-8“;
????/**
?????*?建議為16或者32位
?????*/
????private?static?final?String?KEY?=?“A-16-Byte-keyVal“;
????/**
?????*?必須16位
?????*?初始化向量IV不可以為32位,否則異常java.security.InvalidAlgorithmParameterException:?Wrong?IV?length:?must?be?16?bytes?long
?????*/
????private?static?final?String?IV?=?“A-16-Byte-String“;
????/**
?????*?加密
?????*
?????*?@param?context
?????*?@return
?????*/
????public?static?String?encrypt(String?context)?{
????????try?{
????????????byte[]?decode?=?context.getBytes(CHARSET);
????????????byte[]?bytes?=?createKeyAndIv(decode?Cipher.ENCRYPT_MODE);
????????????return?base64.getEncoder().encodeToString(bytes);
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????????return?null;
????}
????/**
?????*?解密
?????*
?????*?@param?context
?????*?@return
?????*/
????public?static?String?decrypt(String?context)?{
????????try?{
????????????base64.Decoder?decoder?=?base64.getDecoder();
????????????byte[]?decode?=?decoder.decode(context);
????????????byte[]?bytes?=?createKeyAndIv(decode?Cipher.DECRYPT_MODE);
????????????return?new?String(bytes?CHARSET);
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????????return?null;
????}
????/**
?????*?獲取key?&?iv
?????*
?????*?@param?context
?????*?@param?opmode
?????*?@return
?????*?@throws?Exception
?????*/
????public?static?byte[]?createKeyAndIv(byte[]?context?int?opmode)?throws?Exception?{
????????byte[]?key?=?KEY.getBytes(CHARSET);
????????byte[]?iv?=?IV.getBytes(CHARSET);
????????return?cipherFilter(context?opmode?key?iv);
????}
????/**
?????*?執(zhí)行操作
?????*
?????*?@param?context
?????*?@param?opmode
?????*?@param?key
?????*?@param?iv
?????*?@return
?????*?@throws?Exception
?????*/
????public?static?byte[]?cipherFilter(byte[]?context?int?opmode?byte[]?key?byte[]?iv)?throws?Exception?{
????????Key?secretKeySpec?=?new?SecretKeySpec(key?ALGORITHM);
????????AlgorithmParameterSpec?ivParameterSpec?=?new?IvParameterSpec(iv);
????????Cipher?cipher?=?Cipher.getInstance(TRANSFORMATION);
????????cipher.init(opmode?secretKeySpec?ivParameterSpec);
????????return?cipher.doFinal(context);
????}
????/**
?????*?主方法測試
?????*
?????*?@param?args
?????*/
????public?static?void?main(String[]?args)?{
????????String?context?=?“zhaors“;
????????System.out.println(“元數(shù)據(jù)“?+?context);
????????String?encrypt?=?encrypt(context);
????????System.out.println(“加密之后:“?+?encrypt);
????????String?decry
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3204??2018-07-06?15:57??cryptojs\AESUtils.java
?????文件?????191936??2016-12-14?20:00??cryptojs\crypto-js.js
?????文件???????1255??2018-07-05?18:18??cryptojs\node_test.js
?????文件???????2039??2018-07-05?17:42??cryptojs\test.html
?????目錄??????????0??2018-07-06?15:47??cryptojs
-----------?---------??----------?-----??----
???????????????198434????????????????????5
評論
共有 條評論