歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Go語言/Golang實現base64加密解密

Go語言/Golang實現base64加密解密

日期:2017/3/1 9:42:24   编辑:Linux編程

Go語言/Golang實現base64加密解密

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))
}

Ubuntu 安裝Go語言包 http://www.linuxidc.com/Linux/2013-05/85171.htm

《Go語言編程》高清完整版電子書 http://www.linuxidc.com/Linux/2013-05/84709.htm

Go語言並行之美 -- 超越 “Hello World” http://www.linuxidc.com/Linux/2013-05/83697.htm

我為什麼喜歡Go語言 http://www.linuxidc.com/Linux/2013-05/84060.htm

Go語言內存分配器的實現 http://www.linuxidc.com/Linux/2014-01/94766.htm

Go語言的國際化支持(基於gettext-go) http://www.linuxidc.com/Linux/2014-01/94917.htm

Copyright © Linux教程網 All Rights Reserved