decrypt_pdf.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import pako from 'pako';
  2. // 反混淆:URL安全 Base64 -> 标准 Base64
  3. function deobfuscate(obfStr: string) {
  4. let base64 = obfStr.replace(/-/g, '+').replace(/_/g, '/');
  5. while (base64.length % 4) base64 += '=';
  6. return base64;
  7. }
  8. // Base64 转 Uint8Array
  9. function b642ab(base64: string) {
  10. const binary = atob(base64);
  11. const len = binary.length;
  12. const bytes = new Uint8Array(len);
  13. for (let i = 0; i < len; i++) {
  14. bytes[i] = binary.charCodeAt(i);
  15. }
  16. return bytes.buffer;
  17. }
  18. // 主函数:反混淆 + 解压成字符串
  19. function deobfuscateAndDecompress(obfStr: string): Uint8Array {
  20. const b64 = deobfuscate(obfStr);
  21. const compressedBuf = b642ab(b64);
  22. const decompressed = pako.inflate(new Uint8Array(compressedBuf));
  23. return decompressed;
  24. }
  25. export async function decrypt(url: string): Promise<Uint8Array | undefined> {
  26. const res = await fetch(url).catch((err) => {
  27. console.error('Error during fetch:', err)
  28. return undefined
  29. })
  30. if (!res) return undefined
  31. const buffer = await res.text().catch((err) => {
  32. console.error('Error during get text:', err)
  33. return undefined
  34. })
  35. if (!buffer) return undefined
  36. return deobfuscateAndDecompress(buffer)
  37. }