Golang AES CFB - 变异IV

Ste*_*ook 3 encryption openssl go

我在Go中编写了一个客户端应用程序,需要与服务器端的C程序进行交互.客户端执行AES CFB加密,服务器解密.不幸的是,服务器端有一个重用初始化向量的错误.它尝试根据以下内容执行3次解密操作: -
key1,iv
key2,iv
key3,iv

由于这个问题,iv实际上在解密操作之间被修改.我现在的问题是如何使用Go在客户端重现此行为.

通过将Println插入下面的加密函数,我可以看到cfb结构,我认为它包含下一个块的修改后的IV,但因为它是一个流接口,我不知道如何将它提取到字节切片中.有什么建议?

谢谢

package main

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

func encrypt_aes_cfb(plain, key, iv []byte) (encrypted []byte) {
  block, err := aes.NewCipher(key)
  if err != nil {
    panic(err)
  }
  encrypted = make([]byte, len(plain))
  stream := cipher.NewCFBEncrypter(block, iv)
  stream.XORKeyStream(encrypted, plain)
  fmt.Println(stream)
  return
}

func main() {
  plain := []byte("Hello world...16Hello world...32")
  key := make([]byte, 32)
  iv := make([]byte, 16)
  enc := encrypt_aes_cfb(plain, key, iv)
  fmt.Println("Key: ", hex.EncodeToString(key))
  fmt.Println("IV:  ", hex.EncodeToString(iv))
  fmt.Println("Enc: ", hex.EncodeToString(enc))
}
Run Code Online (Sandbox Code Playgroud)

Jim*_*imB 6

走下你所暗示的道路有点难看,并且在实施改变时容易中断.

您可以通过以下方式从流中获取IV:

s := reflect.Indirect(reflect.ValueOf(stream))
lastIV := s.FieldByName("next").Bytes()
Run Code Online (Sandbox Code Playgroud)

但是,有一种更简单的方法!连接纯文本输入,以便第二个流从第一个结束时的IV开始(依此类推).

Playground Example

combined := append(plain, plain2...)
encCombined := encrypt_aes_cfb(combined, key, iv)

enc := encCombined[:len(plain)]
enc2 := encCombined[len(plain):]
Run Code Online (Sandbox Code Playgroud)