我有一个django应用程序,它为Flash应用程序提供加密的媒体文件。python中的加密是用PyCrypto完成的,如下所示(我还包括说明,以防有用):
def encrypt_aes(key, text):
try:
raw = _pad(text)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(raw))
except Exception as e:
print e.message
return text
def decrypt_aes(key, text):
try:
enc = base64.b64decode(text)
iv = enc[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
return _unpad(cipher.decrypt(enc[AES.block_size:]))
except Exception as e:
print e.message
return text
def _pad(s):
bs = 16
return s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
def _unpad(s):
return s[:-ord(s[len(s) - 1:])]我还不能解密Python提供的媒体文件(使用‘LoaderMax’通过GreenSock下载,使用‘DataLoader’下载)。我的AS3代码(使用AS3Crypto)如下:
public static function decipher(_key:String):void{
key=_key;
cipher = Crypto.getCipher("simple-aes-cbc", Hex.toArray(key));
cipher.decrypt(ByteArray(_content));
}我得到了
RangeError:错误#2006
一种怀疑是,在Python中,我有64位基,但我认为AS3 ByteArray是32位基。我已经尝试了下面的方法,但是得到了同样的错误。
cipher.decrypt(ByteArray(com.hurlant.util.Base64.decodeToByteArray(_content))); 另一种怀疑是,我没有适当地从_content中删除“填充”/适当地设置我的IV (这是由填充指定的,我必须从_content中删除)。然而,这应该通过“简单”声明来完成。我一直在尝试,但没有成功:
var pad:IPad = new PKCS5
cipher = Crypto.getCipher("simple-aes", Hex.toArray(key),pad);
pad.setBlockSize(cipher.getBlockSize());有人能建议我如何解决这个问题吗?:)
非常感谢!
发布于 2014-05-05 15:18:29
好吧,终于找出了出了什么问题。除了一些AS3调整之外,我们错误地将文件传输为MP3/image (应该是text/html)。
我们的Python仍然如上。我们的AS3被调整到下面。
下面是我们使用的AS3类:
package com.xperiment.preloader
{
import com.greensock.loading.DataLoader;
import com.hurlant.crypto.Crypto;
import com.hurlant.crypto.symmetric.ICipher;
import com.hurlant.util.Base64;
import flash.events.Event;
import flash.utils.ByteArray;
public class EncryptedDataLoader extends DataLoader
{
private static var backlog:Vector.<EncryptedDataLoader>;
private static var cipher:ICipher;
private var decrypted:Boolean = true;
public function EncryptedDataLoader(urlOrRequest:*, vars:Object=null)
{
this.addEventListener(Event.COMPLETE,decryptL);
super(urlOrRequest, vars);
}
public function decryptL(e:Event):void {
trace("start decrypt");
e.stopImmediatePropagation();
this.removeEventListener(Event.COMPLETE,decryptL);
backlog ||= new Vector.<EncryptedDataLoader>;
backlog.push(this);
if(cipher) pingBacklog();
}
public function decipher():void
{
_content = Base64.decodeToByteArray( _content );
cipher.decrypt( _content );
decrypted=true;
this.dispatchEvent(new Event(Event.COMPLETE));
}
public static function setCipher(_key:String):void{
var keyBA:ByteArray = new ByteArray;
keyBA.writeMultiByte(_key, "iso-8859-1");
cipher = Crypto.getCipher("simple-aes", keyBA);
pingBacklog();
}
public static function kill():void{
cipher.dispose();
cipher = null;
}
public static function pingBacklog():void{
if(backlog){
var encrypted:EncryptedDataLoader;
while(backlog.length>0){
encrypted=backlog.shift();
encrypted.decipher();
}
backlog=null;
}
}
}}
https://stackoverflow.com/questions/23459055
复制相似问题