99re热视频这里只精品,久久久天堂国产精品女人,国产av一区二区三区,久久久精品成人免费看片,99久久精品免费看国产一区二区三区

Go 加密和解密數(shù)據(jù)

2022-05-13 17:39 更新

前面小節(jié)介紹了如何存儲(chǔ)密碼,但是有的時(shí)候,我們想把一些敏感數(shù)據(jù)加密后存儲(chǔ)起來(lái),在將來(lái)的某個(gè)時(shí)候,隨需將它們解密出來(lái),此時(shí)我們應(yīng)該在選用對(duì)稱加密算法來(lái)滿足我們的需求。

base64加解密

如果Web應(yīng)用足夠簡(jiǎn)單,數(shù)據(jù)的安全性沒(méi)有那么嚴(yán)格的要求,那么可以采用一種比較簡(jiǎn)單的加解密方法是base64,這種方式實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單,Go語(yǔ)言的base64包已經(jīng)很好的支持了這個(gè),請(qǐng)看下面的例子:

package main

import (
    "encoding/base64"
    "fmt"
)

func base64Encode(src []byte) []byte {
    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())
    }

    if hello != string(enbyte) {
        fmt.Println("hello is not equal to enbyte")
    }

    fmt.Println(string(enbyte))
}

高級(jí)加解密

Go語(yǔ)言的crypto里面支持對(duì)稱加密的高級(jí)加解密包有:

  • crypto/aes包:AES(Advanced Encryption Standard),又稱Rijndael加密法,是美國(guó)聯(lián)邦政府采用的一種區(qū)塊加密標(biāo)準(zhǔn)。
  • crypto/des包:DES(Data Encryption Standard),是一種對(duì)稱加密標(biāo)準(zhǔn),是目前使用最廣泛的密鑰系統(tǒng),特別是在保護(hù)金融數(shù)據(jù)的安全中。曾是美國(guó)聯(lián)邦政府的加密標(biāo)準(zhǔn),但現(xiàn)已被AES所替代。

因?yàn)檫@兩種算法使用方法類似,所以在此,我們僅用aes包為例來(lái)講解它們的使用,請(qǐng)看下面的例子

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "fmt"
    "os"
)

var commonIV = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}

func main() {
    //需要去加密的字符串
    plaintext := []byte("My name is Astaxie")
    //如果傳入加密串的話,plaint就是傳入的字符串
    if len(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))

    // 創(chuàng)建加密算法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)
}

上面通過(guò)調(diào)用函數(shù)aes.NewCipher(參數(shù)key必須是16、24或者32位的[]byte,分別對(duì)應(yīng)AES-128, AES-192或AES-256算法),返回了一個(gè)cipher.Block接口,這個(gè)接口實(shí)現(xiàn)了三個(gè)功能:

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)
}

這三個(gè)函數(shù)實(shí)現(xiàn)了加解密操作,詳細(xì)的操作請(qǐng)看上面的例子。

總結(jié)

這小節(jié)介紹了幾種加解密的算法,在開(kāi)發(fā)Web應(yīng)用的時(shí)候可以根據(jù)需求采用不同的方式進(jìn)行加解密,一般的應(yīng)用可以采用base64算法,更加高級(jí)的話可以采用aes或者des算法。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)