Skip to content

Commit

Permalink
fix: 修改加解密问题
Browse files Browse the repository at this point in the history
  • Loading branch information
super321 committed Jan 10, 2025
1 parent 563661d commit 3432e6f
Showing 1 changed file with 2 additions and 45 deletions.
47 changes: 2 additions & 45 deletions internal/app/iptv/crypto.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,12 @@
package iptv

import (
"bytes"
"crypto/des"
"encoding/hex"
"errors"
"strings"

"github.com/forgoer/openssl"
)

// pad 补码函数,将文本填充至块大小的倍数(8字节)
func pad(src []byte) []byte {
padding := des.BlockSize - len(src)%des.BlockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padText...)
}

// unPad 去补码函数,移除填充的数据
func unPad(src []byte) ([]byte, error) {
length := len(src)
if length == 0 {
return nil, errors.New("input is empty")
}

// 获取填充的长度
unPadding := int(src[length-1])

// 检查填充长度是否有效
if unPadding <= 0 || unPadding > length {
return nil, errors.New("invalid padding size")
}

return src[:(length - unPadding)], nil
}

// removePaddingCharacters 去除补码字符
func removePaddingCharacters(src []byte) ([]byte, error) {
// 去除 '\x08' 字符
src = bytes.ReplaceAll(src, []byte{'\x08'}, []byte{})
return src, nil
}

type TripleDESCrypto struct {
key []byte
}
Expand All @@ -62,9 +27,7 @@ func NewTripleDESCrypto(key string) *TripleDESCrypto {

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

// 去除补码
result, err := removePaddingCharacters(decrypted)
if err != nil {
return "", err
}

return string(result), nil
return string(decrypted), nil
}

0 comments on commit 3432e6f

Please sign in to comment.