From ac0f07816770eecafeb4d881de758ef45e84e0e7 Mon Sep 17 00:00:00 2001 From: "2312708932@qq.com" Date: Sat, 14 Dec 2024 14:11:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=9E=B6=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- aes/aes.go | 57 +++++++++++++++++++++++++++++++++++++++++++----------- help.go | 2 +- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index dd20fc5..47707b5 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ Usage of bin/goodlink-linux-amd64: -gogo-restart-delay int 自动重启的延迟时间, 单位: 毫秒 (default 1000) -key string - 自定义, 请保证客户端和服务端一致。为避免冲突, 建议16-32个字节长度: {name}_{YYYYMMDDHHMM}, 例如: kony_202412140928 + 自定义, 客户端和服务端必须一致。16-24个字节长度: {name}_{YYYYMMDDHHMM}, 例如: kony_202412140928 -local string 客户端监听的地址端口 -pprof_addr string diff --git a/aes/aes.go b/aes/aes.go index e5fc5f0..78b431f 100644 --- a/aes/aes.go +++ b/aes/aes.go @@ -5,10 +5,30 @@ import ( "crypto/aes" "crypto/cipher" "encoding/base64" + "fmt" + "goodlink/md5" ) +func getKey(key string) []byte { + if len(key) > 24 { + return []byte(key[:24]) + } + + // 填充key + for len(key) < 24 { + key = key + "0" + } + return []byte(key) +} + +func PKCS7Padding(ciphertext []byte, blocksize int) []byte { + padding := blocksize - len(ciphertext)%blocksize + padtext := bytes.Repeat([]byte{byte(padding)}, padding) + return append(ciphertext, padtext...) +} + func Encrypt(origData []byte, key string) string { - k := []byte(key) + k := getKey(key) block, _ := aes.NewCipher(k) blockSize := block.BlockSize() origData = PKCS7Padding(origData, blockSize) @@ -18,9 +38,15 @@ func Encrypt(origData []byte, key string) string { return base64.StdEncoding.EncodeToString(cryted) } +func PKCS7UnPadding(origData []byte) []byte { + length := len(origData) + unpadding := int(origData[length-1]) + return origData[:(length - unpadding)] +} + func Decrypt(cryted []byte, key string) []byte { crytedByte, _ := base64.StdEncoding.DecodeString(string(cryted)) - k := []byte(key) + k := getKey(key) block, _ := aes.NewCipher(k) blockSize := block.BlockSize() blockMode := cipher.NewCBCDecrypter(block, k[:blockSize]) @@ -29,14 +55,23 @@ func Decrypt(cryted []byte, key string) []byte { return PKCS7UnPadding(orig) } -func PKCS7Padding(ciphertext []byte, blocksize int) []byte { - padding := blocksize - len(ciphertext)%blocksize - padtext := bytes.Repeat([]byte{byte(padding)}, padding) - return append(ciphertext, padtext...) -} +func AesTest() { + text := []byte("hello world") -func PKCS7UnPadding(origData []byte) []byte { - length := len(origData) - unpadding := int(origData[length-1]) - return origData[:(length - unpadding)] + temp0 := md5.Encode(string(text)) + fmt.Printf("md5后: %s\n", temp0) + + key := "1234567812345677777777777781238" + temp1 := Encrypt(text, key) + fmt.Printf("加密后: %s\n", temp1) + + temp2 := Decrypt([]byte(temp1), key) + fmt.Printf("解密后: %s\n", temp2) + + key = "123456781238" + temp1 = Encrypt(text, key) + fmt.Printf("加密后: %s\n", temp1) + + temp2 = Decrypt([]byte(temp1), key) + fmt.Printf("解密后: %s\n", temp2) } diff --git a/help.go b/help.go index 664f0fb..93bf733 100644 --- a/help.go +++ b/help.go @@ -32,7 +32,7 @@ func help() { flag.IntVar(&m_cli_redis_id, "redis_id", 15, "Redis服务可使用的表ID") flag.StringVar(&m_cli_tun_local_addr, "local", "", "客户端监听的地址端口") flag.StringVar(&m_cli_tun_remote_addr, "remote", "", "服务端所处网络中, 需要被远程访问的主机地址端口, 例如: 192.168.3.2:9999") - flag.StringVar(&m_cli_tun_key, "key", "", "自定义, 请保证客户端和服务端一致。为避免冲突, 建议16-32个字节长度: {name}_{YYYYMMDDHHMM}, 例如: kony_202412140928") + flag.StringVar(&m_cli_tun_key, "key", "", "自定义, 客户端和服务端必须一致。16-24个字节长度: {name}_{YYYYMMDDHHMM}, 例如: kony_202412140928") flag.Parse()