Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- namespace Gamm
- {
- static class XOR
- {
- public static string Encrypt(string text, string key)
- {
- //Повторяем ключ пока он не станет размером с текст. Для слова весна и ключа ок получится ключ ококо
- while (key.Length < text.Length)
- {
- key += key;
- if (key.Length > text.Length) key = key.Remove(text.Length);
- }
- //Переводим в массив byte
- byte[] textByte = Encoding.Default.GetBytes(text);
- byte[] keyByte = Encoding.Default.GetBytes(key);
- byte[] result = new byte[keyByte.Length];
- //Кодирование с помощью XOR
- for (int i = 0; i < keyByte.Length; i++)
- {
- result[i] = (byte)(textByte[i] ^ keyByte[i]);
- }
- return Encoding.Default.GetString(result);
- }
- }
- internal class Program
- {
- public static void Main(string[] args)
- {
- string text, key, encryptText, decryptText;
- Console.WriteLine("Введите текст для шифрования");
- text = Console.ReadLine();
- Console.WriteLine("Введите ключ для шифрования");
- key = Console.ReadLine();
- encryptText = XOR.Encrypt(text, key);
- Console.WriteLine("Зашифрованный текст: \"" + encryptText + "\"");
- Console.WriteLine("Введите ключ для расшифрования");
- key = Console.ReadLine();
- decryptText = XOR.Encrypt(encryptText, key);
- Console.WriteLine("Расшифрованный текст: \"" + decryptText + "\"");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement