修改了base64的代码

This commit is contained in:
astaxie
2012-10-30 14:20:50 +08:00
parent 20f5ddcf1d
commit 64d34185fa

15
9.6.md
View File

@@ -11,23 +11,17 @@
"fmt" "fmt"
) )
const (
base64Table = "123QRSTUabcdVWXYZHijKLAWDCABDstEFGuvwxyzGHIJklmnopqr234560178912"
)
var coder = base64.NewEncoding(base64Table)
func base64Encode(src []byte) []byte { func base64Encode(src []byte) []byte {
return []byte(coder.EncodeToString(src)) return []byte(base64.StdEncoding.EncodeToString(src))
} }
func base64Decode(src []byte) ([]byte, error) { func base64Decode(src []byte) ([]byte, error) {
return coder.DecodeString(string(src)) return base64.StdEncoding.DecodeString(string(src))
} }
func main() { func main() {
// encode // encode
hello := "hello world" hello := "你好,世界! hello world"
debyte := base64Encode([]byte(hello)) debyte := base64Encode([]byte(hello))
fmt.Println(debyte) fmt.Println(debyte)
// decode // decode
@@ -43,6 +37,7 @@
fmt.Println(string(enbyte)) fmt.Println(string(enbyte))
} }
## 高级加解密 ## 高级加解密
Go语言的`crypto`里面支持对称加密的高级加解密包有: Go语言的`crypto`里面支持对称加密的高级加解密包有:
@@ -92,7 +87,7 @@ Go语言的`crypto`里面支持对称加密的高级加解密包有:
cfb.XORKeyStream(ciphertext, plaintext) cfb.XORKeyStream(ciphertext, plaintext)
fmt.Printf("%s=>%x\n", plaintext, ciphertext) fmt.Printf("%s=>%x\n", plaintext, ciphertext)
// 密字符串 // 密字符串
cfbdec := cipher.NewCFBDecrypter(c, commonIV) cfbdec := cipher.NewCFBDecrypter(c, commonIV)
plaintextCopy := make([]byte, len(plaintext)) plaintextCopy := make([]byte, len(plaintext))
cfbdec.XORKeyStream(plaintextCopy, ciphertext) cfbdec.XORKeyStream(plaintextCopy, ciphertext)