go - 如何用 Golang 解密 AES 加密的传输流段?

标签 go aes

我知道使用 openssl(使用 OpenSSL 1.1.0g 测试),以下节用于解密 enc.ts , mimetype: video/MP2T, to a ffplay playable clear.ts h264 segment:

openssl aes-128-cbc -d -in enc.ts -out clear.ts -iv 353833383634 -K 9e8c69bcaafa6b636e076935e29986b5 -nosalt

尽管使用 Golang 的 https://golang.org/pkg/crypto/cipher/#NewCBCDecrypter我很困惑如何将十六进制 key 和 iv 设置为 byte slice , block 大小是否是一个因素以及如何加载和写出文件。

我试过:

package main

import (
        "crypto/aes"
        "crypto/cipher"
        "encoding/hex"
        "fmt"
        "io/ioutil"
)

func checkerror(err error) {
        if err != nil {
                panic(err)
        }
}

func main() {
        key, err := hex.DecodeString("9e8c69bcaafa6b636e076935e29986b5")
        checkerror(err)
        iv, err := hex.DecodeString("353833383634")
        checkerror(err)
        ciphertext, err := ioutil.ReadFile("enc.ts")
        checkerror(err)
        block, err := aes.NewCipher(key)
        checkerror(err)
        mode := cipher.NewCBCDecrypter(block, iv)
        mode.CryptBlocks(ciphertext, ciphertext)
        fmt.Printf("%s\n", ciphertext)
}

但这会导致 panic :cipher.NewCB​​CDecrypter: IV length must equal block size。我错过了什么?

最佳答案

您的 iv 确实太短了,所以 openssl 只是为您填充了零:

openssl aes-128-cbc -d -in enc.ts -out clear.ts -iv 353833383634 -K 9e8c69bcaafa6b636e076935e29986b5 -nosalt -P
key=9E8C69BCAAFA6B636E076935E29986B5
iv =35383338363400000000000000000000

关于go - 如何用 Golang 解密 AES 加密的传输流段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48057470/

相关文章:

go - 在 Golang Web 服务器中使用映射处理程序

sockets - 在 Golang 中限制 tcp 下载速度

sockets - 许多客户端连接到 Go 服务器时出错

sql - 为什么我的代码错误(mssql : Violation of PRIMARY KEY constraint 'PK_SMSBlast2' . 无法在对象 'dbo.SMSBlast2' 中插入重复键)?

java - 使用 AES(128) 对音频/视频文件进行加密和解密

.net - AES 加密 .net 到 swift

python - 从 go 调用 python 回调指针

c# - C++ .NET DLL 与 C# 托管代码? (文件加密 AES-128+XTS)

c# - 如何提高 Bouncy CaSTLe API 的加密速度?

cryptography - AES CTR模式的互操作性?