开发者

Base64 encoded output differs from as3crypto and pycrypto encryption libraries

I'm trying to use symmetric encryption to pass data from actionscript 3 (client) to python (server).

The libraries I'm using are as3crypto and pycrypto, I'm not sure if I'm using these libraries correctly.

Actionscript 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.
}

This is the base64 encoded output of the function above.

xvVqLzV5TU4=

Now, using the same key, initialization vector, and algorithm from the pycrypto library gives me different output.

Python:

from Crypto.Cipher import Blowf开发者_如何学编程ish
B = Blowfish.new( 'Thisisthekey', Blowfish.MODE_CBC, '30313233' )
S = 'testtest'
X = B.encrypt( S )
import base64
Y = base64.b64encode( X )
print Y

I82NQEkSHhE=

I'm pretty sure that I'm doing something wrong with the encryption process because I can base64 encode 'testtest' on both libraries and receive the same output.

Actionscript 3:

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

Yields...

dGVzdHRlc3Q=

Python:

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

Yields

dGVzdHRlc3Q=

Could someone please encrypt and base64encode the same string with the same IV in either python or actionscript, so I know which library is actually producing the correct output?


The problem might be in padding. ActionScript uses no padding, but Py doesn't show what padding it uses. So, it can be the reason. Try another padding (PKCS#5 for instance) with actionscript.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜