Guest User

Vigenère_cipher encode

a guest
Mar 6th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace Vigenère_cipher
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             const string key = "REDDIT";
  11.             const string message = "TODAYISMYBIRTHDAY";
  12.  
  13.             string encoded = Encode(key, message);
  14.         }
  15.  
  16.         private static string Encode(string key, string message)
  17.         {
  18.             var encoded = new StringBuilder(message.Length);
  19.  
  20.             int keyIndex = 0;
  21.  
  22.             for (int i = 0; i < message.Length; i++)
  23.             {
  24.                 int keyValue = CreateAlphabeticalValueFromChar(key[keyIndex]);
  25.                 int messageValue = CreateAlphabeticalValueFromChar(message[i]);
  26.  
  27.                 int encodedValue =  1+ (keyValue + messageValue) % 26;
  28.  
  29.                 encoded.Append(CreateAlphabeticalValueFromInt(encodedValue));
  30.  
  31.                 keyIndex = (keyIndex + 1)%key.Length;
  32.             }
  33.                
  34.             return encoded.ToString();
  35.         }
  36.  
  37.         private static int CreateAlphabeticalValueFromChar(char character)
  38.         {
  39.             return Char.ToLower(character) - 'a';
  40.         }
  41.  
  42.         private static char CreateAlphabeticalValueFromInt(int character)
  43.         {
  44.             return (char) (('a' +  character)-1);
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment