• 9.6 加密和解密数据
    • base64加解密
    • 高级加解密
    • 总结
    • links

    9.6 加密和解密数据

    前面小节介绍了如何存储密码,但是有的时候,我们想把一些敏感数据加密后存储起来,在将来的某个时候,随需将它们解密出来,此时我们应该在选用对称加密算法来满足我们的需求。

    base64加解密

    如果Web应用足够简单,数据的安全性没有那么严格的要求,那么可以采用一种比较简单的加解密方法是base64,这种方式实现起来比较简单,Go语言的base64包已经很好的支持了这个,请看下面的例子:

    1. package main
    2. import (
    3. "encoding/base64"
    4. "fmt"
    5. )
    6. func base64Encode(src []byte) []byte {
    7. return []byte(base64.StdEncoding.EncodeToString(src))
    8. }
    9. func base64Decode(src []byte) ([]byte, error) {
    10. return base64.StdEncoding.DecodeString(string(src))
    11. }
    12. func main() {
    13. // encode
    14. hello := "你好,世界! hello world"
    15. debyte := base64Encode([]byte(hello))
    16. fmt.Println(debyte)
    17. // decode
    18. enbyte, err := base64Decode(debyte)
    19. if err != nil {
    20. fmt.Println(err.Error())
    21. }
    22. if hello != string(enbyte) {
    23. fmt.Println("hello is not equal to enbyte")
    24. }
    25. fmt.Println(string(enbyte))
    26. }

    高级加解密

    Go语言的crypto里面支持对称加密的高级加解密包有:

    • crypto/aes包:AES(Advanced Encryption Standard),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。
    • crypto/des包:DES(Data Encryption Standard),是一种对称加密标准,是目前使用最广泛的密钥系统,特别是在保护金融数据的安全中。曾是美国联邦政府的加密标准,但现已被AES所替代。

    因为这两种算法使用方法类似,所以在此,我们仅用aes包为例来讲解它们的使用,请看下面的例子

    1. package main
    2. import (
    3. "crypto/aes"
    4. "crypto/cipher"
    5. "fmt"
    6. "os"
    7. )
    8. var commonIV = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
    9. func main() {
    10. //需要去加密的字符串
    11. plaintext := []byte("My name is Astaxie")
    12. //如果传入加密串的话,plaint就是传入的字符串
    13. if len(os.Args) > 1 {
    14. plaintext = []byte(os.Args[1])
    15. }
    16. //aes的加密字符串
    17. key_text := "astaxie12798akljzmknm.ahkjkljl;k"
    18. if len(os.Args) > 2 {
    19. key_text = os.Args[2]
    20. }
    21. fmt.Println(len(key_text))
    22. // 创建加密算法aes
    23. c, err := aes.NewCipher([]byte(key_text))
    24. if err != nil {
    25. fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
    26. os.Exit(-1)
    27. }
    28. //加密字符串
    29. cfb := cipher.NewCFBEncrypter(c, commonIV)
    30. ciphertext := make([]byte, len(plaintext))
    31. cfb.XORKeyStream(ciphertext, plaintext)
    32. fmt.Printf("%s=>%x\n", plaintext, ciphertext)
    33. // 解密字符串
    34. cfbdec := cipher.NewCFBDecrypter(c, commonIV)
    35. plaintextCopy := make([]byte, len(plaintext))
    36. cfbdec.XORKeyStream(plaintextCopy, ciphertext)
    37. fmt.Printf("%x=>%s\n", ciphertext, plaintextCopy)
    38. }

    上面通过调用函数aes.NewCipher(参数key必须是16、24或者32位的[]byte,分别对应AES-128, AES-192或AES-256算法),返回了一个cipher.Block接口,这个接口实现了三个功能:

    1. type Block interface {
    2. // BlockSize returns the cipher's block size.
    3. BlockSize() int
    4. // Encrypt encrypts the first block in src into dst.
    5. // Dst and src may point at the same memory.
    6. Encrypt(dst, src []byte)
    7. // Decrypt decrypts the first block in src into dst.
    8. // Dst and src may point at the same memory.
    9. Decrypt(dst, src []byte)
    10. }

    这三个函数实现了加解密操作,详细的操作请看上面的例子。

    总结

    这小节介绍了几种加解密的算法,在开发Web应用的时候可以根据需求采用不同的方式进行加解密,一般的应用可以采用base64算法,更加高级的话可以采用aes或者des算法。

    • 目录
    • 上一节: 存储密码
    • 下一节: 小结