Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp10
- {
- class Program
- {
- public static void Main()
- {
- char[] s = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
- string key = "b";
- for (int i = 0; i < s.Length; i++)
- {
- VigenereEncrypt(s, key);
- Console.WriteLine(s);
- }
- Console.WriteLine();
- for (int i = 0; i < s.Length; i++)
- {
- VigenereDecrypt(s, key);
- Console.WriteLine(s);
- }
- }
- static void VigenereEncrypt(char[] s, string key)
- {
- for (int i = 0; i < s.Length; i++) s[i] = Char.ToUpper(s[i]);
- key = key.ToUpper();
- int j = 0;
- for (int i = 0; i < s.Length; i++)
- {
- if (Char.IsLetter(s[i]))
- {
- s[i] = (char)(s[i] + key[j] - 'A');
- if (s[i] > 'Z')
- {
- s[i] = (char)(s[i] - 'Z' + 'A' - 1);
- }
- }
- j += 1 == key.Length ? 0 : j + 1;
- }
- }
- static void VigenereDecrypt(char[] s, string key)
- {
- for (int i = 0; i < s.Length; i++)
- s[i] = Char.ToUpper(s[i]);
- key = key.ToUpper();
- int j = 0;
- for (int i = 0; i < s.Length; i++)
- {
- if (Char.IsLetter(s[i]))
- {
- s[i] = s[i] >= key[j] ? (char)(s[i] - key[j] + 'A') : (char)('A' + ('Z' - key[j] + s[i] - 'A') + 1);
- }
- j +=1 == key.Length ? 0 : j + 1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement