当前位置: 首页 > news >正文

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// 打开要加密的 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;?>

http://www.lryc.cn/news/119701.html

相关文章:

  • CBCGPRibbon 添加Edit、Combox、Hyperlink控件
  • 漫话拥塞控制:BBR 是个单流模型
  • HTML详解连载(1)
  • 最新版本2023UI千月影视APP源码 开源完美版前后端完美匹配 后端基于ThinkPHP框架
  • centos7安装Docker详细步骤(无坑版教程)
  • Python入门自学进阶-Web框架——39、redis、rabbitmq、git——2
  • 了解IL汇编跳转语句
  • JVM运行时五大数据区域详解
  • Vuex 使用教程
  • springboot启动you will need to add ‘org.slf4j‘ to prefer-application-packages异常解决
  • 云原生核心原则和特征
  • 【ElasticSearch入门】
  • SQL | 注释
  • oi知识表+NOIP提高组算法及算法思想总结
  • 【mysql】实现递归查询
  • JUC并发编程之原子类
  • 测试设计中隐藏的边界有哪些?
  • 领航优配:暑期旅游市场热度持续攀升,相关公司业绩有望持续释放
  • 基于 CentOS 7 构建 LVS-DR 集群 及 配置nginx负载均衡
  • docker搭建在线Markdown服务器
  • 打靶练习:WestWild 1.1(一个简单但不失优雅的Ubuntu靶机)
  • 【2.3】Java微服务:sentinel服务哨兵
  • 【C++】开源:abseil-cpp基础组件库配置使用
  • 【GPT-3 】创建能写博客的AI工具
  • [保研/考研机试] KY35 最简真分数 北京大学复试上机题 C++实现
  • 算法备案后,企业需要做什么?合规与执行挑战
  • 云原生应用程序的自动化管理和编排
  • Spring项目整合过滤链模式~实战应用
  • FFmpeg常见命令行(五):FFmpeg滤镜使用
  • 网络编程 tcp udp http编程流程 网络基础知识