小花狸监控之加密

小花狸监控
采用对称加密算法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博客 ” ,链接:http://blog.itpub.net/29254281/viewspace-1477252/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29254281/viewspace-1477252/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值