惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
罗磊的独立博客
人人都是产品经理
人人都是产品经理
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
L
Lohrmann on Cybersecurity
博客园 - 【当耐特】
量子位
Last Week in AI
Last Week in AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
Cyber Attacks, Cyber Crime and Cyber Security
腾讯CDC
有赞技术团队
有赞技术团队
Cyberwarzone
Cyberwarzone
T
Tor Project blog
V
V2EX
L
LINUX DO - 热门话题
Security Latest
Security Latest
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
NISL@THU
NISL@THU
C
Cisco Blogs
T
Tailwind CSS Blog
G
GRAHAM CLULEY
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
小众软件
小众软件
K
Kaspersky official blog
博客园 - 司徒正美
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
S
Schneier on Security
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
Scott Helme
Scott Helme
J
Java Code Geeks
博客园 - 聂微东
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
C
Cybersecurity and Infrastructure Security Agency CISA
F
Fortinet All Blogs
T
Threat Research - Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
雷峰网
雷峰网

PHP

新版六国语言小额 JieDai 系统源码 - V2EX 免费虚拟主机空间安装的一个 PHP 文件论坛 - V2EX 有没有什么好用的免费开源 CRM 系统 - V2EX 2026 年 PHP 路在何方 - V2EX 2026 年,学 laravel 有没有前途: - V2EX 有没有这样的 PHP 框架 - V2EX PHP 是不是快死了 - V2EX php74-fpm 加 nginx, PHP 文件只要不是 root /var/www/html 就提示 No input file specified.求大佬看看 - V2EX PHP framewok 框架推荐 - V2EX 基于 yii2 框架开发的后台管理软件,免费 - V2EX 「一键部署你的专属服务器」——WNMP 一键包,让 Web 环境搭建回归简单 - V2EX 完蛋了,为什么我感觉 PHP 的语法这么优雅呢 - V2EX 未来属于 PHP - V2EX 记一次微信 access_token invalid credential, access_token is invalid or not latest - V2EX 重造 PHP -HTTP 性能检测,新增 List<int>、HashMap<K, V> - V2EX 求助: PHP 错误,请高手帮我改写下面的 PHP 代码 - V2EX 为什么现在 WEB3、区块链、钱包之类开始用 PHP 了 - V2EX PHP 8.5 加入了 pipe 语法 - V2EX 推荐一个优雅的 PHP http 请求工具,仿照了 py 的 requests - V2EX 大家好,我又來了,新作品 https://www.freetalkhub.com, PHP 开发 - V2EX laravel 和 thinkphp 选择哪个? - V2EX 202505 最新调研: PHP Opcode 加密混淆哪家强? - V2EX 有做过基于 webrtc 技术的多人视频会议系统的吗 - V2EX 2025 年 PHP 路在何方 - V2EX 求助 CodeIgniter 输出的 html 格式很乱,缩进乱七八糟的。。。 - V2EX 有个关于 PHP 的小疑惑 - V2EX workman 与阿里云实时语音通信的问题。 - V2EX 目前 PHP 中比较好用的工作流引擎有哪些? - V2EX 有老哥熟悉 OpenCart 的吗? - V2EX 开源 PHP Composer 私有 package 管理平台 - V2EX 推荐个好用的集成环境 - V2EX phpinfo(); logo 变成了绿色的大象 - V2EX PHP 项目有类似前端 monorepo 的概念吗? 想用 PHP 做微服务开发,有偿求指导 - V2EX PHP 语言已经过气了吗 - V2EX 国内 PHP 卷王是鸟哥 mysql update 更新失败的原因。 - V2EX PHP 程序员是往运维还是前端发展作为拓展呢 - V2EX 求大佬解决一下一个问题 - V2EX [2024-11-21] PHP 8.4 Released! - V2EX 如何获得抖音视频播放量?各位大牛支支招~ 如何优雅地切换 composer 镜像 - V2EX Laravel 二手项目,语言切换问题,求解 - V2EX 两道 PHP 题目,都是求 flag 值 帮忙推荐一个开源论坛源码? Laravel 完成了 A 轮融资🎉 - V2EX PHP 虽然没落了,但是 PHP 的东西是真的好用 - V2EX 世界上最好的 serverless 平台! - V2EX imgur 是怎么做到一个链接新窗口打开 302,但是还能作为图片显示的? - V2EX PHP 编译器 BPC 编译 ThinkPHP8 + PHPUnit 测试 的视频来了 - V2EX
与银行对接 sm4 国密算法 - V2EX
cbasil · 2024-10-31 · via PHP
cbasil

14

cbasil      2024 年 10 月 31 日

@ca2oh4 我当时也考虑用 golang 写一个脚本,然后 php 通过 http 调用。不过后面解决了就不用了。这是当时写的 golang 案例

```golang
package main

import (
"bytes"
"crypto/cipher"
"encoding/hex"
"fmt"

"github.com/tjfoc/gmsm/sm4"
)

// PKCS5Padding 使用 PKCS5 填充
func PKCS5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}

// PKCS5UnPadding 去除 PKCS5 填充
func PKCS5UnPadding(src []byte) []byte {
length := len(src)
unpadding := int(src[length-1])
return src[:(length - unpadding)]
}

// SM4 CBC 模式加密
func sm4CBCEncrypt(key, plaintext, iv []byte) ([]byte, error) {
block, err := sm4.NewCipher(key)
if err != nil {
return nil, err
}

plaintext = PKCS5Padding(plaintext, block.BlockSize())
ciphertext := make([]byte, len(plaintext))
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, plaintext)
return ciphertext, nil
}

// SM4 CBC 模式解密
func sm4CBCDecrypt(key, ciphertext, iv []byte) ([]byte, error) {
block, err := sm4.NewCipher(key)
if err != nil {
return nil, err
}

plaintext := make([]byte, len(ciphertext))
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(plaintext, ciphertext)
plaintext = PKCS5UnPadding(plaintext)
return plaintext, nil
}

func main() {
key, _ := hex.DecodeString("key") // 16 字节的十六进制密钥
iv, _ := hex.DecodeString("iv") // 16 字节的 IV
plaintext := []byte("This is a secret message.")

// 加密
ciphertext, err := sm4CBCEncrypt(key, plaintext, iv)
if err != nil {
fmt.Println("Encryption error:", err)
return
}
fmt.Printf("Ciphertext (hex): %s\n", hex.EncodeToString(ciphertext))
// 解密
decrypted, err := sm4CBCDecrypt(key, ciphertext, iv)
if err != nil {
fmt.Println("Decryption error:", err)
return
}
fmt.Printf("Decrypted text: %s\n", decrypted)
}
```