views:

203

answers:

2

I'm trying to encrypt/decrypt files in flex (AIR) using the as3crypto package. the problem is that when attempting to process slightly large files (over 5M) the process time gets ridiculously long and the client freezes (get the "not responding" title) so i tried to go Async and encrypt/decrypt a chunk at a time and interlace it with the frame refresh rate.

the encryption goes smooth, or so it seems, but when i try to decrypt the result back to the original document i get a padding error: "Error: PKCS#5:unpad: Invalid padding value. expected [252], found [152]"

my code is like so (between initiation and finalization):

  • the run method gets called repeatedly until the file is completed
  • _buffer contains the byte array from the source file
  • _result the result
  • CHUNK is the bite size of bytes that i process each time
  • the cipher is initiated as: Crypto.getCipher("aes-ecb", _key, Crypto.getPad("pkcs5"));

    public function run(data:Object):Boolean{
    
    
    
    if((_buffer.length-_position)>CHUNK){
        processChunk(_position,CHUNK);
        _position += CHUNK;
        var e:ProgressEvent = new ProgressEvent(ProgressEvent.PROGRESS,false,false,_position,_buffer.length);
        this.dispatchEvent(e);
        return true;
    }else if(!_isFinnalized){
        processChunk(_position,_buffer.length - _position);
        this.dispatchEvent(new Event(Event.COMPLETE));
        finnalize();
    
    
    }
    
    
    return false;
    
    } private function processChunk(position:uint,chunk:uint):void{ var buffer:ByteArray = new ByteArray(); buffer.writeBytes(_buffer,position,chunk); if(_action==ENCRYPT){ _aes.encrypt(buffer); }else{ _aes.decrypt(buffer); } _result.writeBytes(buffer); }

help me, please!

A: 

..found the bug.

the encryption and decryption actions change the actual chunk size. in the processChunck method, the ByteArray length before encryption was 16400 and after changed to 16416. using the decrypt changes the ByteArray length from 16416 back to 16400.

hence the solution i chose was to use two numeric values, one for each method used.

I have no idea why this happens, its not the padding (tested it with Crypto.getPad("none")).

I'll be posting the code on my blog in a day or two. (http://giladmanor.com)

Gilad Manor