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;


?>
相关推荐
SY.ZHOU1 小时前
Flutter 与原生通信
android·flutter·ios
Wgllss1 小时前
Android监听开机自启,是否在前后台,锁屏界面,息屏后自动亮屏,一直保持亮屏
android·架构·android jetpack
_一条咸鱼_2 小时前
大厂Android面试秘籍:Activity 组件间通信
android·面试·android jetpack
张小凡vip2 小时前
pycharm已有python3.7,如何新增Run Configurations中的Python interpreter为python 3.9
ide·python·pycharm
小白鼠零号2 小时前
记录 | Pycharm中如何调用Anaconda的虚拟环境
ide·python·pycharm
冉冉同学2 小时前
【HarmonyOS NEXT】解决微信浏览器无法唤起APP的问题
android·前端·harmonyos
韶博雅3 小时前
mysql表类型查询
android·数据库·mysql
studyForMokey3 小时前
【Android学习记录】工具使用
android·学习
小wanga3 小时前
【MySQL】索引特性
android·数据库·mysql
牛了爷爷4 小时前
php伪协议
android·开发语言·php