using System; using System.Linq; using System.Collections.Generic; using System.Text; namespace AniNIX.Crypto { public abstract class Cipher { //Cipher description to be set by each individual cipher. public abstract String Description(); public abstract String Command(); /// /// Create a new Cipher /// /// Cipher public Cipher (Workbench w) { string helpString = String.Format("{0} -- {1}\n",this.Command().PadRight(16),this.Description().Split('\n')[0]); if (!w.HelpText.ToString().Contains(helpString)) { w.HelpText.Append(helpString); } if (!w.SwitchCases.ContainsKey(this.Command())) { w.SwitchCases.Add(this.Command(),this); } } public Cipher() { } /// /// We should be able to act on a workspace and command line. Most ciphers will use the same syntax. Those that don't can override. /// /// The current version of the text being worked on. /// The command sequence. /// The updated version of the workSpace public virtual String RunCommand(String workSpace,String inputText,String[] line) { if (workSpace == null || line == null || line.Length < 2) { Console.Error.WriteLine("Malformed request."); return workSpace; } switch (line[1]) { case "encrypt": return Encrypt(workSpace,inputText,line); case "decrypt": return Decrypt(workSpace,inputText,line); case "help": case "": GetHelp(); return workSpace; default: Console.Error.WriteLine("Invalid command. Type help for more."); return workSpace; } } /// /// Show the helptext for this cipher. By default, most ciphers will only have encrypt, decrypt, and help functions. /// /// This is the incoming line and we use it to get the cipher name public virtual void GetHelp() { String command = Command(); Console.WriteLine(String.Format("Help for the {0} cipher suite.\n{1}\n",command,Description())); Console.WriteLine("Usage:"); Console.Write(command); Console.WriteLine(" encrypt key -- encrypt with the key"); Console.Write(command); Console.WriteLine(" decrypt key -- decrypt with the key"); Console.Write(command); Console.WriteLine(" help -- show this helptext."); } /// /// All ciphers must be able to encrypt a string. /// /// The current version of the text being worked on. /// The command sequence. /// The updated version of the workSpace public abstract String Encrypt(string workSpace,String ciphetText,String[] line); /// /// All ciphers must be able to decrypt a string. /// /// The current version of the text being worked on. /// The command sequence. /// The updated version of the workSpace public abstract String Decrypt(String workSpace,String inputText,String[] line); } }