#region
using System;
#endregion
namespace Vigenere
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(wasEncryptVigenere("It is a good day!", "was"));
Console.WriteLine(wasDecryptVigenere("Et ao a ykov zaq!", "was"));
Console.ReadKey();
}
//////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Permutes an array in reverse a given number of times.
///
/// the array type
/// the array
/// the number of times to permute
/// the array with the elements permuted
private static T[] wasReversePermuteArrayElements(T[] input, int times)
{
if (times.Equals(0)) return input;
T[] slice = new T[input.Length];
Array.Copy(input, 1, slice, 0, input.Length - 1);
Array.Copy(input, 0, slice, input.Length - 1, 1);
return wasReversePermuteArrayElements(slice, --times);
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Expand the VIGENRE key to the length of the input.
///
/// the input to expand to
/// the key to expand
/// the expanded key
private static string wasVigenereExpandKey(string input, string enc_key)
{
string exp_key = "";
int i = 0, j = 0;
do
{
char p = input[i];
if (!char.IsLetter(p))
{
exp_key += p;
++i;
continue;
}
int m = j%enc_key.Length;
exp_key += enc_key[m];
++j;
++i;
} while (i < input.Length);
return exp_key;
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Encrypt using VIGENERE.
///
/// the input to encrypt
/// the key to encrypt with
/// the encrypted input
private static string wasEncryptVigenere(string input, string enc_key)
{
char[] a =
{
'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'
};
enc_key = wasVigenereExpandKey(input, enc_key);
string result = "";
int i = 0;
do
{
char p = input[i];
if (!char.IsLetter(p))
{
result += p;
++i;
continue;
}
char q =
wasReversePermuteArrayElements(a, Array.IndexOf(a, enc_key[i]))[
Array.IndexOf(a, char.ToLowerInvariant(p))];
if (char.IsUpper(p))
{
q = char.ToUpperInvariant(q);
}
result += q;
++i;
} while (i < input.Length);
return result;
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Decrypt using VIGENERE.
///
/// the input to decrypt
/// the key to decrypt with
/// the decrypted input
private static string wasDecryptVigenere(string input, string enc_key)
{
char[] a =
{
'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'
};
enc_key = wasVigenereExpandKey(input, enc_key);
string result = "";
int i = 0;
do
{
char p = input[i];
if (!char.IsLetter(p))
{
result += p;
++i;
continue;
}
char q =
a[
Array.IndexOf(wasReversePermuteArrayElements(a, Array.IndexOf(a, enc_key[i])),
char.ToLowerInvariant(p))];
if (char.IsUpper(p))
{
q = char.ToUpperInvariant(q);
}
result += q;
++i;
} while (i < input.Length);
return result;
}
}
}