當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO NewCFBDecrypter用法及代碼示例

GO語言"crypto/cipher"包中"NewCFBDecrypter"函數的用法及代碼示例。

用法:

func NewCFBDecrypter(block Block, iv []byte) Stream

NewCFBDecrypter 返回一個使用給定塊以密碼反饋模式解密的流。 iv 的長度必須與 Block 的塊大小相同。

例子:

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "encoding/hex"
    "fmt"
)

func main() {
    // Load your secret key from a safe place and reuse it across multiple
    // NewCipher calls. (Obviously don't use this example key for anything
    // real.) If you want to convert a passphrase to a key, use a suitable
    // package like bcrypt or scrypt.
    key, _ := hex.DecodeString("6368616e676520746869732070617373")
    ciphertext, _ := hex.DecodeString("7dd015f06bec7f1b8f6559dad89f4131da62261786845100056b353194ad")

    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    // The IV needs to be unique, but not secure. Therefore it's common to
    // include it at the beginning of the ciphertext.
    if len(ciphertext) < aes.BlockSize {
        panic("ciphertext too short")
    }
    iv := ciphertext[:aes.BlockSize]
    ciphertext = ciphertext[aes.BlockSize:]

    stream := cipher.NewCFBDecrypter(block, iv)

    // XORKeyStream can work in-place if the two arguments are the same.
    stream.XORKeyStream(ciphertext, ciphertext)
    fmt.Printf("%s", ciphertext)
}

輸出:

some plaintext

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 NewCFBDecrypter。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。