|
|
@@ -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);
|
|
|
+ }
|
|
|
+}
|