Advertisement
Artem_Chepurov

Untitled

Nov 19th, 2022
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace Gamm
  5. {
  6.     static class XOR
  7.     {
  8.         public static string Encrypt(string text, string key)
  9.         {
  10.             //Повторяем ключ пока он не станет размером с текст. Для слова весна и ключа ок получится ключ ококо                    
  11.             while (key.Length < text.Length)
  12.             {
  13.                 key += key;
  14.                 if (key.Length > text.Length) key = key.Remove(text.Length);
  15.             }
  16.             //Переводим в массив byte
  17.             byte[] textByte = Encoding.Default.GetBytes(text);
  18.             byte[] keyByte = Encoding.Default.GetBytes(key);
  19.            
  20.             byte[] result = new byte[keyByte.Length];
  21.             //Кодирование с помощью XOR
  22.             for (int i = 0; i < keyByte.Length; i++)
  23.             {
  24.                 result[i] = (byte)(textByte[i] ^ keyByte[i]);
  25.             }
  26.  
  27.             return Encoding.Default.GetString(result);
  28.         }
  29.     }
  30.     internal class Program
  31.     {
  32.         public static void Main(string[] args)
  33.         {
  34.             string text, key, encryptText, decryptText;
  35.             Console.WriteLine("Введите текст для шифрования");
  36.             text = Console.ReadLine();
  37.             Console.WriteLine("Введите ключ для шифрования");
  38.             key = Console.ReadLine();
  39.             encryptText = XOR.Encrypt(text, key);
  40.             Console.WriteLine("Зашифрованный текст: \"" + encryptText + "\"");
  41.             Console.WriteLine("Введите ключ для расшифрования");
  42.             key = Console.ReadLine();
  43.             decryptText = XOR.Encrypt(encryptText, key);
  44.             Console.WriteLine("Расшифрованный текст: \"" + decryptText + "\"");
  45.            
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement