You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.1 KiB
40 lines
1.1 KiB
|
|
import CryptoJS from '@/utils/crypto-js.js'
|
|
|
|
export const encrypt_by_des=(plain_data, key, mode, padding)=> {
|
|
try {
|
|
var cfg = mode ? {mode: mode} : {mode: CryptoJS.mode.ECB};
|
|
if (padding) { cfg.padding = padding; }
|
|
|
|
var d = CryptoJS.enc.Utf8.parse(plain_data);
|
|
var k = CryptoJS.enc.Utf8.parse(key);
|
|
var ed1 = CryptoJS.DES.encrypt(d,k,cfg);
|
|
|
|
var b64 = CryptoJS.enc.Base64.stringify(ed1.ciphertext);
|
|
return b64;
|
|
} catch (e) {
|
|
return '';
|
|
}
|
|
}
|
|
// DES加密
|
|
export const encryptDes = (message, key) => {
|
|
const keyHex = CryptoJS.enc.Utf8.parse(key);
|
|
const encrypted = CryptoJS.DES.encrypt(message, keyHex, {
|
|
mode: CryptoJS.mode.ECB,
|
|
padding: CryptoJS.pad.Pkcs7
|
|
});
|
|
return encrypted.toString();
|
|
}
|
|
|
|
// DES解密
|
|
export const decryptDes = (ciphertext, key) => {
|
|
const keyHex = CryptoJS.enc.Utf8.parse(key);
|
|
// direct decrypt ciphertext
|
|
const decrypted = CryptoJS.DES.decrypt({
|
|
ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
|
|
}, keyHex, {
|
|
mode: CryptoJS.mode.ECB,
|
|
padding: CryptoJS.pad.Pkcs7
|
|
});
|
|
return decrypted.toString(CryptoJS.enc.Utf8);
|
|
}
|