小花狸監控之加密
小花狸監控
採用對稱加密演算法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))
}
~
結果:
參考:
採用對稱加密演算法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))
}
~
結果:
參考:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1477252/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 小花狸監控之MySQLMySql
- 小花狸監控之RedisRedis
- 小花狸監控之MongodbMongoDB
- 小花狸監控之網路收發
- 小花狸監控之安全加固expect備份指令碼指令碼
- 小花狸ITPUB部落格備份工具
- MongoDB之監控MongoDB
- 伺服器監控之 ping 監控伺服器
- 11.prometheus監控之黑盒(blackbox)監控Prometheus
- ORACLE監控之OSW部署Oracle
- MySQL監控工具之orzdbaMySql
- redis監控之Redis LiveRedis
- Apache Kafka監控之KafkaOffsetMonitorApacheKafka
- 效能測試之Docker監控Docker
- Zabbix監控之遷移Zabbix
- PostgreSQL之鎖監控指令碼SQL指令碼
- springboot之actuator監控Spring Boot
- 監控mysql之orzdba和orztopMySql
- Session重疊問題學習(七)--小花狸合併演算法和最後一次優化Session演算法優化
- WPA2 加密協議已被破解 用來監控?加密協議
- Kubernetes監控實踐(2):可行監控方案之Prometheus和SensuPrometheus
- Java監控神器之psi-probe監控Tomcat和應用JavaTomcat
- 分散式監控系統之Zabbix主動、被動及web監控分散式Web
- 呼叫鏈監控 CAT 之 入門
- redis監控工具之redis-liveRedis
- Kubernetes監控之InfluxDBUX
- Docker 之 執行狀態監控Docker
- 模擬登入之web監控Web
- 黑盒監控、日誌監控
- 6.prometheus監控--監控dockerPrometheusDocker
- 微服務架構之「 呼叫鏈監控 」微服務架構
- 微服務架構之「 監控系統 」微服務架構
- web前端之異常/錯誤監控Web前端
- 效能監控工具之Grafana+Prometheus+ExportersGrafanaPrometheusExport
- prometheus之docker監控與告警系列(一)PrometheusDocker
- prometheus之docker監控與告警系列(二)PrometheusDocker
- prometheus之docker監控與告警系列(三)PrometheusDocker
- 效能測試之JVM的監控GrafanaJVMGrafana