Код: Выделить всё
byte[] cipher;
using (Aes algo= Aes.Create())
{
algo.Mode = CipherMode.ECB;
algo.Key = key;
algo.KeySize = key.Length * 8;
algo.Padding = PaddingMode.None;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = algo.CreateEncryptor(algo.Key, null);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
// Sometimes another using StreamWriter/StreamReader if supplying
// string instead of byte[]
csEncrypt.Write(plain);
csEncrypt.FlushFinalBlock();
}
cipher = msEncrypt.ToArray();
}
}
Код: Выделить всё
byte[] cipher;
using (Aes algo= Aes.Create())
{
algo.Mode = CipherMode.ECB;
algo.Key = Key;
algo.KeySize = key.Length * 8;
algo.Padding = PaddingMode.None;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = algo.CreateEncryptor(key, null);
cipher = encryptor.TransformFinalBlock(plain, 0, plain.Length);
}
Я пытался рассчитать время, чтобы увидеть, работает ли один быстрее, чем другой, но секундомер просто дает мне совершенно разные результаты. , даже после 100 000 запусков.
Примечание: это всего лишь простой пример – нет нужды распространяться о том, насколько плох ЕЦБ.
В примере C# веб-сайта MS используется метод MemoryStream/CryptoStream, поэтому
- Это более эффективно?
- они просто дают пример использования CryptoStream?
Подробнее здесь: https://stackoverflow.com/questions/790 ... encryption
Мобильная версия