我可以解密javascript中php完成的加密pdf文件吗?

时间:2019-12-06 05:06:04

标签: javascript php

我已使用以下代码加密了pdf文件

<?php
// Key for encryption and decryption
$key = '1323214214124325bw4tertbretbertertetrt';

function my_encrypt($data, $key) {
    // Remove the base64 encoding from our key
    $encryption_key = base64_decode($key);
    // Generate an initialization vector
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
    // Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
    $encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);
    // The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
    return base64_encode($encrypted . '::' . $iv);
}

function my_decrypt($data, $key) {
    // Remove the base64 encoding from our key
    $encryption_key = base64_decode($key);
    // To decrypt, split the encrypted data from our IV - our unique separator used was "::"
    list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);
    return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
}

$msg = file_get_contents('/home/ttpl50/Downloads/e-2018.pdf');
$msg_encrypted = my_encrypt($msg, $key);
$file = fopen('/home/ttpl50/Downloads/example.pdf', 'wb');
fwrite($file, $msg_encrypted);
fclose($file);
echo "File Encryption successfull" . PHP_EOL;

$msg = file_get_contents('/home/ttpl50/Downloads/example.pdf');
$msg_decrypted = my_decrypt($msg, $key);
$file = fopen('/home/ttpl50/Downloads/decrypt.pdf', 'wb');
fwrite($file, $msg_decrypted);
fclose($file);
echo PHP_EOL . "File Decryption successfull";

unlink('/home/ttpl50/Downloads/example.pdf');

此代码在php中效果很好,但是我想将加密的文件发送到浏览器,并使用javascript在浏览器本身中解密。如果有人单击pdf的下载或打印按钮,我想请求加密的pdf文件。有什么办法可以解密由php加密的javascript文件?

0 个答案:

没有答案