UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
byte[] inputByteArray = unicodeEncoding.GetBytes(inputString);
byte[] outputByteArray;
MemoryStream outputMemoryStream = new MemoryStream();
DES des = new DESCryptoServiceProvider();
CryptoStream cryptoStream =
new CryptoStream(outputMemoryStream,
des.CreateEncryptor(Cryptographer.Key,
Cryptographer.IV),
CryptoStreamMode.Write);
cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
outputMemoryStream.Position = 0;
outputByteArray=outputMemoryStream.ToArray();
cryptoStream.Close();
outputMemoryStream.Close();
return unicodeEncoding.GetString(outputByteArray);
Всё отрабатывает и даже потом расшифровывается:
UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
byte[] inputByteArray = unicodeEncoding.GetBytes(inputString);
byte[] outputByteArray;
MemoryStream outputMemoryStream = new MemoryStream();
DES des = new DESCryptoServiceProvider();
CryptoStream cryptoStream =
new CryptoStream(outputMemoryStream,
des.CreateDecryptor(Cryptographer.Key,
Cryptographer.IV),
CryptoStreamMode.Write);
cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
outputMemoryStream.Position = 0;
outputByteArray=outputMemoryStream.ToArray();
outputMemoryStream.Close();
return unicodeEncoding.GetString(outputByteArray);
Вначале естественно была инициализация:
static Cryptographer()
{ //загрузить определённые ключи
byte[] cr = { 155, 156, 135, 170, 210, 232, 108, 198 };
byte[] field = { 105, 25, 131, 203, 171, 234, 78, 8 };
DES alg = DES.Create();
alg.Key = cr;
alg.IV = field;
alg.Mode = CipherMode.ECB;
Cryptographer.IV = alg.IV;
Cryptographer.Key = alg.Key;
/* или же можно сгенерить
DES des = new DESCryptoServiceProvider();
des.GenerateIV();
des.GenerateKey();
Cryptographer.IV = des.IV;
Cryptographer.Key = des.Key;
*/
}
|