Files
tms-erp-web/src/utils/crypto.js

95 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import CryptoJS from 'crypto-js';
export default class crypto {
/**
* token加密key 使用@org.springblade.test.CryptoKeyGenerator获取,需和后端配置保持一致
* @type {string}
*/
static cryptoKey = 'NWmP0cuIWAfL6GPeLJlbY8898b5iPk1y';
/**
* 报文加密key 使用@org.springblade.test.CryptoKeyGenerator获取,需和后端配置保持一致
* @type {string}
*/
static aesKey = 'gLJvoZfX9pOp3nkIdQ9U2DzxlzsrTvJC';
/**
* 报文加密key 使用@org.springblade.test.CryptoKeyGenerator获取,需和后端配置保持一致
* @type {string}
*/
static desKey = 'hdlxNCD2KZI91Taq';
/**
* aes 加密方法
* @param data
* @returns {*}
*/
static encrypt(data) {
return this.encryptAES(data, this.aesKey);
}
/**
* aes 解密方法
* @param data
* @returns {*}
*/
static decrypt(data) {
return this.decryptAES(data, this.aesKey);
}
/**
* aes 加密方法同javaAesUtil.encryptToBase64(text, aesKey);
*/
static encryptAES(data, key) {
const dataBytes = CryptoJS.enc.Utf8.parse(data);
const keyBytes = CryptoJS.enc.Utf8.parse(key);
const encrypted = CryptoJS.AES.encrypt(dataBytes, keyBytes, {
iv: keyBytes,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
/**
* aes 解密方法同javaAesUtil.decryptFormBase64ToString(encrypt, aesKey);
*/
static decryptAES(data, key) {
const keyBytes = CryptoJS.enc.Utf8.parse(key);
const decrypted = CryptoJS.AES.decrypt(data, keyBytes, {
iv: keyBytes,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
return CryptoJS.enc.Utf8.stringify(decrypted);
}
/**
* des 加密方法同javaDesUtil.encryptToBase64(text, desKey)
*/
static encryptDES(data, key) {
const keyHex = CryptoJS.enc.Utf8.parse(key);
const encrypted = CryptoJS.DES.encrypt(data, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
return encrypted.toString();
}
/**
* des 解密方法同javaDesUtil.decryptFormBase64(encryptBase64, desKey);
*/
static decryptDES(data, key) {
const keyHex = CryptoJS.enc.Utf8.parse(key);
const 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);
}
}