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.
32 lines
933 B
32 lines
933 B
|
4 years ago
|
import { CryptoJS} from './cryptoJS.js'
|
||
|
|
const key = "C_s@P<{%";
|
||
|
|
export const encrypt = (str) => {
|
||
|
|
let keyHex = CryptoJS.enc.Utf8.parse(key)
|
||
|
|
var ivHex = CryptoJS.enc.Utf8.parse(key);
|
||
|
|
var encrypted = CryptoJS.DES.encrypt(str, keyHex, {
|
||
|
|
iv:ivHex,mode: CryptoJS.mode.CBC,padding:CryptoJS.pad.Pkcs7
|
||
|
|
}
|
||
|
|
);
|
||
|
|
return encrypted.toString()
|
||
|
|
}
|
||
|
|
|
||
|
|
export const decrypt = (ciphertext) => {
|
||
|
|
let keyHex = CryptoJS.enc.Utf8.parse(key)
|
||
|
|
var ivHex = CryptoJS.enc.Utf8.parse(key);
|
||
|
|
var decrypted = CryptoJS.DES.decrypt({
|
||
|
|
ciphertext: CryptoJS.enc.Hex.parse(base64ToHex(ciphertext)
|
||
|
|
)},keyHex,{
|
||
|
|
iv:ivHex,padding: CryptoJS.pad.Pkcs7
|
||
|
|
});
|
||
|
|
return decrypted.toString(CryptoJS.enc.Utf8);
|
||
|
|
}
|
||
|
|
|
||
|
|
function base64ToHex(str) {
|
||
|
|
const raw = atob(str);
|
||
|
|
let result = '';
|
||
|
|
for (let i = 0; i < raw.length; i++) {
|
||
|
|
const hex = raw.charCodeAt(i).toString(16);
|
||
|
|
result += (hex.length === 2 ? hex : '0' + hex);
|
||
|
|
}
|
||
|
|
return result.toUpperCase();
|
||
|
|
}
|