package main;
import (
"encoding/base64"
"fmt"
"strings"
)
func baseStdEncode(srcBtye []byte) string {
encoding := base64.StdEncoding.EncodeToString(srcBtye)
return encoding
}
func baseDeEncode(src string) string {
reader := strings.NewReader(src)
decoder := base64.NewDecoder(base64.StdEncoding, reader)
// 以流式解碼
buf := make([]byte, 2)
// 保存解碼後的數據
dst := ""
for {
n, err := decoder.Read(buf)
if n == 0 || err != nil {
break
}
dst += string(buf[:n])
}
fmt.Println("解碼後的數據爲:", dst)
return dst
}
func main(){
code:="445"
encodeCode:=baseStdEncode([]byte(code))
println(code+" base64加密後的數據爲: "+encodeCode)
println(encodeCode+" base64解密後的數據爲: "+baseDeEncode(encodeCode))ide
}加密