There is a calculator application in which all calculations will be encrypted, saved to a text file (each calculation on a new line), and when you call the History window, the contents of the file are decrypted, a window opens with a textBlock'ом , into which the calculation history is placed.

There is a string that I pass to the method for encryption (the string gets the value after clicking the = button):

 story_str = numb1.ToString() + " " + operation + " " + numb2.ToString() + " = " + res + Environment.NewLine; 

Method for encryption (called after pressing the = button):

 private void ToStory(string a) { FileStream stream = new FileStream("E:\\calculations.txt", FileMode.OpenOrCreate, FileAccess.Write); DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider(); cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH"); cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH"); CryptoStream crStream = new CryptoStream(stream, cryptic.CreateEncryptor(), CryptoStreamMode.Write); byte[] data = ASCIIEncoding.ASCII.GetBytes(a); crStream.Write(data, 0, data.Length); crStream.Close(); stream.Close(); } 

Method to decrypt:

 private void FromStory() { FileStream stream = new FileStream("E:\\calculations.txt", FileMode.Open, FileAccess.Read); DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider(); cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH"); cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH"); CryptoStream crStream = new CryptoStream(stream, cryptic.CreateDecryptor(), CryptoStreamMode.Read); StreamReader reader = new StreamReader(crStream); string data = reader.ReadToEnd(); Top_field.Text = data; reader.Close(); stream.Close(); } 

However, in this case, only the last calculation is saved in the file. And how to make it so that everything is saved and with the transfer to a new line?

  • And what about WPF? You have no bindings, no MVVM ... - EvgeniyZ
  • @EvgeniyZ WPF does not appear in this part of the code. In the application, it is used for building, creating custom controls and some other features - Moonlight
  • Well, in this part of the code, I only see Top_field.Text = data; - this can be done in WinForm. Conclusion - for us from the WPF tag to sense zero .. - EvgeniyZ
  • @EvgeniyZ took note - Moonlight

0