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;


?>
相关推荐
IAM四十二9 天前
Google 端侧 AI 框架 LiteRT 初探
android·深度学习·tensorflow
CYRUS_STUDIO9 天前
手把手教你用 Chrome 断点调试 Frida 脚本,JS 调试不再是黑盒
android·app·逆向
wuk9989 天前
深入理解PHP中的生成器(Generators)
开发语言·php
A5资源网9 天前
为WordPress 网站创建一个纯文本网站地图(Sitemap)
前端·数据仓库·html·php
PHP武器库9 天前
[Hestia]开源网络服务器控制面板,快速、可靠、开源
运维·服务器·网络·开源·php
Just丶Single9 天前
安卓NDK初识
android
编程乐学9 天前
网络资源模板--基于Android Studio 实现的咖啡点餐App
android·android studio·大作业·奶茶点餐·安卓移动开发·咖啡点餐
二流小码农9 天前
鸿蒙开发:基于node脚本实现组件化运行
android·ios·harmonyos
Wgllss9 天前
Kotlin+协程+FLow+Channel+Compose 实现一个直播多个弹幕效果
android·架构·android jetpack
顾林海9 天前
Android WebView内存释放全解析:从泄漏检测到彻底释放的实战指南
android·面试·性能优化