crypto / cipher-XORKeyStream对src [] byte有什么作用?

I'm doing AES encryption using Go, I found that the source bytes changed after encryption. Seems that XORKeyStream function does the change if cap(source) > len(source), what it exactly does to the src []byte?

go version go1.12.5 darwin/amd64

func main() {
    byte1 := []byte("123abc")
    fmt.Println("content1:", byte1, "len1:", len(byte1), "cap1:", cap(byte1)) // content1: [49 50 51 97 98 99] len1: 6 cap1: 6
    buf := bytes.NewBuffer([]byte("123abc"))
    byte2, _ := ioutil.ReadAll(buf)
    fmt.Println("content2:", byte2, "len2:", len(byte2), "cap2:", cap(byte2)) // content2: [49 50 51 97 98 99] len2: 6 cap2: 1536

    _, _, _, err := crypt.AESEnc(byte1)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("content1:", byte1, "len1:", len(byte1), "cap1:", cap(byte1)) // content1: [49 50 51 97 98 99] len1: 6 cap1: 6
    _, _, _, err = crypt.AESEnc(byte2)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("content2:", byte2, "len2:", len(byte2), "cap2:", cap(byte2)) // content2: [132 200 7 200 195 8] len2: 6 cap2: 1536
}
func AESEnc(data []byte) ([]byte, []byte, string, error) {
    key := make([]byte, 16)
    iv := make([]byte, 16)
    _, err := io.ReadFull(rand.Reader, key)
    if err != nil {
        return nil, nil, "", err
    }
    _, err = io.ReadFull(rand.Reader, iv)
    if err != nil {
        return nil, nil, "", err
    }
    block, err := aes.NewCipher(key)
    if err != nil {
        return nil, nil, "", err
    }
    pdata := pckspadding(data, block.BlockSize())
    stream := cipher.NewCFBEncrypter(block, iv)
    stream.XORKeyStream(pdata, pdata)
    return key, iv, base64.StdEncoding.EncodeToString(pdata), nil
}

func pckspadding(ciphertext []byte, blockSize int) []byte {
    padding := blockSize - len(ciphertext)%blockSize
    padtext := bytes.Repeat([]byte{byte(padding)}, padding)
    return append(ciphertext, padtext...)
}

byte2 changes after encryption, what happened?

aes

1个回答



我不熟悉crypto / cypher-XORKeyStream,但我可以告诉您XOR对位的作用,如果有帮助的话。 我有一些电子经验,这是XOR门的真相表:</ p>

“ </ p>

输入X和Y代表两位。 输出Z是对X和Y进行XOR运算的结果。</ p>

在英语中,您会对自己说:“输入,一个或另一个,但不能同时输入两个”,结果输出为“ 真正”。 </ p>

不知道这将有多少帮助,或者不知道如何使用crypto / cypher-XORKeyStream将其应用于两个以上的输入位。
但这是一个示例:</ p>

  X = 00110001010 
Y = 11111111111
Z = 11001110101
</ code> </ pre>

祝你好运!</ p>
< / DIV>

展开原文

原文

I'm not familiar a crypto/cypher-XORKeyStream but I can tell you what XOR does to bits if that is helpfull. I have some Electronic experience and here is the truth table to an XOR gate:

XOR Gate Truth Table

Inputs X and Y represent two bits. The output Z is the result of XOR-ing X and Y.

In English you would say to yourself "Inputs, either one or the other but not both" results in an output of "True".

Don't know how much help this will be or how to apply it to more than two input bits with a crypto/cypher-XORKeyStream. But here would be an example:

X = 00110001010
Y = 11111111111
Z = 11001110101

Good Luck!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐