Reading from the cryptostom to the end of the stream

I have problems with the code below. I have a file in a temporary place that needs encryption, this function encrypts the data, which is then stored in the location "pathToSave".

When checking, the whole file does not seem to be processed properly. There are no bits in my release, and I suspect that it has something to do with a while loop that does not go through the entire stream.

Aside, if I try to call CryptStrm.Close () after a while loop, I get an exception. This means that if I try to decrypt the file, I get a file already using the error!

I tried everything ordinary and Ive looked at similar problems here, any help would be great.

thanks

public void EncryptFile(String tempPath, String pathToSave) { try { FileStream InputFile = new FileStream(tempPath, FileMode.Open, FileAccess.Read); FileStream OutputFile = new FileStream(pathToSave, FileMode.Create, FileAccess.Write); RijndaelManaged RijCrypto = new RijndaelManaged(); //Key byte[] Key = new byte[32] { ... }; //Initialisation Vector byte[] IV = new byte[32] { ... }; RijCrypto.Padding = PaddingMode.None; RijCrypto.KeySize = 256; RijCrypto.BlockSize = 256; RijCrypto.Key = Key; RijCrypto.IV = IV; ICryptoTransform Encryptor = RijCrypto.CreateEncryptor(Key, IV); CryptoStream CryptStrm = new CryptoStream(OutputFile, Encryptor, CryptoStreamMode.Write); int data; while (-1 != (data = InputFile.ReadByte())) { CryptStrm.WriteByte((byte)data); } } catch (Exception EncEx) { throw new Exception("Encoding Error: " + EncEx.Message); } } 

EDIT:

I made the assumption that my problem is with encryption. My decryption may be the culprit

  public String DecryptFile(String encryptedFilePath) { FileStream InputFile = new FileStream(encryptedFilePath, FileMode.Open, FileAccess.Read); RijndaelManaged RijCrypto = new RijndaelManaged(); //Key byte[] Key = new byte[32] { ... }; //Initialisation Vector byte[] IV = new byte[32] { ... }; RijCrypto.Padding = PaddingMode.None; RijCrypto.KeySize = 256; RijCrypto.BlockSize = 256; RijCrypto.Key = Key; RijCrypto.IV = IV; ICryptoTransform Decryptor = RijCrypto.CreateDecryptor(Key, IV); CryptoStream CryptStrm = new CryptoStream(InputFile, Decryptor, CryptoStreamMode.Read); String OutputFilePath = Path.GetTempPath() + "myfile.name"; StreamWriter OutputFile = new StreamWriter(OutputFilePath); OutputFile.Write(new StreamReader(CryptStrm).ReadToEnd()); CryptStrm.Close(); OutputFile.Close(); return OutputFilePath; } 
+4
source share
2 answers

Well, there are a few things.

1. Your IV size setting is too large. If you have a key size of 256, the IV for Rijndael is 16 bytes. If you try to set a key with more bytes than you get an exception. The size IV should be set to the block size / 8 cm. MSDN , so that you have the correct size.

  1. You have an optional None mode. If your data exactly matches the block size, then you will have problems. You probably want to select the fill mode.
  2. Usually you need to call FlushFInalBlock when you finish writing the crypto stream, when you encrypt so that all records are written to the stream.

I am posting some sample code using memory streams to show encryption and decryption.

 var tempData = "This is the text to encrypt. It not much, but it all there is."; using (var rijCrypto = new RijndaelManaged()) { byte[] encryptedData; rijCrypto.Padding = System.Security.Cryptography.PaddingMode.ISO10126; rijCrypto.KeySize = 256; using (var input = new MemoryStream(Encoding.Unicode.GetBytes(tempData))) using (var output = new MemoryStream()) { var encryptor = rijCrypto.CreateEncryptor(); using (var cryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write)) { var buffer = new byte[1024]; var read = input.Read(buffer, 0, buffer.Length); while (read > 0) { cryptStream.Write(buffer, 0, read); read = input.Read(buffer, 0, buffer.Length); } cryptStream.FlushFinalBlock(); encryptedData = output.ToArray(); } } using (var input = new MemoryStream(encryptedData)) using (var output = new MemoryStream()) { var decryptor = rijCrypto.CreateDecryptor(); using (var cryptStream = new CryptoStream(input, decryptor, CryptoStreamMode.Read)) { var buffer = new byte[1024]; var read = cryptStream.Read(buffer, 0, buffer.Length); while (read > 0) { output.Write(buffer, 0, read); read = cryptStream.Read(buffer, 0, buffer.Length); } cryptStream.Flush(); var result = Encoding.Unicode.GetString(output.ToArray()); } } } 
+13
source

KeySize must be specified first. It seems like an error, for example, it works

  using (var aes = new AesManaged()) { aes.KeySize = 256; aes.Mode = CipherMode.CBC; aes.IV = iv; aes.Key = passwordHash; aes.Padding = PaddingMode.PKCS7; 

But it is not

  using (var aes = new AesManaged()) { aes.Mode = CipherMode.CBC; aes.IV = iv; aes.Key = passwordHash; aes.Padding = PaddingMode.PKCS7; aes.KeySize = 256; 
0
source

Source: https://habr.com/ru/post/1401657/


All Articles