使用AES-CFB时,Go和Pycrypto的不同结果 [英] Different Results in Go and Pycrypto when using AES-CFB

查看:355
本文介绍了使用AES-CFB时,Go和Pycrypto的不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个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

密码= unhexlify(0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF)
iv = unhexlify(00000000000000000000000000000000

#encrypt
payload = unhexlify(ababababababababababababababababababababababababababababababababab )

打印IV:,hexlify(iv),len:,len(iv)
打印密码长度:,len(密码)


cipher = Crypto.Cipher.AES.new(
key = password,
mode = Crypto.Cipher.AES.MODE_CFB,
IV = iv)

payload = cipher.encrypt(payload)

print hexlify(payload)#dbf6b1877ba903330cb9cf0c4f530d40bf77fe2bf505820e993741c7f698ad6b

这是Go示例:

 包主

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


//加密
func main(){
payload,err1:= hex.DecodeString(abababababababababababababababababababababababababababababababababababababababababababababababababab)
password,err2:= hex.DecodeString(0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF)
iv,err3:= hex.DecodeString(00000000000000000000000000000000)

如果err1!= nil {
fmt.Printf(error 1:%v,err1)
return
}

如果err2!= nil {
fmt.Printf(error 2:%v,err2)
return
}

如果err3!= nil {
fmt.Printf(error 3:%v,err3)
return
}

aesBlock,err4:= aes.NewCipher(password)

fmt.Printf(IV length:%v\\\
,len(iv))
fmt.Printf(password length:%v\\\
,len(p ass $)

如果err4!= nil {
fmt.Printf(error 4:%v,err4)
return
}
$

$ b fmt.Printf(%v\\\
,hex.EncodeToString(有效载荷))// db70cd9e6904359cb848410bfa38d7d0a47b594f7eff72d547d3772c9d4f5dbe
}

这里是 golang链接,我找不到安装了PyCrypto的Python pastebin。



如标题&来源,这两个片段产生不同的cyphertext:

Python:dbf6b1877ba903330cb9cf0c4f530d40bf77fe2bf505820e993741c7f698ad6b

Golang:db70cd9e6904359cb848410bfa38d7d0a47b594f7eff72d547d3772c9d4f5dbe



两种语言都可以解密他们的本机'cypthertext,但也不能解密别人'。因为python实现已经存在,我正在寻找一个解决方案,将允许Go解密cyphertext加密与示例PyCrypto AES设置&关键大小。

解决方案

对当前系统的研究表明,我们的python系统使用CFB8(8位段)。 Go不支持这个开箱即用,但目前CFBDecrypter / CFBEncrypter中使用的源代码看起来可以很容易地进行调整。


I am adding a go application to an already existing python codebase. I've been having trouble dealing with encryption between the languages. This is using go 1.2.1 and Python 2.7.x / PyCrypto 2.7a1.

Here is the Python sample:

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

And this is the Go sample:

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
}

Here is the golang link, I could not find a Python pastebin that had PyCrypto installed.

As suggested by the title & source, the two snippets produce different cyphertext:
Python: dbf6b1877ba903330cb9cf0c4f530d40bf77fe2bf505820e993741c7f698ad6b
Golang: db70cd9e6904359cb848410bfa38d7d0a47b594f7eff72d547d3772c9d4f5dbe

Both languages can decrypt their 'native' cypthertext, but neither can decrypt the others'. Because the python implementation already exists, I'm looking for a solution that will allow Go to decrypt cyphertext encrypted with the example PyCrypto AES settings & key size.

解决方案

Research on the current system has revealed that our python system uses CFB8 (8 bit segments). Go does not support this out of the box, but the source code used in the current CFBDecrypter / CFBEncrypter looks like it can be adapted fairly easily.

这篇关于使用AES-CFB时,Go和Pycrypto的不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆