c# - How to Decrypt File which was encrypted using RijndaelManaged -


i can encrypt image file. can not decrypt file.

while ((readlen = cryptstrm.read(bs, 0, bs.length)) > 0)

anyone can guess part wrong? code programmed following.

when read encrypted file, can not read @ all. , cryptostream's property called length , position has "notsupportedexception", when see cryptstream's property using vss.

i waste many hours solve problem..... pls me.....

encrypt [bitmap >> encrypted fie]

decrypt [encrypted fie >> file]


encrypt

    public static void encryptfile(         bitmap bmp, string destfile, byte[] key, byte[] iv)     {          system.security.cryptography.rijndaelmanaged rijndael =             new system.security.cryptography.rijndaelmanaged();          rijndael.key = key;         rijndael.iv = iv;          system.io.filestream outfs = new system.io.filestream(             destfile, system.io.filemode.create, system.io.fileaccess.write);          system.security.cryptography.icryptotransform encryptor =             rijndael.createencryptor();          system.security.cryptography.cryptostream cryptstrm =             new system.security.cryptography.cryptostream(                 outfs, encryptor,                 system.security.cryptography.cryptostreammode.write);          memorystream ms = new memorystream();         bmp.save(ms, imageformat.jpeg);           byte[] bs = new byte[1024];         int readlen;         while ((readlen = ms.read(bs, 0, bs.length)) > 0)         {             cryptstrm.write(bs, 0, readlen);         }          ms.close();         cryptstrm.close();         encryptor.dispose();         outfs.close();     } 

decrypt

    public static void decryptfile(         string sourcefile, string destfile, byte[] key, byte[] iv)     {          system.security.cryptography.rijndaelmanaged rijndael =             new system.security.cryptography.rijndaelmanaged();          rijndael.key = key;         rijndael.iv = iv;          system.io.filestream infs = new system.io.filestream(             sourcefile, system.io.filemode.open, system.io.fileaccess.read);          system.security.cryptography.icryptotransform decryptor =             rijndael.createdecryptor();          system.security.cryptography.cryptostream cryptstrm =             new system.security.cryptography.cryptostream(                 infs, decryptor,                 system.security.cryptography.cryptostreammode.read);          system.io.filestream outfs = new system.io.filestream(             destfile, system.io.filemode.create, system.io.fileaccess.write);         byte[] bs = new byte[1024];         int readlen;          while ((readlen = cryptstrm.read(bs, 0, bs.length)) > 0)         {             outfs.write(bs, 0, readlen);         }          outfs.close();         cryptstrm.close();         decryptor.dispose();         infs.close();     } 

try setting mode , padding members of rijndael.
default padding mode caused problems when did similar implementation.

             // reasonable set encryption mode cipher block chaining             // (cbc). use default options other symmetric key parameters.             rijndael.mode = ciphermode.cbc;             rijndael.padding = paddingmode.none; 


Comments