Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- namespace Vigenère_cipher
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string key = "REDDIT";
- const string message = "TODAYISMYBIRTHDAY";
- string encoded = Encode(key, message);
- }
- private static string Encode(string key, string message)
- {
- var encoded = new StringBuilder(message.Length);
- int keyIndex = 0;
- for (int i = 0; i < message.Length; i++)
- {
- int keyValue = CreateAlphabeticalValueFromChar(key[keyIndex]);
- int messageValue = CreateAlphabeticalValueFromChar(message[i]);
- int encodedValue = 1+ (keyValue + messageValue) % 26;
- encoded.Append(CreateAlphabeticalValueFromInt(encodedValue));
- keyIndex = (keyIndex + 1)%key.Length;
- }
- return encoded.ToString();
- }
- private static int CreateAlphabeticalValueFromChar(char character)
- {
- return Char.ToLower(character) - 'a';
- }
- private static char CreateAlphabeticalValueFromInt(int character)
- {
- return (char) (('a' + character)-1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment