-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdecrypt.go
42 lines (37 loc) · 978 Bytes
/
decrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package hlsdl
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/binary"
)
const (
syncByte = uint8(71) //0x47
)
func decryptAES128(crypted, key, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, iv[:blockSize])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = pkcs5UnPadding(origData)
return origData, nil
}
func pkcs5Padding(cipherText []byte, blockSize int) []byte {
padding := blockSize - len(cipherText)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(cipherText, padText...)
}
func pkcs5UnPadding(origData []byte) []byte {
length := len(origData)
unPadding := int(origData[length-1])
return origData[:(length - unPadding)]
}
func defaultIV(seqID uint64) []byte {
buf := make([]byte, 16)
binary.BigEndian.PutUint64(buf[8:], seqID)
return buf
}