<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 9.6 加密和解密數據 前面小節介紹了如何存儲密碼,但是有的時候,我們想把一些敏感數據加密后存儲起來,在將來的某個時候,隨需將它們解密出來,此時我們應該在選用對稱加密算法來滿足我們的需求。 ## base64加解密 如果Web應用足夠簡單,數據的安全性沒有那么嚴格的要求,那么可以采用一種比較簡單的加解密方法是`base64`,這種方式實現起來比較簡單,Go語言的`base64`包已經很好的支持了這個,請看下面的例子: 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)) } ## 高級加解密 Go語言的`crypto`里面支持對稱加密的高級加解密包有: - `crypto/aes`包:AES(Advanced Encryption Standard),又稱Rijndael加密法,是美國聯邦政府采用的一種區塊加密標準。 - `crypto/des`包:DES(Data Encryption Standard),是一種對稱加密標準,是目前使用最廣泛的密鑰系統,特別是在保護金融數據的安全中。曾是美國聯邦政府的加密標準,但現已被AES所替代。 因為這兩種算法使用方法類似,所以在此,我們僅用aes包為例來講解它們的使用,請看下面的例子 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)) // 創建加密算法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`接口,這個接口實現了三個功能: 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>) * 上一節: [存儲密碼](<09.5.md>) * 下一節: [小結](<09.7.md>)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看