mirror of
https://gitee.com/konyshe/goodlink.git
synced 2025-09-26 20:51:22 +08:00
key使用md5加密
This commit is contained in:
57
aes/aes.go
Normal file
57
aes/aes.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package aes
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func Encrypt(orig string, key string) string {
|
||||
origData := []byte(orig)
|
||||
k := []byte(key)
|
||||
block, _ := aes.NewCipher(k)
|
||||
blockSize := block.BlockSize()
|
||||
origData = PKCS7Padding(origData, blockSize)
|
||||
blockMode := cipher.NewCBCEncrypter(block, k[:blockSize])
|
||||
cryted := make([]byte, len(origData))
|
||||
blockMode.CryptBlocks(cryted, origData)
|
||||
return base64.StdEncoding.EncodeToString(cryted)
|
||||
}
|
||||
|
||||
func Decrypt(cryted string, key string) string {
|
||||
crytedByte, _ := base64.StdEncoding.DecodeString(cryted)
|
||||
k := []byte(key)
|
||||
block, _ := aes.NewCipher(k)
|
||||
blockSize := block.BlockSize()
|
||||
blockMode := cipher.NewCBCDecrypter(block, k[:blockSize])
|
||||
orig := make([]byte, len(crytedByte))
|
||||
blockMode.CryptBlocks(orig, crytedByte)
|
||||
orig = PKCS7UnPadding(orig)
|
||||
return string(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 PKCS7UnPadding(origData []byte) []byte {
|
||||
length := len(origData)
|
||||
unpadding := int(origData[length-1])
|
||||
return origData[:(length - unpadding)]
|
||||
}
|
||||
|
||||
func EncodeTest() {
|
||||
orig := "hello world"
|
||||
key := "123456781234567812345678"
|
||||
fmt.Println("ԭ<>ģ<EFBFBD>", orig)
|
||||
|
||||
encryptCode := Encrypt(orig, key)
|
||||
fmt.Println("<22><><EFBFBD>ģ<EFBFBD>", encryptCode)
|
||||
|
||||
decryptCode := Decrypt(encryptCode, key)
|
||||
fmt.Println("<22><><EFBFBD>ܽ<EFBFBD><DCBD><EFBFBD><EFBFBD><EFBFBD>", decryptCode)
|
||||
}
|
15
main.go
15
main.go
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"gogo"
|
||||
"goodlink/md5"
|
||||
"goodlink/tunnel"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -18,13 +19,23 @@ func main2() {
|
||||
|
||||
if m_cli_tun_local_addr != "" {
|
||||
go func() {
|
||||
if err := tunnel.ProcessClient(m_cli_tun_local_addr, m_cli_redis_addr, m_cli_redis_pass, m_cli_redis_id, m_cli_tun_key, true); err != nil {
|
||||
if err := tunnel.ProcessClient(m_cli_tun_local_addr,
|
||||
m_cli_redis_addr,
|
||||
m_cli_redis_pass,
|
||||
m_cli_redis_id,
|
||||
md5.Encode(m_cli_tun_key),
|
||||
true); err != nil {
|
||||
|
||||
log.Println(err)
|
||||
os.Exit(0)
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
go tunnel.ProcessServer(m_cli_tun_remote_addr, m_cli_redis_addr, m_cli_redis_pass, m_cli_redis_id, m_cli_tun_key)
|
||||
go tunnel.ProcessServer(m_cli_tun_remote_addr,
|
||||
m_cli_redis_addr,
|
||||
m_cli_redis_pass,
|
||||
m_cli_redis_id,
|
||||
md5.Encode(m_cli_tun_key))
|
||||
}
|
||||
|
||||
ch := make(chan os.Signal, 1)
|
||||
|
19
md5/md5.go
Normal file
19
md5/md5.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package md5
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func Encode(data string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(data))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
func EncodeTest() {
|
||||
strTest := "I love this beautiful world!"
|
||||
strEncrypted := "98b4fc4538115c4980a8b859ff3d27e1"
|
||||
fmt.Println(Encode(strTest) == strEncrypted)
|
||||
}
|
Reference in New Issue
Block a user