Advertisement
vovanhik_24

Task1.1

Apr 8th, 2025
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Task1_1
  6. {
  7.     public class Program
  8.     {
  9.         const int BlockSize = 4;
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             Console.Write("Введите текст для шифрования: ");
  14.             string input = Console.ReadLine() ?? "";
  15.  
  16.             Console.Write("Введите ключ (строка): ");
  17.             string keyInput = Console.ReadLine() ?? "";
  18.  
  19.             byte[] key = GetKeyBytes(keyInput);
  20.             byte[] plaintext = Encoding.UTF8.GetBytes(input);
  21.  
  22.             byte[] iv = GenerateRandomBlock();
  23.             Console.WriteLine($"IV: {BitConverter.ToString(iv)}");
  24.  
  25.             byte[] ciphertext = Encrypt(plaintext, key, iv);
  26.             Console.WriteLine($"Зашифрованный текст (в hex): {BitConverter.ToString(ciphertext)}");
  27.  
  28.             byte[] decrypted = Decrypt(ciphertext, key, iv);
  29.             string decryptedText = Encoding.UTF8.GetString(decrypted).TrimEnd('\0');
  30.  
  31.             Console.WriteLine($"Расшифрованный текст: {decryptedText}");
  32.         }
  33.  
  34.         static byte[] GetKeyBytes(string keyInput)
  35.         {
  36.             byte[] keyBytes = new byte[BlockSize];
  37.             byte[] inputBytes = Encoding.UTF8.GetBytes(keyInput);
  38.  
  39.             for (int i = 0; i < BlockSize; i++)
  40.             {
  41.                 keyBytes[i] = i < inputBytes.Length ? inputBytes[i] : (byte)0;
  42.             }
  43.  
  44.             return keyBytes;
  45.         }
  46.  
  47.         static byte[] GenerateRandomBlock()
  48.         {
  49.             Random rand = new Random();
  50.  
  51.             byte[] block = new byte[BlockSize];
  52.             rand.NextBytes(block);
  53.            
  54.             return block;
  55.         }
  56.  
  57.         static List<byte[]> SplitIntoBlocks(byte[] data)
  58.         {
  59.             List<byte[]> blocks = new List<byte[]>();
  60.  
  61.             for (int i = 0; i < data.Length; i += BlockSize)
  62.             {
  63.                 byte[] block = new byte[BlockSize];
  64.                 int remaining = Math.Min(BlockSize, data.Length - i);
  65.  
  66.                 Array.Copy(data, i, block, 0, remaining);
  67.                 blocks.Add(block);
  68.             }
  69.  
  70.             return blocks;
  71.         }
  72.  
  73.         static byte[] Encrypt(byte[] plaintext, byte[] key, byte[] iv)
  74.         {
  75.             List<byte[]> plainBlocks = SplitIntoBlocks(plaintext);
  76.             List<byte[]> cipherBlocks = new List<byte[]>();
  77.  
  78.             byte[] prevCipherBlock = iv;
  79.  
  80.             foreach (var block in plainBlocks)
  81.             {
  82.                 byte[] xorWithPrev = XOR(block, prevCipherBlock);
  83.                 byte[] cipherBlock = XOR(xorWithPrev, key);
  84.  
  85.                 cipherBlocks.Add(cipherBlock);
  86.                 prevCipherBlock = cipherBlock;
  87.             }
  88.  
  89.             return CombineBlocks(cipherBlocks);
  90.         }
  91.  
  92.         static byte[] Decrypt(byte[] ciphertext, byte[] key, byte[] iv)
  93.         {
  94.             List<byte[]> cipherBlocks = SplitIntoBlocks(ciphertext);
  95.             List<byte[]> decryptedBlocks = new List<byte[]>();
  96.  
  97.             byte[] prevCipherBlock = iv;
  98.  
  99.             foreach (var block in cipherBlocks)
  100.             {
  101.                 byte[] xorWithKey = XOR(block, key);
  102.                 byte[] plainBlock = XOR(xorWithKey, prevCipherBlock);
  103.  
  104.                 decryptedBlocks.Add(plainBlock);
  105.                 prevCipherBlock = block;
  106.             }
  107.  
  108.             return CombineBlocks(decryptedBlocks);
  109.         }
  110.  
  111.         static byte[] XOR(byte[] a, byte[] b)
  112.         {
  113.             byte[] result = new byte[BlockSize];
  114.  
  115.             for (int i = 0; i < BlockSize; i++)
  116.             {
  117.                 result[i] = (byte)(a[i] ^ b[i]);
  118.             }
  119.  
  120.             return result;
  121.         }
  122.  
  123.         static byte[] CombineBlocks(List<byte[]> blocks)
  124.         {
  125.             List<byte> result = new List<byte>();
  126.            
  127.             foreach (var block in blocks)
  128.             {
  129.                 result.AddRange(block);
  130.             }
  131.  
  132.             return result.ToArray();
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement