Итак, вот мой беспорядок. Он возвращает случайные символы Unicode. Я использую тот же ключ для обоих методов, а также тот же IV, и я использую одну и ту же кодировку для них обоих. Что вызывает случайный ответ?AES в обе стороны, производя случайные символы
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace FileFish
{
class Program
{
public static void Main()
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.GenerateIV();
Console.WriteLine(Decrypt(Encoding.UTF8.GetBytes("APPLEAPPLEAPPLEAPPLEAPPLEAPPLEAP"), aes.IV, Encrypt(Encoding.UTF8.GetBytes("APPLEAPPLEAPPLEAPPLEAPPLEAPPLEAP"), aes.IV, "cheese")));
Console.ReadKey(true);
}
private static byte[] Encrypt(byte[] key, byte[] iv, string plaintext)
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.Key = key;
aes.IV = iv;
ICryptoTransform encryptor = aes.CreateEncryptor();
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(new CryptoStream(ms, encryptor, CryptoStreamMode.Write));
sw.Write(plaintext);
return ms.ToArray();
}
private static string Decrypt(byte[] key, byte[] iv, byte[] ciphertext)
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.Key = key;
aes.IV = iv;
ICryptoTransform decryptor = aes.CreateEncryptor();
MemoryStream ms = new MemoryStream(ciphertext);
StreamReader sr = new StreamReader(new CryptoStream(ms, decryptor, CryptoStreamMode.Read));
return sr.ReadToEnd();
}
}
}
«Вместо t зная, что вы будете кататься самостоятельно, вам лучше использовать то, что, как доказано, работает ». - хороший совет. –