How to decrypt an AES transport stream segment using Golang?

I know, using openssl (tested with OpenSSL 1.1.0g), the following stanza works to decrypt enc.ts , mimetype: video / MP2T, into the h264 segment clear.ts h264 ffplay:

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

Although with Golang https://golang.org/pkg/crypto/cipher/#NewCBCDecrypter I am very confused how the hexadecimal key and iv are set as byte fragments, regardless of whether the block sizes are a factor and how to load and write the file.

I tried:

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)
}

But this leads to a panic: cipher.NewCBCDecrypter: IV length must equal block size. What am I missing?

+4
source share
1 answer

Yours is ivreally too short, so opensl just adds zero for you:

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

Source: https://habr.com/ru/post/1691608/


All Articles