小花狸监控
采用对称加密算法AES
明文首先经过Base64编码,然后用空格填充到指定位数,使用AES加密,最后将加密的数据再用Base64编码
$GOPATH/probe/module/AES.go
package module
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"strings"
"sync"
)
const (
//aesTable是密钥
aesTable = "abcdefghijklmnopkrstuvwsyz333333"
base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
)
var (
block cipher.Block
mutex sync.Mutex
base64Coder = base64.NewEncoding(base64Table)
)
func EncryptString(src string) string {
datastr := base64Coder.EncodeToString([]byte(src))
for len(datastr)%aes.BlockSize != 0 {
datastr = datastr + " "
}
data, _ := Encrypt([]byte(datastr))
result := base64Coder.EncodeToString(data)
return result
}
// AES加密
func Encrypt(src []byte) ([]byte, error) {
// 验证输入参数
// 必须为aes.Blocksize的倍数
if len(src)%aes.BlockSize != 0 {
return nil, errors.New("crypto/cipher: input not full blocks")
}
encryptText := make([]byte, aes.BlockSize+len(src))
iv := encryptText[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encryptText[aes.BlockSize:], src)
return encryptText, nil
}
func DecryptString(src string) string {
var result []byte
data, _ := base64Coder.DecodeString(src)
data, _ = Decrypt(data)
datastr := strings.Trim(string(data), " ")
result, _ = base64Coder.DecodeString(datastr)
return string(result)
}
// AES解密
func Decrypt(src []byte) ([]byte, error) {
// hex
decryptText, err := hex.DecodeString(fmt.Sprintf("%x", string(src)))
if err != nil {
return nil, err
}
// 长度不能小于aes.Blocksize
if len(decryptText) < aes.BlockSize {
return nil, errors.New("crypto/cipher: ciphertext too short")
}
iv := decryptText[:aes.BlockSize]
decryptText = decryptText[aes.BlockSize:]
// 验证输入参数
// 必须为aes.Blocksize的倍数
if len(decryptText)%aes.BlockSize != 0 {
return nil, errors.New("crypto/cipher: ciphertext is not a multiple of the block size")
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(decryptText, decryptText)
return decryptText, nil
}
func init() {
mutex.Lock()
defer mutex.Unlock()
if block != nil {
return
}
cblock, err := aes.NewCipher([]byte(aesTable))
if err != nil {
panic("aes.NewCipher: " + err.Error())
}
block = cblock
}
调用程序
$GOPATH/main.go
package main
import (
"fmt"
"probe/module"
)
func main() {
data := "zbcdfe"
fmt.Println("明文:", data)
s := module.EncryptString(data)
fmt.Println("密文:", s)
fmt.Println("解密:", module.DecryptString(s))
}
~
结果:

参考:
http://www.oschina.net/code/snippet_197499_25891
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1477252/
,如需转载,请注明出处,否则将追究法律责任。