Good day!
A class question I am writing in C #
The class should encrypt the string and decrypt .... The problems with the errors described in my other question have been resolved.
Now the class is working, but the encryption and decryption is rather strange
1. Suppose we encrypt the string "My Mom Is Very Cool".
2. In exchange, we get "䷓ 䑾 ョ 蜻  摼 ዮ 䈱 㑴 㑴  쇅 ︉ 䵚 ⮊⟠Ⰹ 촌 ".
3. When deciphering the resulting: "My Mom Is Ve ᢐ 뫛 늄눵 ⟥ol"
4. When you re-encrypt "䷓ 䑾 ョ 蜻  摼 ዮ 䈱 㑴 㑴  쇅 ︉ 䵚 ⮊⟠Ⰹ 촌 ". (same as item 2)

Below I post the entire class code.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Security.Cryptography; namespace OD { public class Cryptography { // Ключ и IV для TripleDES static byte[] TripleKey = { 77, 79, 156, 172, 12, 40, 96, 226, 93, 78, 90, 103, 186, 78, 117, 0, 85, 127, 114, 91, 148, 210, 242, 255 }; static byte[] TripleIV = { 19, 127, 43, 85, 21, 117, 80, 151 }; // Процедура шифрования public static string EncryptData(string data) { // Определяем алгоритм SymmetricAlgorithm Alg = DefineAlg(); ICryptoTransform Cryptor = CreateEnc(Alg); // Получаем массив байтов byte[] bytedata = ToBytes(data); byte[] encbytedata; using (MemoryStream memorystream = new MemoryStream()) { using (CryptoStream cryptostream = new CryptoStream(memorystream, Cryptor, CryptoStreamMode.Write)) { // Шифруем cryptostream.Write(bytedata, 0, bytedata.Length); cryptostream.FlushFinalBlock(); cryptostream.Flush(); encbytedata = memorystream.ToArray(); } } UnicodeEncoding enc = new UnicodeEncoding(); return enc.GetString(encbytedata); } // Процедура дешифрования public static string DecryptData(string data) { // Определить алгоритм SymmetricAlgorithm Alg = DefineAlg(); ICryptoTransform Cryptor = CreateDec(Alg); // Получаем массив байтов byte[] bytedata = ToBytes(data); string decdata; using (MemoryStream memorystream = new MemoryStream(bytedata)) { using (CryptoStream cryptostream = new CryptoStream(memorystream, Cryptor, CryptoStreamMode.Read)) { using (BinaryReader streamreader = new BinaryReader(cryptostream)) { UnicodeEncoding enc = new UnicodeEncoding(); decdata = enc.GetString(streamreader.ReadBytes(bytedata.Length)); } } } return decdata; } // Функция, преобразующая строку в массив байтов static byte[] ToBytes(string s) { UnicodeEncoding enc = new UnicodeEncoding(); return enc.GetBytes(s); } // Функция, возвращающая выбранный алгоритм шифрования static SymmetricAlgorithm DefineAlg() { return new TripleDESCryptoServiceProvider(); } // Функция, создающая интерфейс-шифратор static ICryptoTransform CreateEnc(SymmetricAlgorithm AlgType) { return AlgType.CreateEncryptor(TripleKey, TripleIV); } // Функция, создающая интерфейс-дешифратор static ICryptoTransform CreateDec(SymmetricAlgorithm AlgType) { return AlgType.CreateDecryptor(TripleKey, TripleIV); } } } 

Please tell me why the whole problem?
Thanks in advance.

    1 answer 1

    And what the hell do you run through the string? This is not correct. The result of encrypting a string is a set of bytes. Well then work with bytes. If you need to display, then run through BASE64. Surely your strange "hallucinations" are caused by the display of bytes in Unicode and back, in which data is distorted.

    • Oh ... really ... what it is I)) Thank you very much) I'll give you a bonus - pelmeshka80