Skip to content

Commit

Permalink
feat: 旧版兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
miaoyin committed Nov 10, 2023
1 parent 91ae2d0 commit 66db3da
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cfg/cfg_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func ExampleCfg_String() {
}

func ExampleCfg_Read() {
data, err := cfg.New("key").Read([]byte(`a=AES(oY8aex0d4WjWokMZSUDyDQ==)
b=DES(azzeIdDM09M=)
data, err := cfg.New("key").Read([]byte(`a=AES(A/43wTj2AVQboZZ0lNMqbw==)
b=DES(LABOK5l6Q64=)
c=DES[abc]`))

fmt.Println(string(data))
Expand Down
6 changes: 3 additions & 3 deletions cfg/cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
)

const (
_file = "test.toml"
_data = `k1 = "AES(oY8aex0d4WjWokMZSUDyDQ==)"
k2 = "DES(Jiga4xHtvWM=)"
_data = `k1 = "AES(A/43wTj2AVQboZZ0lNMqbw==)"
k2 = "DES(LABOK5l6Q64=)"
k3 = "DES[中文]"`
_file = "test.toml"
_keyErr = "key err"
)

Expand Down
78 changes: 68 additions & 10 deletions cfg/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,35 @@ func (p Cipher) Encrypt(src, key string) string {
return base64.StdEncoding.EncodeToString(p.EncryptBytes([]byte(src), key))
}

func (p Cipher) EncodeToString(src, key string) string {
return fmt.Sprintf("%s(%s)", p.String(), p.Encrypt(src, key))
}

func (p Cipher) EncryptBytes(src []byte, key string) []byte {
blockMode, blockSize := p.Block(key, true)
if p == AESMD5 || p == DESMD5 {
blockMode, blockSize := p.BlockMode(key, true)

src = pkcs5Padding(src, blockSize)

cryted := make([]byte, len(src))
blockMode.CryptBlocks(cryted, src)

return cryted
}

src = pkcs5Padding(src, blockSize)
var (
ret []byte
block = p.Block(key)
srcBytes = Padding(src, block.BlockSize())
tmp = make([]byte, block.BlockSize())
)

cryted := make([]byte, len(src))
blockMode.CryptBlocks(cryted, src)
for index := 0; index < len(srcBytes); index += block.BlockSize() {
block.Encrypt(tmp, srcBytes[index:index+block.BlockSize()])
ret = append(ret, tmp...)
}

return cryted
return ret
}

func (p Cipher) Decrypt(src, key string) (string, error) {
Expand All @@ -53,21 +73,59 @@ func (p Cipher) Decrypt(src, key string) (string, error) {
}

func (p Cipher) DecryptBytes(src []byte, key string) ([]byte, error) {
if p == AESMD5 || p == DESMD5 {
var (
blockMode, _ = p.BlockMode(key, false)
orig = make([]byte, len(src))
)

blockMode.CryptBlocks(orig, src)

return pkcs5Trimming(orig)
}

var (
blockMode, _ = p.Block(key, false)
orig = make([]byte, len(src))
ret []byte
block = p.Block(key)
tmp = make([]byte, block.BlockSize())
)

blockMode.CryptBlocks(orig, src)
for index := 0; index < len(src); index += block.BlockSize() {
block.Decrypt(tmp, src[index:index+block.BlockSize()])
ret = append(ret, tmp...)
}

ret, err := UnPadding(ret)
if err != nil {
return nil, err
}

if _checkRegex.Match(ret) {
return ret, nil
}

return pkcs5Trimming(orig)
return nil, ErrKey
}

func (p Cipher) String() string {
return _names[p]
}

func (p Cipher) Block(key string, isEnc bool) (cipher.BlockMode, int) {
func (p Cipher) Block(key string) cipher.Block {
keyBytes := sha256.Sum256([]byte(key))
if p == DES {
// nolint
block, _ := des.NewCipher(keyBytes[:8])

return block
}

block, _ := aes.NewCipher(keyBytes[:])

return block
}

func (p Cipher) BlockMode(key string, isEnc bool) (cipher.BlockMode, int) {
var (
keyBytes []byte
block cipher.Block
Expand Down
9 changes: 9 additions & 0 deletions cfg/cipher_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ func ExampleIsEncrypt() {
// false
}

func ExampleCipher_decrypt2() {
fmt.Println(
cfg.DES.Decrypt("HLZn0k/XGi5fmu2OCXsOuNdsstzF/2JCsY7q38hQEwSYFpGlO036ypruiYtMJmDB64JNZiiYh0sntLsphrX36A==", "woda"),
)

// Output:
// TXprMU5tRTVZV1pqTW1Kak5ESTVNV0l4TkdNME4yRmhOMlppTURZM05HVQ== <nil>
}

func ExampleCipher_Decrypt() {
fmt.Println(cfg.AESMD5.Decrypt(cfg.AESMD5.Encrypt("AESMD5", "pass"), "pass"))
fmt.Println(cfg.DESMD5.Decrypt(cfg.DESMD5.Encrypt("DESMD5", "pass"), "pass"))
Expand Down
40 changes: 39 additions & 1 deletion cfg/padding.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cfg

import "bytes"
import (
"bytes"
"crypto/rand"
)

func pkcs5Trimming(encrypt []byte) ([]byte, error) {
var (
Expand All @@ -21,3 +24,38 @@ func pkcs5Padding(cipherText []byte, blockSize int) []byte {

return append(cipherText, padText...)
}

func Padding(cipherText []byte, blockSize int) []byte {
var (
padding = getPaddingSize(cipherText, blockSize)
padData = make([]byte, padding-1)
)

_, _ = rand.Read(padData)
// nolint
padData = append(padData, byte(padding))

return append(cipherText, padData...)
}

func UnPadding(cipherText []byte) ([]byte, error) {
var (
length = len(cipherText)
cipherLen = int(cipherText[length-1])
)

if length < cipherLen {
return nil, ErrKey
}

return cipherText[:length-cipherLen], nil
}

func getPaddingSize(cipherText []byte, blockSize int) int {
remainder := len(cipherText) % blockSize
if remainder == 0 {
return blockSize
}

return blockSize - remainder
}

0 comments on commit 66db3da

Please sign in to comment.