viewer

64 阅读1分钟

  async function loadAndDecryptPdf() { try { // 请求后端获取加密的PDF数据和密钥 const response = await fetch('/encrypted-pdf'); const encryptedData = await response.arrayBuffer(); const key = response.headers.get('Encryption-Key');

    // 将ArrayBuffer转换为WordArray
    const encryptedWordArray = CryptoJS.lib.WordArray.create(new Uint8Array(encryptedData));

    // 使用AES解密
    const decrypted = CryptoJS.AES.decrypt(
        { ciphertext: encryptedWordArray },
        CryptoJS.enc.Utf8.parse(key),
        { mode: CryptoJS.mode.ECB } // 根据加密模式调整
    );

    // 转换为Uint8Array
    const decryptedArray = new Uint8Array(decrypted.toString(CryptoJS.enc.Latin1).length);
    for (let i = 0; i < decryptedArray.length; i++) {
        decryptedArray[i] = decrypted.toString(CryptoJS.enc.Latin1).charCodeAt(i);
    }

    // 生成Blob URL
    const blob = new Blob([decryptedArray], { type: 'application/pdf' });
    const url = URL.createObjectURL(blob);

    // 使用pdf.js查看器打开
    window.open(`/pdfjs/web/viewer.html?file=${encodeURIComponent(url)}`, '_blank');
} catch (error) {
    console.error('解密失败:', error);
}

}

// 调用函数 loadAndDecryptPdf();