Skip to content

Instantly share code, notes, and snippets.

@arestifo
Created November 24, 2014 18:16
Show Gist options
  • Select an option

  • Save arestifo/c3b449ba9c827fe1cb9e to your computer and use it in GitHub Desktop.

Select an option

Save arestifo/c3b449ba9c827fe1cb9e to your computer and use it in GitHub Desktop.
CeasarCipher in C#!
using System;
using System.Collections.Generic;
class Program
{
static readonly char[] alpha = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
static void Main(string[] args)
{
Console.WriteLine(CeasarEncrypt("yz", 1));
Console.ReadKey();
}
public static string CeasarEncrypt(string str, int rot)
{
char[] inStr = str.ToCharArray();
char[] outC = new char[inStr.Length];
Dictionary<char, int> dict = new Dictionary<char, int>();
for (int x = 0; x < alpha.Length; x++) { dict.Add(alpha[x], x); }
for (int i = 0; i < inStr.Length; i++)
{
int l;
dict.TryGetValue(inStr[i], out l);
outC[i] = alpha[(l + rot) % alpha.Length];
}
return new string(outC);
}
}
@arestifo
Copy link
Author

(This is working, with a demo)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment