doujiu8178 2019-05-15 00:55
浏览 425

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?

  • 写回答

1条回答 默认 最新

  • douwo1517 2019-05-15 02:17
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作