一朝墩、下載插件
npm install @zip.js/zip.js
npm install crypto-js
npm install file-saver
二、將數(shù)據(jù)加密與解密
1.將數(shù)據(jù)加密
var key = CryptoJS.enc.Utf8.parse('key');
var iv = CryptoJS.enc.Utf8.parse('iv');
var encrypted = CryptoJS.AES.encrypt("Hello, this is a secret message!", key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
var encryptedString = encrypted.toString();
2.將數(shù)據(jù)解密
let decryptString = CryptoJS.AES.decrypt(encryptedString, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString(CryptoJS.enc.Utf8)
3.將文件進(jìn)行加密
const reader = new FileReader();
reader.onload = async (e) => {
const content = e.target.result;
// 使用CryptoJS加密內(nèi)容
var encrypted = CryptoJS.AES.encrypt(content, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
var encryptedContent = encrypted.toString();
};
reader.readAsDataURL(this.files); // 讀取文件內(nèi)容為文本
三蜡峰、對數(shù)據(jù)與文件生產(chǎn)壓縮包
const TEXT_CONTENT = "xxx,xxx,x,xx,xxxx"
const FILENAME = "demo.docx";
let password = "password";
const BLOB = new Blob([TEXT_CONTENT], { type: zip.getMimeType(FILENAME) });
const fileBlob = new Blob([this.files], { type: zip.getMimeType(this.files.name) });
const blobWriter = new zip.BlobWriter("application/zip");
const zipWriter = new zip.ZipWriter(blobWriter);
await zipWriter.add(FILENAME, new zip.BlobReader(BLOB), { password: password, zipCrypto: true });
await zipWriter.add("demo.txt", new zip.TextReader("hello world"), { password: password, zipCrypto: true });
await zipWriter.add(this.files.name, new zip.BlobReader(fileBlob), { password: password, zipCrypto: true });
const blob = await zipWriter.close();
FileSaver.saveAs(blob, 'encrypted.zip'); // 下載文件
// 轉(zhuǎn)換 blob 為 file對象 進(jìn)行文件上傳功能
const file = new File([blob], "encrypted.zip", { type: blob.type })