Can't decrypt video using C# and Rijndael -
i can't decrypt video file encrypted using rijndael. have @ encryption routine:
using (filestream _streaminput = new filestream(inputpath,filemode.open,fileaccess.read)) { using (filestream _streamoutput = new filestream(outputpath,filemode.create,fileaccess.write)) { rijndaelmanaged _cryptorm = new rijndaelmanaged(); unicodeencoding _encodingunicode = new unicodeencoding(); byte[] _key = _encodingunicode.getbytes(string.isnullorempty(custompassword)?_mediapass:custompassword); using (cryptostream _streamcrypto = new cryptostream(_streamoutput, _cryptorm.createencryptor(_key,_key), cryptostreammode.write)) { long _bufferlength = _streaminput.length; if (_bufferlength>_encryptedblock) { byte[] _encryptedbuffer = new byte[_encryptedblock]; byte[] _unencryptedbufferblock = new byte[_encryptedblock]; // encrypted block _streaminput.read(_encryptedbuffer,0,(int)_encryptedblock); _streamcrypto.write(_encryptedbuffer,0,(int)_encryptedblock); // rest int _readbytescount = 0; while ((_readbytescount=_streaminput.read(_unencryptedbufferblock,0,_encryptedblock))!=0) { _streamoutput.write(_unencryptedbufferblock,0,(int)_readbytescount); } } _streamcrypto.dispose(); } _cryptorm.dispose(); _streamoutput.dispose(); } _streaminput.dispose(); }
and decryption routine:
using (filestream _streaminput = new filestream(inputpath, filemode.open, fileaccess.read)) { using (filestream _streamoutput = new filestream(outputpath, filemode.create, fileaccess.write)) { rijndaelmanaged _cryptorm = new rijndaelmanaged(); unicodeencoding _encodingunicode = new unicodeencoding(); byte[] _key = _encodingunicode.getbytes(prepareencryptionkey(string.isnullorempty(custompassword)?_mediapass:custompassword,"")); seek { using (cryptostream _streamcrypto = new cryptostream(_streaminput, _cryptorm.createdecryptor(_key, _key), cryptostreammode.read)) { seek { int _readbytescount = 0; //encrypted block byte[] _buffer = new byte[_encryptedblock]; byte[] _bufferunencrypted = new byte[_encryptedblock]; int decrypt_length = _streamcrypto.read(_buffer, 0, _encryptedblock); _streamoutput.write(_buffer, 0, decrypt_length); // rest while ((_readbytescount = _streaminput.read(_bufferunencrypted, 0, _encryptedblock)) != 0) { _streamoutput.write(_bufferunencrypted, 0, _readbytescount); } } grab { } _streamcrypto.dispose(); } } grab { } _cryptorm.dispose(); _streamoutput.dispose(); } _streaminput.dispose(); }
other points: - no error beingness thrown video not playing. - size of decrypted file same of encrypted file.
edit:
private static string prepareencryptionkey(string key, string part) { if (part == "") { if (key.length != 8) { homecoming key.padright(8, '~'); } else { homecoming key; } } else { if (key.length != (8 - part.length)) { if (key.length > (8 - part.length)) { homecoming key.substring(0, (8 - part.length)) + part; } else { homecoming key.padright((8 - part.length), '~') + part; } } else { homecoming key + part; } } }
it seems both methods needs fixes:
1/ encrypt part - cannot write output info straight _streamoutput _streamcrypto. otherwise write have taken input file. per original code, encrypt first block of data.
while ((_readbytescount = _streaminput.read(_unencryptedbufferblock, 0, encryptedblocksize)) != 0) { _streamcrypto.write(_unencryptedbufferblock, 0, (int)_readbytescount); }
2/ decrypt part - cannot read input stream straight _streamcrypto
while ((_readbytescount = _streamcrypto.read(_bufferunencrypted, 0, encryptedblocksize)) != 0) { _streamoutput.write(_bufferunencrypted, 0, _readbytescount); }
3/ prepareencryptionkey method should used in both encryption , decryption method. ensure have same in both methods.
c# video encryption rijndael rijndaelmanaged
No comments:
Post a Comment