c# - Image Encrypting Getting Padding Error -


i developing image encrypting program. have got 2 applications. 1 of them, converting image byte array , encrypting rijndael. after saving encrypted byte array file. second application decrypting. reading byte array file. after decrypt , show image in picturebox.

but geting "padding invalid , cannot removed." error in decryption application.

now saving encrypted byte array file code (i not sure true way byte array file ?);

protected bool savedata(string filename, byte[] data)         {             binarywriter writer = null;              try             {                 // create new stream write file                 writer = new binarywriter(file.open(filename,filemode.openorcreate));                  // writer raw data                                 writer.write(data);                 writer.flush();                 writer.close();             }             catch             {                 return false;             }              return true;         } 

i giving method save file location , encrypted byte array. , worked. dont know, correct way ?

and decryption application reading encrypted byte array file method;

protected byte[] getdata(string filename) {     fileinfo f = new fileinfo(filename);     binaryreader br = new binaryreader(file.open(filename, filemode.open));     byte[] = br.readbytes(convert.toint32(f.length));     return a; } 

and error location decryption method;

public static byte[] decryptbytes(byte[] encryptedbytes, string passphrase, string saltvalue)         {             rijndaelmanaged rijndaelcipher = new rijndaelmanaged();              rijndaelcipher.mode = ciphermode.cbc;             byte[] salt = encoding.ascii.getbytes(saltvalue);             passwordderivebytes password = new passwordderivebytes(passphrase, salt, "sha1", 2);              icryptotransform decryptor = rijndaelcipher.createdecryptor(password.getbytes(32), password.getbytes(16));              memorystream memorystream = new memorystream(encryptedbytes);             cryptostream cryptostream = new cryptostream(memorystream, decryptor, cryptostreammode.read);             byte[] plainbytes = new byte[encryptedbytes.length];               int decryptedcount = cryptostream.read(plainbytes, 0, plainbytes.length); // getting error line. padding invalid , cannot removed.                memorystream.flush();             cryptostream.flush();             memorystream.close();             cryptostream.close();              return plainbytes;         } 

encryption code

  public static byte[] encryptbytes(byte[] inputbytes, string passphrase, string saltvalue)         {             rijndaelmanaged rijndaelcipher = new rijndaelmanaged();             rijndaelcipher.mode = ciphermode.cbc;             byte[] salt = encoding.ascii.getbytes(saltvalue);             passwordderivebytes password = new passwordderivebytes(passphrase, salt, "sha1", 2);             icryptotransform encryptor = rijndaelcipher.createencryptor(password.getbytes(32), password.getbytes(16));             memorystream memorystream = new memorystream();             cryptostream cryptostream = new cryptostream(memorystream, encryptor, cryptostreammode.write);             cryptostream.write(inputbytes, 0, inputbytes.length);             cryptostream.flushfinalblock();             byte[] cipherbytes = memorystream.toarray();             memorystream.close();             cryptostream.close();             return cipherbytes;         } 

full code decryption application

namespace imagedecrypte {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }          private string encpass;         private string line;         private string okunanveri;         private byte[] sifrelidosyadizi;         private byte[] cozulmusdosyadizi;         private const string saltpass = "codework";         private string sfre;         private string dyol;          private void button1_click(object sender, eventargs e)         {             openfiledialog file = new openfiledialog();             file.filter = "Şifrelenmiş dosyalar (*.cw)|*.cw";             file.filterindex = 2;             file.restoredirectory = true;             file.checkfileexists = false;             file.title = "Şifrelenmiş dosya seçiniz..";             file.initialdirectory =         environment.getfolderpath(environment.specialfolder.desktop);             if (file.showdialog() == dialogresult.ok)             {                 dyol = file.filename;                 string dosyaadi = file.safefilename;                 label1.text = dosyaadi;                 sfre = textbox1.text;             }         }          private void button2_click(object sender, eventargs e)         {             sfre = textbox1.text;             sifrelidosyadizi = getdata(dyol);             cozulmusdosyadizi = decryptbytes(sifrelidosyadizi, sfre, saltpass);             picturebox1.image = bytearraytoimage(cozulmusdosyadizi);         }          public static byte[] decryptbytes(byte[] encryptedbytes, string passphrase, string saltvalue)         {             rijndaelmanaged rijndaelcipher = new rijndaelmanaged();              rijndaelcipher.mode = ciphermode.cbc;             byte[] salt = encoding.ascii.getbytes(saltvalue);             passwordderivebytes password = new passwordderivebytes(passphrase, salt, "sha1", 2);              icryptotransform decryptor = rijndaelcipher.createdecryptor(password.getbytes(32), password.getbytes(16));              memorystream memorystream = new memorystream(encryptedbytes);             cryptostream cryptostream = new cryptostream(memorystream, decryptor, cryptostreammode.read);             byte[] plainbytes = new byte[encryptedbytes.length];               int decryptedcount = cryptostream.read(plainbytes, 0, plainbytes.length);               memorystream.flush();             cryptostream.flush();             memorystream.close();             cryptostream.close();              return plainbytes.take(decryptedcount).toarray();         }          public image bytearraytoimage(byte[] bytearrayin)         {             memorystream ms = new memorystream(bytearrayin);             image returnimage = image.fromstream(ms);             return returnimage;         }          //file byte array        ###################################################################         protected byte[] getdata(string filename)         {             fileinfo f = new fileinfo(filename);             binaryreader br = new binaryreader(file.open(filename, filemode.open));             byte[] = br.readbytes(convert.toint32(f.length));             return a;         }         //file byte array        ###################################################################     } } 

full code encryption application

namespace imageencrypte {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }          private string encpass;         private byte[] bytearrayforimage;         private byte[] bytearraycoded;         private const string saltpass = "codework";          private void button1_click(object sender, eventargs e)         {             openfiledialog file = new openfiledialog();             file.filter = "jpeg dosyası |*.jpg| png dosyası|*.png";             file.filterindex = 2;             file.restoredirectory = true;             file.checkfileexists = false;             file.title = "bir İmaj dosyası seçiniz..";             file.initialdirectory =         environment.getfolderpath(environment.specialfolder.desktop);             encpass = textbox1.text;             if (file.showdialog() == dialogresult.ok)             {                 string dosyayolu = file.filename;                 string dosyaadi = file.safefilename;                 label1.text = dosyaadi;                 image img = image.fromfile(dosyayolu);                 picturebox1.image = img;                 bytearrayforimage = imagetobytearray(img);                 bytearraycoded = encryptbytes(bytearrayforimage, encpass, saltpass);             }         }          private void button2_click(object sender, eventargs e)         {             savefiledialog sf = new savefiledialog();             sf.title = "Şifrelenmiş dosyayı kaydet";             sf.checkfileexists = false;             sf.checkpathexists = true;             sf.restoredirectory = true;             sf.defaultext = "cw";             sf.filename = "encodedfile";             sf.supportmultidottedextensions = false;             sf.filter = "Şifrelenmiş dosyalar (*.cw)|*.cw";             sf.initialdirectory =         environment.getfolderpath(environment.specialfolder.desktop);             if (sf.showdialog() == dialogresult.ok)             {                 string dosyayolu = sf.filename;                  bool cevap = savedata(dosyayolu, bytearraycoded);                 if (cevap)                 {                     messagebox.show("ok");                 }                 else                 {                     messagebox.show("problem");                 }               }         }           //image byte array      ####################################################################         public byte[] imagetobytearray(system.drawing.image imagein)         {             using (var ms = new memorystream())             {                 imagein.save(ms, system.drawing.imaging.imageformat.gif);                 return ms.toarray();             }         }         //image byte array      ####################################################################          //byte array file        ###################################################################         protected bool savedata(string filename, byte[] data)         {             binarywriter writer = null;              try             {                 // create new stream write file                 writer = new binarywriter(file.open(filename,filemode.openorcreate));                  // writer raw data                                 writer.write(data);                 writer.flush();                 writer.close();             }             catch             {                 return false;             }              return true;         }         //bytte array file       ###################################################################          //encryptbytes              ###################################################################         public static byte[] encryptbytes(byte[] inputbytes, string passphrase, string saltvalue)         {             rijndaelmanaged rijndaelcipher = new rijndaelmanaged();             rijndaelcipher.mode = ciphermode.cbc;             byte[] salt = encoding.ascii.getbytes(saltvalue);             passwordderivebytes password = new passwordderivebytes(passphrase, salt, "sha1", 2);             icryptotransform encryptor = rijndaelcipher.createencryptor(password.getbytes(32), password.getbytes(16));             memorystream memorystream = new memorystream();             cryptostream cryptostream = new cryptostream(memorystream, encryptor, cryptostreammode.write);             cryptostream.write(inputbytes, 0, inputbytes.length);             cryptostream.flushfinalblock();             byte[] cipherbytes = memorystream.toarray();             memorystream.close();             cryptostream.close();             return cipherbytes;         }         //encryptbytes              ###################################################################     } } 

what can before going crazy man ? thank , waiting precious answers.

thanks maximilian gerhardt solution here;

    public static byte[] encryptbytes(byte[] inputbytes, string passphrase, string saltvalue)     {         rijndaelmanaged rijndaelcipher = new rijndaelmanaged();         rijndaelcipher.mode = ciphermode.cbc;         rijndaelcipher.padding = paddingmode.pkcs7;          byte[] salt = encoding.utf32.getbytes(saltvalue);         //byte[] salt = encoding.ascii.getbytes(saltvalue);         passwordderivebytes password = new passwordderivebytes(passphrase, salt, "sha1", 2);         icryptotransform encryptor = rijndaelcipher.createencryptor(password.getbytes(32), password.getbytes(16));         memorystream memorystream = new memorystream();         cryptostream cryptostream = new cryptostream(memorystream, encryptor, cryptostreammode.write);         cryptostream.write(inputbytes, 0, inputbytes.length);         cryptostream.flushfinalblock();         cryptostream.flush();         byte[] cipherbytes = memorystream.toarray();         memorystream.close();         cryptostream.close();         return cipherbytes;     }       public static byte[] decryptbytes(byte[] encryptedbytes, string passphrase, string saltvalue)     {         rijndaelmanaged rijndaelcipher = new rijndaelmanaged();          rijndaelcipher.mode = ciphermode.cbc;         rijndaelcipher.padding = paddingmode.pkcs7;         byte[] salt = encoding.utf32.getbytes(saltvalue);         //byte[] salt = encoding.ascii.getbytes(saltvalue);         passwordderivebytes password = new passwordderivebytes(passphrase, salt, "sha1", 2);          icryptotransform decryptor = rijndaelcipher.createdecryptor(password.getbytes(32), password.getbytes(16));          memorystream memorystream = new memorystream(encryptedbytes);         cryptostream cryptostream = new cryptostream(memorystream, decryptor, cryptostreammode.read);         byte[] plainbytes = new byte[encryptedbytes.length];           int decryptedcount = cryptostream.read(plainbytes, 0, plainbytes.length);           memorystream.flush();         cryptostream.flush();         memorystream.close();         cryptostream.close();          return plainbytes.take(decryptedcount).toarray();     }      public static byte[] getdata(string filename)     {         return file.readallbytes(filename);     }      protected bool savedata(string filename, byte[] data)     {         try         {             file.writeallbytes(filename, data);             return true;         }         catch         {             return false;         }     } 

Comments