源码分享-M3U8数据流ts的AES-128解密并合并---GoLang实现

之前使用C语言实现了一次,见M3U8数据流ts的AES-128解密并合并。

学习了Go语言后,又用Go重新实现了一遍。源码如下,无第三方库依赖。

package main

import (
	"crypto/aes"
	"crypto/cipher"
	"encoding/binary"
	"fmt"
	"io"
	"os"
	"strconv"
)

func decodeAES128CBC(key []byte, index int, inFile string, out io.Writer) error {
	block, err := aes.NewCipher(key)
	if err != nil {
		return err
	}
	inBuf, err := os.ReadFile(inFile)
	if err != nil || len(inBuf) == 0 {
		return err
	}

	var iv [16]byte
	binary.BigEndian.PutUint32(iv[12:], uint32(index))

	outBuf := make([]byte, len(inBuf))
	mode := cipher.NewCBCDecrypter(block, iv[:])
	mode.CryptBlocks(outBuf, inBuf)

	pad := int(outBuf[len(outBuf)-1])
	_, err = out.Write(outBuf[:len(outBuf)-pad])
	return err
}

func decodeAesM3u8(key, inPath, outFile string) (int, error) {
	out, err := os.Create(outFile)
	if err != nil {
		return 0, err
	}
	defer out.Close()

	for i := 0; ; i++ {
		err = decodeAES128CBC([]byte(key), i, inPath+strconv.Itoa(i), out)
		if err != nil {
			if i == 0 {
				return 0, err
			}
			return i, nil
		}
	}
}

func mergeM3u8(inPath, outFile string) (int, error) {
	out, err := os.Create(outFile)
	if err != nil {
		return 0, err
	}
	defer out.Close()

	for i := 0; ; i++ {
		in, err := os.Open(inPath + strconv.Itoa(i))
		if err != nil {
			if i == 0 {
				return 0, err
			}
			return i, nil
		}

		_, err = io.Copy(out, in)
		if err != nil {
			_ = in.Close()
			return i, err
		}

		err = in.Close()
		if err != nil {
			return i, err
		}
	}
}

func main() {
	inPath, key, outFile := os.Args[1], os.Args[2], os.Args[3]

	var n int
	var err error
	if len(key) == 16 {
		n, err = decodeAesM3u8(key, inPath, outFile)
	} else {
		n, err = mergeM3u8(inPath, outFile)
	}
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("successfully converted", n, "files")
}

你可能感兴趣的:(源码分析,M3U8,AES-128,ts,Go)