c# - FileNotFoundException when using FileStream to Encrypt -


i using c# in visual studio windows form application write program can encrypt , decrypt files. following walkthrough: https://msdn.microsoft.com/en-us/library/aa964697(v=vs.85).aspx , have completed minor changes made environment , preferences.

when try encrypt file given 'filenotfoundexception unhandled' error when program tries use filestream encrypt file. point seems working.

here code encryptfile method:

private void encryptfile(string infile)     {         // create instance of rijndael symetric encryption of data.         rijndaelmanaged rjndl = new rijndaelmanaged();         rjndl.keysize = 256;         rjndl.blocksize = 256;         rjndl.mode = ciphermode.cbc;         icryptotransform transform = rjndl.createencryptor();          // use rsacryptoserviceprovider enrypt rijndael key.         byte[] keyencrypted = rsa.encrypt(rjndl.key, false);          // create byte arrays contain length values of key , iv.         byte[] lenk = new byte[4];         byte[] leniv = new byte[4];          int lkey = keyencrypted.length;         lenk = bitconverter.getbytes(lkey);         int liv = rjndl.iv.length;         leniv = bitconverter.getbytes(liv);          // write following filestream encrypted file (outfs):         // - length of key         // - length of iv         // - ecrypted key         // - iv         // - encrypted cipher content          // change file's extension ".enc"         string outfile = encrfolder + infile.substring(0, infile.lastindexof(".")) + ".enc";          using (filestream outfs = new filestream(outfile, filemode.create))         {             outfs.write(lenk, 0, 4);             outfs.write(leniv, 0, 4);             outfs.write(keyencrypted, 0, lkey);             outfs.write(rjndl.iv, 0, liv);              // write cipher text using cryptostream encrypting.             using (cryptostream outstreamencrypted = new cryptostream(outfs, transform, cryptostreammode.write))             {                 // encrypting chunk @ time, can save memory , accommodate large files.                 int count = 0;                 int offset = 0;                  // blocksizebytes can arbitrary size.                 int blocksizebytes = rjndl.blocksize / 8;                 byte[] data = new byte[blocksizebytes];                 int bytesread = 0;                  using (filestream infs = new filestream(infile, filemode.open))                 {                                         {                         count = infs.read(data, 0, blocksizebytes);                         offset += count;                         outstreamencrypted.write(data, 0, count);                         bytesread += blocksizebytes;                     }                     while (count > 0);                     infs.close();                 }                 outstreamencrypted.flushfinalblock();                 outstreamencrypted.close();             }             outfs.close();         }     } 

the error happens @ line "using (filestream infs = new filestream(infile, filemode.open))". here image of error: http://imgur.com/kwufeva

what causing error , fix?

you may overthink line :

string outfile = encrfolder + infile.substring(0, infile.lastindexof(".")) + ".enc"; 

(https://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx)


Comments