C# Pass Encrypted Data from a file into a memory stream. Throwing exception -


Sort a continuation with the previous question, have a bunch of googling, but I do not think I'm right here Basically what I am doing is opening a code such as a segment of code:

  byte [] cipher text = file. Read AllBytes (encrypted file);  

Then pass a byte array through a steady force method which calls the actual decryption method and if no cryptographic exception is thrown, then it returns the correct one. On my main form, I got a button click which, if I say static bolt from the statement, then unlock some additional interface items, if this truth comes back, and the "invalid password" message box Pops up if it goes wrong.

I stepped through the whole process, and throwing it "padding is invalid and can not be removed." The exception is that, some searches have shown that when the wrong decryption key should be supplied. I know that I am passing the same key, at least I'm typing it, so I'm assuming that the problem is either byte [] passed or with itself Here is an example of the decryption method:

  public static string decrypt (string password, byte [] ciphertext) {byte [] key, iv; CreateKeyIV (password, outside key, iv out); (Cryptostam deck = new cryptostream (encrypted, algorithm.createcryptter (key, iv), cryptostem mod., Using (StreamReader Reader = new streamrider (DC)) (memorystream encrypted = new memorystream) ) {Try {return reader.ReadToEnd (); } Catch (cryptographic expansion) {new cryptographic expression ("invalid password."); } Finally {encrypted.Close (); Dec.Close (); Reader.Close (); }}}  

Is anyone missing me?

I will not open CryptoStream on a StreamReader But actually I will get the decrypted data directly by passing read class cryptostream , a byte [] buffer.


This is my snippet, I used it with MemoryStream and FileStream.

  stream s = ...; ICryptoTransform cTransform = Encryptor.CreateDecryptor (); (Cryptoscript cStream = new cryptoStream (s, cTransform, CryptoStreamMode.Read)) {byte [] mCryptoData = new byte [s.Length]; CStream.Read (mCryptoData, 0, mCryptoData.Length); ...  

Comments