前端代码: // 加密 encrypt(word, keyStr, ivStr) { let key = CryptoJS.enc.Utf8.parse('ihaierForTodoKey') // 中与后台一样 密码 let iv = CryptoJS.enc.Utf8.parse('ihaierForTodo_Iv') // 中与后台一样 if (keyStr) { key = CryptoJS.enc.Utf8.parse(keyStr) iv = CryptoJS.enc.Utf8.parse(ivStr) } const srcs = CryptoJS.enc.Utf8.parse(word) var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }) return CryptoJS.enc.Base64.stringify(encrypted.ciphertext) }
this.encrypt(userName.trim());
后台代码:AESUtil.java
import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; public class AESUtil { //密钥 (需要前端和后端保持一致)十六位作为密钥 private static final String KEY = "ihaierForTodoKey"; //密钥偏移量 (需要前端和后端保持一致)十六位作为密钥偏移量 private static final String IV = "ihaierForTodo_Iv"; //算法 private static final String ALGORITHMSTR = "AES/CBC/PKCS5Padding"; /** * base 64 decode * @param base64Code 待解码的base 64 code * @return 解码后的byte[] * @throws Exception */ public static byte[] base64Decode(String base64Code) throws Exception{ return StringUtils.isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code); } /** * AES解密 * * @param encryptBytes 待解密的byte[] * @return 解密后的String * @throws Exception */ public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHMSTR); byte[] temp = IV.getBytes("UTF-8"); IvParameterSpec iv = new IvParameterSpec(temp); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY.getBytes(), "AES"), iv); byte[] decryptBytes = cipher.doFinal(encryptBytes); System.out.print(new String(decryptBytes)); return new String(decryptBytes); } /** * 将base 64 code AES解密 * @param encryptStr 待解密的base 64 code * @return 解密后的string * @throws Exception */ public static String aesDecrypt(String encryptStr) throws Exception { return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr)); } }
在接收到参数调用进行解密: String mobile = AESUtil.aesDecrypt(mobile);