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;


?>
相关推荐
rorg3 小时前
使用 Laravel 中的自定义存根简化工作
php·laravel
Morpheon6 小时前
Cursor 1.0 版本 GitHub MCP 全面指南:从安装到工作流增强
ide·github·cursor·mcp
猿小猴子7 小时前
主流 AI IDE 之一的 Cursor 介绍
ide·人工智能·cursor
androidwork8 小时前
Android LinearLayout、FrameLayout、RelativeLayout、ConstraintLayout大混战
android·java·kotlin·androidx
每次的天空8 小时前
Android第十三次面试总结基础
android·面试·职场和发展
wu_android8 小时前
Android 相对布局管理器(RelativeLayout)
android
李斯维10 小时前
循序渐进 Android Binder(二):传递自定义对象和 AIDL 回调
android·java·android studio
androidwork10 小时前
OkHttp 3.0源码解析:从设计理念到核心实现
android·java·okhttp·kotlin
像风一样自由11 小时前
【001】frida API分类 总览
android·frida
casual_clover11 小时前
Android 之 kotlin 语言学习笔记四(Android KTX)
android·学习·kotlin