【问题标题】:Base64 encoded output differs from as3crypto and pycrypto encryption librariesBase64 编码输出不同于 as3crypto 和 pycrypto 加密库
【发布时间】:2011-01-12 19:19:10
【问题描述】:

我正在尝试使用对称加密将数据从 actionscript 3(客户端)传递到 python(服务器)。

我使用的库是 as3crypto 和 pycrypto,我不确定我是否正确使用了这些库。

动作脚本 3:

private function testOnInit():void {
  var t_toEnc:String = 'testtest';
  var t_byAry:ByteArray = Hex.toArray( Hex.fromString( t_toEnc ) );
  var t_key:ByteArray = Hex.toArray( Hex.fromString( 'Thisisthekey' ) );
  var t_cbc:CBCMode = new CBCMode( new BlowFishKey( t_key ), new NullPad );
  var t_enc:String;
  t_cbc.IV = Hex.toArray( '30313233' );
  t_cbc.encrypt( t_byAry );
  t_enc = Base64.encodeByteArray( t_byAry );      
  dbg( 'b64 encrypted string ' + t_enc ); //this is just a debugging function we use in our code.
}

这是上面函数的base64编码输出。

xvVqLzV5TU4=

现在,使用 pycrypto 库中的相同密钥、初始化向量和算法会得到不同的输出。

Python:

from Crypto.Cipher import Blowfish
B = Blowfish.new( 'Thisisthekey', Blowfish.MODE_CBC, '30313233' )
S = 'testtest'
X = B.encrypt( S )
import base64
Y = base64.b64encode( X )
print Y

I82NQEkSHhE=

我很确定我在加密过程中做错了什么,因为我可以在两个库上对“testtest”进行 base64 编码并接收相同的输出。

动作脚本 3:

var b:ByteArray = new ByteArray();
b.writeUTFBytes( 'testtest' );
dbg( Base64.encodeByteArray( b ) );

产量...

dGVzdHRlc3Q=

Python:

>>> T = 'testtest'
>>> print base64.b64encode( T )

产量

dGVzdHRlc3Q=

有人可以在 python 或 actionscript 中使用相同的 IV 加密和 base64encode 相同的字符串,这样我就知道哪个库实际上产生了正确的输出?

【问题讨论】:

  • 当你使用每个库来解密每个加密值时会发生什么?

标签: encryption pycrypto encryption-symmetric blowfish as3crypto


【解决方案1】:

问题可能出在填充中。 ActionScript 不使用填充,但 Py 不显示它使用什么填充。所以,这可能是原因。使用 actionscript 尝试另一个填充(例如 PKCS#5)。

【讨论】:

  • Pycrypto 邮件列表帮我解决了这个问题。 as3 中的初始化向量的解析方式不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 2013-10-27
  • 2011-04-26
  • 2012-02-09
相关资源
最近更新 更多