Skip to content

Instantly share code, notes, and snippets.

@0x6d61
Created May 16, 2017 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x6d61/64a77e4734b413b08bafddbfb0f61757 to your computer and use it in GitHub Desktop.
Save 0x6d61/64a77e4734b413b08bafddbfb0f61757 to your computer and use it in GitHub Desktop.
C# Simple substitution cipher
using System;
using System.Collections.Generic;
class DecodeCaesarTool {
public static void Main(string[] args) {
if(args.Length == 0 ) {
Console.WriteLine("DecodeCaesar.exe <cipher text>");
}else{
for(int i = 1;i<26;i++) {
List<char> r = DecodeCaesar(args[0],i);
Console.WriteLine("seed:{0},{1}",i,new String(r.ToArray()));
}
}
}
private static List<char> DecodeCaesar(string chiher,int seed) {
char[] alpha = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
List<char> list = new List<char>(alpha);
List<char> result = new List<char>();
char[] text = chiher.ToLower().ToCharArray();
for(int i = 0;i<text.Length;i++) {
if(!Char.IsNumber(text[i])) {
if(25 >= (list.IndexOf(text[i]) + seed)) {
result.Add(list[list.IndexOf(text[i]) + seed]);
}else{
result.Add(list[Math.Abs(seed - list.IndexOf(text[i]))]);
}
}else{
continue;
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment