37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
// crypto.js
|
|
import CryptoJS from 'crypto-js';
|
|
import { JSEncrypt } from "jsencrypt";
|
|
import { getPublicKey } from "@/api/user.js"
|
|
export const Crypto = {
|
|
AES: {
|
|
encrypt: function (plaintext, secretKey) {
|
|
return CryptoJS.AES.encrypt(plaintext, secretKey).toString();
|
|
},
|
|
decrypt: function (ciphertext, secretKey) {
|
|
const bytes = CryptoJS.AES.decrypt(ciphertext, secretKey);
|
|
return bytes.toString(CryptoJS.enc.Utf8);
|
|
}
|
|
}
|
|
};
|
|
export const Encrypt = {
|
|
encrypt: async function (plaintext) {
|
|
let PublicKey = null;
|
|
if (sessionStorage.getItem('PublicKey')) {
|
|
PublicKey = sessionStorage.getItem('PublicKey');
|
|
} else {
|
|
let res = await getPublicKey();
|
|
if (res.IsSuccess) {
|
|
PublicKey = res.Result
|
|
sessionStorage.setItem('PublicKey', res.Result)
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
let encryptor = new JSEncrypt()
|
|
encryptor.setPublicKey(PublicKey)
|
|
return encryptor.encrypt(JSON.stringify(plaintext))
|
|
|
|
},
|
|
|
|
};
|