Нужны исходники программы шифрования и дешифрования файлов на базе алгоритма ГОСТ 28147-89 - C#

Узнай цену своей работы

Формулировка задачи:

у кого есть исходники программы шифрования и дешифрования файлов на базе алгоритма ГОСТ 28147-89 в режиме простой замены и выработки имитовставки?

Решение задачи: «Нужны исходники программы шифрования и дешифрования файлов на базе алгоритма ГОСТ 28147-89»

textual
Листинг программы
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.IO;
  4. using GOST;
  5.  
  6. class TestGostSymmetric
  7. {
  8.     [STAThread]
  9.     static void Main(string[] args)
  10.     {
  11.         //byte[] plain_text = new byte[]{0,1,2,3,4,5,6,7,8,9,10};
  12.         byte[] plain_text = new Byte[512];
  13.         System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  14.         byte[] strbytes = enc.GetBytes("Novodachnaja.  50 years anniversary 7890");
  15.         Array.Copy(strbytes, 0, plain_text, 0, strbytes.Length);
  16.  
  17.         byte[] key = new Byte[32]{0,0,0,0,1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0,5,0,0,0,6,0,0,0,7,0,0,0};
  18.         byte[] iv = new Byte[8] {0,1,2,3,4,5,6,7};
  19.  
  20.         Console.WriteLine("Plain text to encrypt:");
  21.         PrintByteArray(plain_text);
  22.  
  23.         GOSTsymmManaged gost = new GOSTsymmManaged();
  24.         gost.LoadTestSBoxes(2);
  25.         gost.Key = key;
  26.         gost.IV = iv;
  27.         gost.Mode = CipherMode.CBC;
  28.  
  29.  
  30.         GOSTsymmTransform ct_e = (GOSTsymmTransform)gost.CreateEncryptor();
  31.         ct_e.UseExpandedSBoxes = false;
  32.         GOSTsymmTransform ct_d = (GOSTsymmTransform)gost.CreateDecryptor();
  33.  
  34.         MemoryStream ms1 = new MemoryStream();
  35.         MemoryStream ms2 = new MemoryStream();
  36.  
  37.         CryptoStream cs1 = new CryptoStream(ms1, ct_e, CryptoStreamMode.Write);
  38.  
  39.         cs1.Write(plain_text, 0, plain_text.Length);
  40.         cs1.Close();
  41.  
  42.             byte[] cipher_text = ms1.ToArray();
  43.  
  44.         Console.WriteLine("Cipher text:");
  45.         PrintByteArray(cipher_text);
  46.  
  47.         CryptoStream cs2 = new CryptoStream(ms2, ct_d, CryptoStreamMode.Write);
  48.  
  49.         cs2.Write(cipher_text, 0, cipher_text.Length);
  50.         cs2.Close();
  51.  
  52.         byte[] decrypted_text = ms2.ToArray();
  53.  
  54.         Console.WriteLine("DecryptedText:");
  55.         PrintByteArray(decrypted_text);
  56.  
  57.         Console.WriteLine("Done.");
  58.     }
  59.  
  60.  
  61.  
  62.     static void PrintByteArray(Byte[] arr)
  63.     {
  64.         if (arr==null)
  65.         {
  66.             Console.WriteLine("null");
  67.             return;
  68.         }
  69.         int i;
  70.         for (i=0; i<arr.Length; i++)
  71.         {
  72.             Console.Write("{0:X} ", arr[i]);
  73.             if ( (i+9)%8 == 0 ) Console.WriteLine();
  74.         }
  75.         if (i%8 != 0) Console.WriteLine();
  76.     }
  77.  
  78. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4.5 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы