Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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