I am writing a small program for reading / writing text. The first time I come across this, since so far I'm new to C #, I would like to look at a simple example.
Difficult decision I think so far I can not understand.
I am writing a small program for reading / writing text. The first time I come across this, since so far I'm new to C #, I would like to look at a simple example.
Difficult decision I think so far I can not understand.
Thanks for the hints, I found a very simple example for them, although I’ll need to understand it in more detail, but it’s still very simple.
Encrypt text and write it to file
FileStream stream = new FileStream("C:\\mytext.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("Hello World!"); crStream.Write(data,0,data.Length); crStream.Close(); stream.Close();
We decrypt the text and display the result of the decryption in the console
FileStream stream = new FileStream("C:\\mytext.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(); Console.WriteLine(data); Console.ReadKey(); reader.Close(); stream.Close();
Now it remains to understand the operation of the keys, read the manual. to really understand how everything works.
Thanks for the good example. If you want Cyrillic text to display correctly, then when recording you should mute the code when recording.
var dstEncoding = Encoding.UTF8; FileStream stream = new FileStream("C:\\mytext.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 = dstEncoding.GetBytes("Hello World!"); crStream.Write(data,0,data.Length); crStream.Close(); stream.Close();
Source: https://ru.stackoverflow.com/questions/484226/
All Articles