完成了加解密这一章
This commit is contained in:
109
9.6.md
109
9.6.md
@@ -1,11 +1,116 @@
|
||||
##9.6 加密和解密数据
|
||||
前面小节我们介绍了如何存储密码,但是我们开发Web应用过程中遇到过这样的情况,我们想把一些敏感数据进行存储,那存储的时候希望能够是加密方式存储的,但是这些加密的数据我们需要最后解密出来,那么就需要采用一种特定的算法来进行加解密数据。
|
||||
|
||||
##加密数据
|
||||
##base64加解密
|
||||
如果Web应用足够简单,数据的安全性没有那么严格的要求,那么可以采用一种比较简单的加解密方法是`base64`,这种方式实现起来比较简单,Go语言的`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))
|
||||
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))
|
||||
}
|
||||
|
||||
##高级加解密
|
||||
|
||||
Go语言的`crypto`里面支持双向加密的高级加解密包有:
|
||||
|
||||
- `crypto/aes`包:AES(Advanced Encryption Standard),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。
|
||||
- `crypto/des`包:DEA(Data Encryption Algorithm),是一种对称加密算法,是目前使用最广泛的密钥系统,特别是在保护金融数据的安全中。
|
||||
|
||||
下面以aes包作为例子来讲解这两种高级加解密算法的应用,这两种方式使用方法类似,请看下面的例子
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
. "fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
msg := "My name is Astaxie"
|
||||
// some key, 16 Byte long
|
||||
key := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
|
||||
|
||||
Println("len of message: ", len(msg))
|
||||
Println("len of key: ", len(key))
|
||||
// create the new cipher
|
||||
c, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
Println("Error: NewCipher(%d bytes) = %s", len(key), err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
out := make([]byte, len(msg))
|
||||
|
||||
c.Encrypt(out, []byte(msg)) // encrypt the first half
|
||||
//c.Encrypt(msgbuf[16:32], out[16:32]) // encrypt the second half
|
||||
|
||||
Println("len of encrypted: ", len(out))
|
||||
Println(">> ", out)
|
||||
|
||||
// // now we decrypt our encrypted text
|
||||
plain := make([]byte, len(out))
|
||||
c.Decrypt(plain, out)
|
||||
|
||||
Println("msg: ", string(plain))
|
||||
}
|
||||
|
||||
上面通过调用函数`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)
|
||||
}
|
||||
|
||||
这三个函数实现了加解密操作,详细的操作请看上面的例子。
|
||||
|
||||
##总结
|
||||
这小节介绍了几种加解密的算法,在开发Web应用的时候可以根据需求采用不同的方式进行加解密,一般的应用可以采用base64算法,更加高级的话可以采用aes或者des算法。
|
||||
|
||||
|
||||
## links
|
||||
* [目录](<preface.md>)
|
||||
|
||||
3
9.7.md
3
9.7.md
@@ -1,4 +1,7 @@
|
||||
##9.7 小结
|
||||
这一章介绍了一些Web应用中典型的攻击:CSRF攻击、XSS攻击、SQL注入攻击,这些攻击都是由于我们对于用户的输入没有很好的过滤引起的,所以我们专门介绍了如何有效的进行数据过滤,以防止这些攻击的发生。然后针对目前出现的密码泄露事件,介绍了在设计Web应用的时候我们应该如何存储我们的密码,里面介绍了几种不同的存储方式。最后针对敏感数据我们介绍了如何进行加密存储,最后这些数据又可以解密,能够进行双向的加解密操作,Go语言提供了三种算法:base64、aes和des。
|
||||
|
||||
通过这一章的介绍主要的目的是希望读者在编写Web应用的时候多留心一点,能够在你的意识里面加强安全概念,不要让我们编写的Web应用轻松的成为黑客们攻击的目标。而且Go语言在支持这些防攻击方面已经做了大量的工具包,我们可以充分的利用这些包来做出一个安全的Web应用。
|
||||
|
||||
## links
|
||||
* [目录](<preface.md>)
|
||||
|
||||
Reference in New Issue
Block a user