Good all the time of day!
The project has the need to encrypt data. I downloaded the encryption code from the Internet. fiddled with him. Pretty changed ...
When I started testing, I noticed that the encryption procedure works perfectly, but the declining process refuses ...

// Процедура дешифрования public static string DecryptData(string data) { MemoryStream memorystream; StreamReader streamreader; CryptoStream crstream; // Определить алгоритм SymmetricAlgorithm Alg = DefineAlg(); ICryptoTransform Cryptor = CreateDec(Alg); // Получаем массив байтов byte[] bytedata = ToBytes(data); memorystream = new MemoryStream(bytedata); crstream = new CryptoStream(memorystream, Cryptor, CryptoStreamMode.Read); streamreader = new StreamReader(crstream); // Показываем результат string decdata = streamreader.ReadToEnd(); // Закрываем потоки crstream.Close(); streamreader.Close(); memorystream.Close(); return decdata; } 

I can not understand why. Suspicions

  string decdata = streamreader.ReadToEnd(); 

Maybe it's in the stream. When compiling, it flies away from here, and most importantly, no exceptions pop up ... Help me figure it out.
Thanks in advance.

    1 answer 1

    Most likely, the string you have crooked encrypted.

    After encryption is completed, before closing the stream in which encryption occurs, call CryptoStream.FlushFinalBlock ().

    Also check that when encrypting, you first close the CryptoStream, and then the stream in which encryption occurs.

    Still. When working with threads, I recommend using the using block. This is more reliable than calling Close after the operation is completed, since it guarantees the release of resources regardless of the exceptions that occurred.

    • Thank you) plus) And what does this function do? - pelmeshka80
    • This method appends to the stream, on the basis of which CryptoStream is created, the data that the cryptographic algorithm may require when decrypting. - Modus