PHP sm4国密加密解密文件

复制代码
<?php
// 打开要加密的 PDF 文件
$sourceFilePath = 'path/to/source.pdf';
$encryptedFilePath = 'path/to/encrypted.pdf';
$decryptedFilePath = 'path/to/decrypted.pdf';

// 生成一个 AES 密钥(256 位)
$key = random_bytes(32);

// 选择一个加密模式(如 CBC)
$cipher = 'aes-256-cbc';

// 生成一个随机的初始化向量(IV)
$iv = random_bytes(openssl_cipher_iv_length($cipher));

// 加密文件内容并保存到新文件
$sourceFile = fopen($sourceFilePath, 'rb');
$encryptedFile = fopen($encryptedFilePath, 'wb');

while (!feof($sourceFile)) {
    $chunk = fread($sourceFile, 8192);
    $encryptedChunk = openssl_encrypt($chunk, $cipher, $key, 0, $iv);
    fwrite($encryptedFile, $encryptedChunk);
}

fclose($sourceFile);
fclose($encryptedFile);

echo 'File encrypted and saved as encrypted.pdf.' . PHP_EOL;

// 解密文件内容并保存到新文件
$encryptedFile = fopen($encryptedFilePath, 'rb');
$decryptedFile = fopen($decryptedFilePath, 'wb');

while (!feof($encryptedFile)) {
    $encryptedChunk = fread($encryptedFile, 8192);
    $decryptedChunk = openssl_decrypt($encryptedChunk, $cipher, $key, 0, $iv);
    fwrite($decryptedFile, $decryptedChunk);
}

fclose($encryptedFile);
fclose($decryptedFile);

echo 'File decrypted and saved as decrypted.pdf.' . PHP_EOL;
?>

phpsm2sm3sm4: php 国密算法 支持 m2 sm3 sm4 SM3WithSM2签名

php 复制代码
<?php

// 打开要加密的 PDF 文件
$sourceFilePath = 'D:\phpstudy_pro\WWW\public\aa\1.pdf';
$encryptedFilePath = 'D:\phpstudy_pro\WWW\public\aa/encrypted.pdf';
$decryptedFilePath = 'D:\phpstudy_pro\WWW\public\aa/decrypted.pdf';



// 生成一个 AES 密钥(256 位)
$key = random_bytes(32);

// 选择一个加密模式(如 CBC)
$cipher = 'aes-256-cbc';

// 生成一个随机的初始化向量(IV)
$iv = random_bytes(openssl_cipher_iv_length($cipher));

// 打开要加密的 PDF 文件
$sourceFile = fopen($sourceFilePath, 'rb');
$sourceContent = fread($sourceFile, filesize($sourceFilePath));
fclose($sourceFile);

// 加密文件内容
$encryptedContent = openssl_encrypt($sourceContent, $cipher, $key, 0, $iv);

// 将加密后的内容保存到新文件
file_put_contents($encryptedFilePath, $encryptedContent);

echo 'File encrypted and saved as encrypted.pdf.'.$iv . PHP_EOL;


$encryptedFilePath = 'D:\phpstudy_pro\WWW\public\aa/encrypted.pdf';
$decryptedFilePath = 'D:\phpstudy_pro\WWW\public\aa/decrypted.pdf';

// 生成一个 AES 密钥(256 位)
//$key = random_bytes(32);

// 选择一个加密模式(如 CBC)
$cipher = 'aes-256-cbc';

// 生成一个随机的初始化向量(IV)
//$iv = random_bytes(openssl_cipher_iv_length($cipher));

// 打开加密的 PDF 文件
$encryptedFile = fopen($encryptedFilePath, 'rb');
$encryptedContent = fread($encryptedFile, filesize($encryptedFilePath));
fclose($encryptedFile);

// 解密文件内容
$decryptedContent = openssl_decrypt($encryptedContent, $cipher, $key, 0, $iv);

// 将解密后的内容保存到新文件
file_put_contents($decryptedFilePath, $decryptedContent);

echo 'File decrypted and saved as decrypted.pdf.' . PHP_EOL;


?>
相关推荐
MonkeyKing_sunyuhua9 分钟前
在 Visual Studio Code (VSCode) 中配置 MCP(Model Context Protocol)
ide·vscode·编辑器
smileNicky13 分钟前
在 VSCode 中运行 Vue.js 项目
ide·vue.js·vscode
Growthofnotes1 小时前
VSCode中Node.js 使用教程
ide·vscode·node.js
志存高远661 小时前
(面试)Android各版本新特性
android
遗憾皆是温柔2 小时前
MyBatis—动态 SQL
java·数据库·ide·sql·mybatis
IT从业者张某某2 小时前
信奥赛-刷题笔记-队列篇-T3-P3662Why Did the Cow Cross the Road II S
android·笔记
未来之窗软件服务2 小时前
Cacti 未经身份验证SQL注入漏洞
android·数据库·sql·服务器安全
BXCQ_xuan2 小时前
handsome主题美化及优化:10.1.0最新版 - 2
android
圈圈编码2 小时前
MVVM框架
android·学习·kotlin
小妖6663 小时前
VScode 的插件本地更改后怎么生效
ide·vscode·编辑器