Created
November 24, 2014 18:16
-
-
Save arestifo/c3b449ba9c827fe1cb9e to your computer and use it in GitHub Desktop.
CeasarCipher in C#!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(This is working, with a demo)