本文實(shí)例講述了go語(yǔ)言base64加密解密的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
package main
import (
"encoding/base64"
"fmt"
)
const (
base64Table = "123QRSTUabcdVWXYZHijKLAWDCABDstEFGuvwxyzGHIJklmnopqr234560178912"
)
var coder = base64.NewEncoding(base64Table)
func base64Encode(src []byte) []byte {
return []byte(coder.EncodeToString(src))
}
func base64Decode(src []byte) ([]byte, error) {
return coder.DecodeString(string(src))
}
func main() {
// encode
hello := "hello world"
debyte := base64Encode([]byte(hello))
// decode
enbyte, err := base64Decode(debyte)
if err != nil {
fmt.Println(err.Error())
}
if hello != string(enbyte) {
fmt.Println("hello is not equal to enbyte")
}
fmt.Println(string(enbyte))
}
希望本文所述對(duì)大家的Go語(yǔ)言程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- go語(yǔ)言實(shí)現(xiàn)字符串base64編碼的方法
- go語(yǔ)言base64用法實(shí)例
- Go語(yǔ)言實(shí)現(xiàn)Base64、Base58編碼與解碼