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;


?>
相关推荐
K1t0几秒前
Android-UI设计
android·ui
吃汉堡吃到饱1 小时前
【Android】浅析MVC与MVP
android·mvc
Flying_Fish_roe7 小时前
linux-网络管理-网络配置
linux·网络·php
深海呐8 小时前
Android AlertDialog圆角背景不生效的问题
android
ljl_jiaLiang8 小时前
android10 系统定制:增加应用使用数据埋点,应用使用时长统计
android·系统定制
花花鱼8 小时前
android 删除系统原有的debug.keystore,系统运行的时候,重新生成新的debug.keystore,来完成App的运行。
android
落落落sss9 小时前
sharding-jdbc分库分表
android·java·开发语言·数据库·servlet·oracle
消失的旧时光-194311 小时前
kotlin的密封类
android·开发语言·kotlin
hzw051013 小时前
Jupyter的使用
ide·python·jupyter