Skip to content

Commit 3432e6f

Browse files
committed
fix: 修改加解密问题
1 parent 563661d commit 3432e6f

File tree

1 file changed

+2
-45
lines changed

1 file changed

+2
-45
lines changed

internal/app/iptv/crypto.go

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,12 @@
11
package iptv
22

33
import (
4-
"bytes"
5-
"crypto/des"
64
"encoding/hex"
7-
"errors"
85
"strings"
96

107
"github.com/forgoer/openssl"
118
)
129

13-
// pad 补码函数,将文本填充至块大小的倍数(8字节)
14-
func pad(src []byte) []byte {
15-
padding := des.BlockSize - len(src)%des.BlockSize
16-
padText := bytes.Repeat([]byte{byte(padding)}, padding)
17-
return append(src, padText...)
18-
}
19-
20-
// unPad 去补码函数,移除填充的数据
21-
func unPad(src []byte) ([]byte, error) {
22-
length := len(src)
23-
if length == 0 {
24-
return nil, errors.New("input is empty")
25-
}
26-
27-
// 获取填充的长度
28-
unPadding := int(src[length-1])
29-
30-
// 检查填充长度是否有效
31-
if unPadding <= 0 || unPadding > length {
32-
return nil, errors.New("invalid padding size")
33-
}
34-
35-
return src[:(length - unPadding)], nil
36-
}
37-
38-
// removePaddingCharacters 去除补码字符
39-
func removePaddingCharacters(src []byte) ([]byte, error) {
40-
// 去除 '\x08' 字符
41-
src = bytes.ReplaceAll(src, []byte{'\x08'}, []byte{})
42-
return src, nil
43-
}
44-
4510
type TripleDESCrypto struct {
4611
key []byte
4712
}
@@ -62,9 +27,7 @@ func NewTripleDESCrypto(key string) *TripleDESCrypto {
6227

6328
// ECBEncrypt 加密函数,返回十六进制字符串
6429
func (c *TripleDESCrypto) ECBEncrypt(plainText string) (string, error) {
65-
// 补码
66-
paddedText := pad([]byte(plainText))
67-
encrypted, err := openssl.Des3ECBEncrypt(paddedText, c.key, openssl.PKCS7_PADDING)
30+
encrypted, err := openssl.Des3ECBEncrypt([]byte(plainText), c.key, openssl.PKCS7_PADDING)
6831
if err != nil {
6932
return "", err
7033
}
@@ -84,11 +47,5 @@ func (c *TripleDESCrypto) ECBDecrypt(cipherText string) (string, error) {
8447
return "", err
8548
}
8649

87-
// 去除补码
88-
result, err := removePaddingCharacters(decrypted)
89-
if err != nil {
90-
return "", err
91-
}
92-
93-
return string(result), nil
50+
return string(decrypted), nil
9451
}

0 commit comments

Comments
 (0)