我正在使用Go开发一个客户端应用程序,依赖于AES CFB算法。服务器端使用C语言编写。我的问题是,Go的AES CFB实现似乎与许多其他实现(包括OpenSSL)不同。我编写了以下代码来测试我的理论:
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)
return
}
func decrypt_aes_cfb(encrypted, key, iv []byte) (plain []byte) {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
plain = make([]byte, len(encrypted))
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(plain, encrypted)
return
}
func main() {
plain := []byte("Hello world.....")
key := []byte("01234567890123456789012345678901")
iv := []byte("0123456789012345")
enc := encrypt_aes_cfb(plain, key, iv)
dec := decrypt_aes_cfb(enc, key, iv)
fmt.Println("Key: ", hex.EncodeToString(key))
fmt.Println("IV: ", hex.EncodeToString(iv))
fmt.Println("Enc: ", hex.EncodeToString(enc))
fmt.Println("In: ", hex.EncodeToString(plain))
fmt.Println("Out: ", hex.EncodeToString(dec))
}
当这段代码被运行时,看起来完美无缺,但是如果将加密后的字节复制到另一个AES实现中,并使用相同的密钥和IV进行解密,则明文会受损(除了第一字节)。 http://aes.online-domain-tools.com/ 提供了一种简单的测试方法。您有什么建议原因以及如何解决此问题吗?
谢谢 Steve