我正在将go应用程序添加到已经存在的python代码库中。我在处理语言之间的加密时遇到了麻烦。这使用的是go 1.2.1和Python 2.7.x / PyCrypto 2.7a1。
这是Python示例:
import Crypto.Cipher import Crypto.Hash.HMAC import Crypto.Hash.SHA256 import Crypto.PublicKey.RSA from binascii import hexlify, unhexlify #encrypt payload = unhexlify("abababababababababababababababababababababababababababababababab") password = unhexlify("0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF") iv = unhexlify("00000000000000000000000000000000") print "IV: ", hexlify(iv), "len: ", len(iv) print "Password length: ", len(password) cipher = Crypto.Cipher.AES.new( key=password, mode=Crypto.Cipher.AES.MODE_CFB, IV=iv) payload = cipher.encrypt(payload) print hexlify(payload) #dbf6b1877ba903330cb9cf0c4f530d40bf77fe2bf505820e993741c7f698ad6b
这是Go示例:
package main import ( "fmt" "crypto/cipher" "crypto/aes" "encoding/hex" ) // encrypt func main() { payload, err1 := hex.DecodeString("abababababababababababababababababababababababababababababababab") password, err2 := hex.DecodeString("0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF") iv, err3 := hex.DecodeString("00000000000000000000000000000000") if err1 != nil { fmt.Printf("error 1: %v", err1) return } if err2 != nil { fmt.Printf("error 2: %v", err2) return } if err3 != nil { fmt.Printf("error 3: %v", err3) return } aesBlock, err4 := aes.NewCipher(password) fmt.Printf("IV length:%v\n", len(iv)) fmt.Printf("password length:%v\n", len(password)) if err4 != nil { fmt.Printf("error 4: %v", err4) return } cfbDecrypter := cipher.NewCFBEncrypter(aesBlock, iv) cfbDecrypter.XORKeyStream(payload, payload) fmt.Printf("%v\n", hex.EncodeToString(payload)) // db70cd9e6904359cb848410bfa38d7d0a47b594f7eff72d547d3772c9d4f5dbe }
这是golang链接,我找不到安装PyCrypto的Python pastebin。
正如标题和来源所建议的那样,两个代码段会产生不同的密文: Python:dbf6b1877ba903330cb9cf0c4f530d40bf77fe2bf505820e993741c7f698ad6b Golang:db70cd9e6904359cb848410bfa38d7d0a47b594f7eff72d547d377
两种语言都可以解密它们的“本机”密码文本,但是两种语言都不能解密。因为python实现已经存在,所以我正在寻找一种解决方案,该解决方案允许Go解密使用示例PyCrypto AES设置和密钥大小加密的密文。
对当前系统的研究表明,我们的python系统使用CFB8(8位段)。Go不支持此功能,但是当前CFBDecrypter / CFBEncrypter中使用的源代码看起来很容易适应。