html javascript
引入:https://github.com/brix/crypto-js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| var key = CryptoJS.enc.Utf8.parse("aabbccddeeffgghh"); var iv = CryptoJS.enc.Utf8.parse('aabbccddeeffgghh');
function Decrypt(word) { var encryptedHexStr = CryptoJS.enc.Base64.parse(word); var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); var decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); }
function Encrypt(word) { var srcs = CryptoJS.enc.Utf8.parse(word); var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); var result = encrypted.toString(); return result; }
|
java
引用 javax-crypto.jar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| package com.gotrade.portal.util; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;
public class AesEncodeUtil { public static final String VIPARA = "aabbccddeeffgghh";
public static final String bm = "UTF-8";
private static final String ASE_KEY = "aabbccddeeffgghh";
public static String encrypt(String cleartext) {
try { IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes()); SecretKeySpec key = new SecretKeySpec(ASE_KEY.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
byte[] encryptedData = cipher.doFinal(cleartext.getBytes(bm)); System.out.println(encryptedData.toString()); byte[] base64Data = org.apache.commons.codec.binary.Base64.encodeBase64(encryptedData); return new String(base64Data);
} catch (Exception e) { e.printStackTrace(); return ""; } }
public static String decrypt(String encrypted) {
try {
byte[] encryptedBase64Bytes = encrypted.getBytes(); byte[] byteMi = org.apache.commons.codec.binary.Base64.decodeBase64(encryptedBase64Bytes);
IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes()); SecretKeySpec key = new SecretKeySpec(ASE_KEY.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); byte[] decryptedData = cipher.doFinal(byteMi); return new String(decryptedData, bm); } catch (Exception e) { e.printStackTrace(); return ""; } }
public static void main(String[] args) throws Exception {
String content = "message"; System.out.println("加密前:" + content); String encryptResult = encrypt(content);
System.out.println("加密后:" + new String(encryptResult)); String decryptResult = decrypt(encryptResult); System.out.println("解密后:" + new String(decryptResult));
} }
|
参考
https://blog.csdn.net/u010648555/article/details/68066629
https://www.jianshu.com/p/a47477e8126a