golang实现base64加密解密

来源:互联网 发布:电磁波检测软件 编辑:程序博客网 时间:2024/05/18 05:24
01package main
02 
03import (
04    "encoding/base64"
05    "fmt"
06)
07 
08const (
09    base64Table ="123QRSTUabcdVWXYZHijKLAWDCABDstEFGuvwxyzGHIJklmnopqr234560178912"
10)
11 
12var coder = base64.NewEncoding(base64Table)
13 
14func base64Encode(src []byte) []byte {
15    return[]byte(coder.EncodeToString(src))
16}
17 
18func base64Decode(src []byte) ([]byte, error) {
19    returncoder.DecodeString(string(src))
20}
21 
22func main() {
23    // encode  
24    hello := "hello world"
25    debyte := base64Encode([]byte(hello))
26 
27    // decode  
28    enbyte, err := base64Decode(debyte)
29    iferr != nil {
30        fmt.Println(err.Error())
31    }
32 
33    ifhello != string(enbyte) {
34        fmt.Println("hello is not equal to enbyte")
35    }
36 
37    fmt.Println(string(enbyte))
38}