Format and remove 09.6.md spaces
This commit is contained in:
156
zh/09.6.md
156
zh/09.6.md
@@ -6,38 +6,38 @@
|
|||||||
|
|
||||||
```Go
|
```Go
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func base64Encode(src []byte) []byte {
|
func base64Encode(src []byte) []byte {
|
||||||
return []byte(base64.StdEncoding.EncodeToString(src))
|
return []byte(base64.StdEncoding.EncodeToString(src))
|
||||||
|
}
|
||||||
|
|
||||||
|
func base64Decode(src []byte) ([]byte, error) {
|
||||||
|
return base64.StdEncoding.DecodeString(string(src))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// encode
|
||||||
|
hello := "你好,世界! hello world"
|
||||||
|
debyte := base64Encode([]byte(hello))
|
||||||
|
fmt.Println(debyte)
|
||||||
|
// decode
|
||||||
|
enbyte, err := base64Decode(debyte)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func base64Decode(src []byte) ([]byte, error) {
|
if hello != string(enbyte) {
|
||||||
return base64.StdEncoding.DecodeString(string(src))
|
fmt.Println("hello is not equal to enbyte")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
fmt.Println(string(enbyte))
|
||||||
// encode
|
}
|
||||||
hello := "你好,世界! hello world"
|
|
||||||
debyte := base64Encode([]byte(hello))
|
|
||||||
fmt.Println(debyte)
|
|
||||||
// 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))
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
```
|
||||||
## 高级加解密
|
## 高级加解密
|
||||||
@@ -50,70 +50,70 @@ Go语言的`crypto`里面支持对称加密的高级加解密包有:
|
|||||||
因为这两种算法使用方法类似,所以在此,我们仅用aes包为例来讲解它们的使用,请看下面的例子
|
因为这两种算法使用方法类似,所以在此,我们仅用aes包为例来讲解它们的使用,请看下面的例子
|
||||||
```Go
|
```Go
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var commonIV = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
|
var commonIV = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//需要去加密的字符串
|
//需要去加密的字符串
|
||||||
plaintext := []byte("My name is Astaxie")
|
plaintext := []byte("My name is Astaxie")
|
||||||
//如果传入加密串的话,plaint就是传入的字符串
|
//如果传入加密串的话,plaint就是传入的字符串
|
||||||
if len(os.Args) > 1 {
|
if len(os.Args) > 1 {
|
||||||
plaintext = []byte(os.Args[1])
|
plaintext = []byte(os.Args[1])
|
||||||
}
|
|
||||||
|
|
||||||
//aes的加密字符串
|
|
||||||
key_text := "astaxie12798akljzmknm.ahkjkljl;k"
|
|
||||||
if len(os.Args) > 2 {
|
|
||||||
key_text = os.Args[2]
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(len(key_text))
|
|
||||||
|
|
||||||
// 创建加密算法aes
|
|
||||||
c, err := aes.NewCipher([]byte(key_text))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
//加密字符串
|
|
||||||
cfb := cipher.NewCFBEncrypter(c, commonIV)
|
|
||||||
ciphertext := make([]byte, len(plaintext))
|
|
||||||
cfb.XORKeyStream(ciphertext, plaintext)
|
|
||||||
fmt.Printf("%s=>%x\n", plaintext, ciphertext)
|
|
||||||
|
|
||||||
// 解密字符串
|
|
||||||
cfbdec := cipher.NewCFBDecrypter(c, commonIV)
|
|
||||||
plaintextCopy := make([]byte, len(plaintext))
|
|
||||||
cfbdec.XORKeyStream(plaintextCopy, ciphertext)
|
|
||||||
fmt.Printf("%x=>%s\n", ciphertext, plaintextCopy)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//aes的加密字符串
|
||||||
|
key_text := "astaxie12798akljzmknm.ahkjkljl;k"
|
||||||
|
if len(os.Args) > 2 {
|
||||||
|
key_text = os.Args[2]
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(len(key_text))
|
||||||
|
|
||||||
|
// 创建加密算法aes
|
||||||
|
c, err := aes.NewCipher([]byte(key_text))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
//加密字符串
|
||||||
|
cfb := cipher.NewCFBEncrypter(c, commonIV)
|
||||||
|
ciphertext := make([]byte, len(plaintext))
|
||||||
|
cfb.XORKeyStream(ciphertext, plaintext)
|
||||||
|
fmt.Printf("%s=>%x\n", plaintext, ciphertext)
|
||||||
|
|
||||||
|
// 解密字符串
|
||||||
|
cfbdec := cipher.NewCFBDecrypter(c, commonIV)
|
||||||
|
plaintextCopy := make([]byte, len(plaintext))
|
||||||
|
cfbdec.XORKeyStream(plaintextCopy, ciphertext)
|
||||||
|
fmt.Printf("%x=>%s\n", ciphertext, plaintextCopy)
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
上面通过调用函数`aes.NewCipher`(参数key必须是16、24或者32位的[]byte,分别对应AES-128, AES-192或AES-256算法),返回了一个`cipher.Block`接口,这个接口实现了三个功能:
|
上面通过调用函数`aes.NewCipher`(参数key必须是16、24或者32位的[]byte,分别对应AES-128, AES-192或AES-256算法),返回了一个`cipher.Block`接口,这个接口实现了三个功能:
|
||||||
|
|
||||||
```Go
|
```Go
|
||||||
|
|
||||||
type Block interface {
|
type Block interface {
|
||||||
// BlockSize returns the cipher's block size.
|
// BlockSize returns the cipher's block size.
|
||||||
BlockSize() int
|
BlockSize() int
|
||||||
|
|
||||||
// Encrypt encrypts the first block in src into dst.
|
// Encrypt encrypts the first block in src into dst.
|
||||||
// Dst and src may point at the same memory.
|
// Dst and src may point at the same memory.
|
||||||
Encrypt(dst, src []byte)
|
Encrypt(dst, src []byte)
|
||||||
|
|
||||||
// Decrypt decrypts the first block in src into dst.
|
// Decrypt decrypts the first block in src into dst.
|
||||||
// Dst and src may point at the same memory.
|
// Dst and src may point at the same memory.
|
||||||
Decrypt(dst, src []byte)
|
Decrypt(dst, src []byte)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
这三个函数实现了加解密操作,详细的操作请看上面的例子。
|
这三个函数实现了加解密操作,详细的操作请看上面的例子。
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user