From 32f1d6864e442c8b91400a4d8a45cb4d56f9e020 Mon Sep 17 00:00:00 2001 From: smallchill Date: Mon, 8 Jun 2020 16:53:21 +0800 Subject: [PATCH] =?UTF-8?q?:tada:=20=E6=96=B0=E5=A2=9E=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E8=A7=A3=E5=AF=86=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/util/crypto.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/util/crypto.js diff --git a/package.json b/package.json index bea9900..533372b 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "axios": "^0.18.0", "babel-polyfill": "^6.26.0", "classlist-polyfill": "^1.2.0", + "crypto-js": "^4.0.0", "element-ui": "^2.13.2", "js-base64": "^2.5.1", "js-cookie": "^2.2.0", diff --git a/src/util/crypto.js b/src/util/crypto.js new file mode 100644 index 0000000..99cc332 --- /dev/null +++ b/src/util/crypto.js @@ -0,0 +1,62 @@ +import CryptoJS from 'crypto-js' + +export default class crypto { + //使用AesUtil.genAesKey()生成,需和后端配置保持一致 + static aesKey = "O2BEeIv399qHQNhD6aGW8R8DEj4bqHXm"; + + //使用DesUtil.genDesKey()生成,需和后端配置保持一致 + static desKey = "jMVCBsFGDQr1USHo"; + + /** + * aes 加密方法,同java:AesUtil.encryptToBase64(text, aesKey); + */ + static encryptAES(data, key) { + let dataBytes = CryptoJS.enc.Utf8.parse(data); + let keyBytes = CryptoJS.enc.Utf8.parse(key); + let encrypted = CryptoJS.AES.encrypt(dataBytes, keyBytes, { + iv: keyBytes, + mode: CryptoJS.mode.CBC, + padding: CryptoJS.pad.Pkcs7 + }); + return CryptoJS.enc.Base64.stringify(encrypted.ciphertext); + } + + /** + * aes 解密方法,同java:AesUtil.decryptFormBase64ToString(encrypt, aesKey); + */ + static decryptAES(data, key) { + let keyBytes = CryptoJS.enc.Utf8.parse(key); + let decrypted = CryptoJS.AES.decrypt(data, keyBytes, { + iv: keyBytes, + mode: CryptoJS.mode.CBC, + padding: CryptoJS.pad.Pkcs7 + }); + return CryptoJS.enc.Utf8.stringify(decrypted); + } + + /** + * des 加密方法,同java:DesUtil.encryptToBase64(text, desKey) + */ + static encryptDES(data, key) { + let keyHex = CryptoJS.enc.Utf8.parse(key); + let encrypted = CryptoJS.DES.encrypt(data, keyHex, { + mode: CryptoJS.mode.ECB, + padding: CryptoJS.pad.Pkcs7 + }); + return encrypted.toString(); + } + + /** + * des 解密方法,同java:DesUtil.decryptFormBase64(encryptBase64, desKey); + */ + static decryptDES(data, key) { + let keyHex = CryptoJS.enc.Utf8.parse(key); + let decrypted = CryptoJS.DES.decrypt({ + ciphertext: CryptoJS.enc.Base64.parse(data) + }, keyHex, { + mode: CryptoJS.mode.ECB, + padding: CryptoJS.pad.Pkcs7 + }); + return decrypted.toString(CryptoJS.enc.Utf8); + } +}