removed all the footers; formated all the files
This commit is contained in:
37
9.6.md
37
9.6.md
@@ -1,4 +1,4 @@
|
||||
# 9.6 加密和解密数据
|
||||
# 9.6 加密和解密数据
|
||||
前面小节介绍了如何存储密码,但是有的时候,我们想把一些敏感数据加密后存储起来,在将来的某个时候,随需将它们解密出来,此时我们应该在选用对称加密算法来满足我们的需求。
|
||||
|
||||
## base64加解密
|
||||
@@ -20,11 +20,11 @@
|
||||
}
|
||||
|
||||
func main() {
|
||||
// encode
|
||||
// encode
|
||||
hello := "你好,世界! hello world"
|
||||
debyte := base64Encode([]byte(hello))
|
||||
fmt.Println(debyte)
|
||||
// decode
|
||||
// decode
|
||||
enbyte, err := base64Decode(debyte)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
@@ -98,16 +98,16 @@ Go语言的`crypto`里面支持对称加密的高级加解密包有:
|
||||
上面通过调用函数`aes.NewCipher`(参数key必须是16、24或者32位的[]byte,分别对应AES-128, AES-192或AES-256算法),返回了一个`cipher.Block`接口,这个接口实现了三个功能:
|
||||
|
||||
type Block interface {
|
||||
// BlockSize returns the cipher's block size.
|
||||
BlockSize() int
|
||||
|
||||
// Encrypt encrypts the first block in src into dst.
|
||||
// Dst and src may point at the same memory.
|
||||
Encrypt(dst, src []byte)
|
||||
|
||||
// Decrypt decrypts the first block in src into dst.
|
||||
// Dst and src may point at the same memory.
|
||||
Decrypt(dst, src []byte)
|
||||
// BlockSize returns the cipher's block size.
|
||||
BlockSize() int
|
||||
|
||||
// Encrypt encrypts the first block in src into dst.
|
||||
// Dst and src may point at the same memory.
|
||||
Encrypt(dst, src []byte)
|
||||
|
||||
// Decrypt decrypts the first block in src into dst.
|
||||
// Dst and src may point at the same memory.
|
||||
Decrypt(dst, src []byte)
|
||||
}
|
||||
|
||||
这三个函数实现了加解密操作,详细的操作请看上面的例子。
|
||||
@@ -116,10 +116,7 @@ Go语言的`crypto`里面支持对称加密的高级加解密包有:
|
||||
这小节介绍了几种加解密的算法,在开发Web应用的时候可以根据需求采用不同的方式进行加解密,一般的应用可以采用base64算法,更加高级的话可以采用aes或者des算法。
|
||||
|
||||
|
||||
## links
|
||||
* [目录](<preface.md>)
|
||||
* 上一节: [存储密码](<9.5.md>)
|
||||
* 下一节: [小结](<9.7.md>)
|
||||
|
||||
## LastModified
|
||||
* $Id$
|
||||
## links
|
||||
* [目录](<preface.md>)
|
||||
* 上一节: [存储密码](<9.5.md>)
|
||||
* 下一节: [小结](<9.7.md>)
|
||||
|
||||
Reference in New Issue
Block a user