2016-08-04 12:10:50 -05:00
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace AniNIX.Crypto {
public class Caesarian : Cipher {
2016-09-21 19:10:34 -05:00
public override String Description() { return "Caesarian cipher suite\nKey format is a numeric shift."; }
public override String Command() { return "caesar"; }
public Caesarian(Workbench w) : base (w) {}
2016-08-04 12:10:50 -05:00
2016-09-21 19:10:34 -05:00
/// <summary>
/// Decode the user's input and relate to an existing function.
/// </summary>
/// <param name="workSpace">The working copy of the cipher</param>
/// <param name="inputText">The original cipher</param>
/// <param name="line">The user input</param>
/// <returns>The modified workspace string</returns>
public override String RunCommand(String workSpace,String inputText,String[] line) {
2016-08-04 12:10:50 -05:00
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 "brute":
BruteForce(workSpace);
return workSpace;
default:
GetHelp();
return workSpace;
}
}
2016-09-21 19:10:34 -05:00
/// <summary>
/// Show this help text
/// </summary>
public override void GetHelp() {
2016-08-04 12:10:50 -05:00
Console.WriteLine(String.Format("Help for the {0} cipher suite.\n{1}\n",Command(),Description()));
Console.WriteLine("encrypt key -- encrypt with the key\ndecrypt key -- decrypt with the key\nbrute -- brute-force for keys\nhelp -- show this helptext.");
}
2016-09-21 19:10:34 -05:00
/// <summary>
/// Encrypt a string with the cipher
/// </summary>
/// <param name="workSpace">The working copy of the cipher</param>
/// <param name="inputText">The original cipher</param>
/// <param name="line">The user input</param>
/// <returns>The encrypted string</returns>
public override String Encrypt(String workSpace,String inputText,String[] line) {
// Validate input
2016-08-04 12:10:50 -05:00
if (line.Length != 3) {
Console.Error.WriteLine("Bad formatting");
return workSpace;
}
int rotation = 0;
try {
rotation = Int32.Parse(line[2]);
} catch (Exception e) {
Console.Error.WriteLine("Error in parsing rotation value -- should be numeric.");
Console.Error.WriteLine(e.Message);
return workSpace;
}
char[] modified = workSpace.ToCharArray();
2016-09-21 19:10:34 -05:00
//For each character in the workSpace, rotate it by the offset given.
2016-08-04 12:10:50 -05:00
for (int i = 0; i < modified.Length; i++) {
if (Char.IsLetter(modified[i])) {
2016-09-21 19:10:34 -05:00
int baseC;
int modC;
// Have to account for case.
if (modified[i] < 'a') {
baseC = (int)'A';
modC = (int)modified[i] - (int)'A';
} else {
baseC = (int)'a';
modC = (int)modified[i] - (int)'a';
}
modC = (modC + rotation)%26;
modified[i] = (char)(baseC+modC);
2016-08-04 12:10:50 -05:00
}
}
return new String(modified);
}
2016-09-21 19:10:34 -05:00
// This is a dummy override.
public override String Decrypt(String workSpace,String inputText,String[] line) {
return Encrypt(workSpace,inputText,line);
}
2016-08-04 12:10:50 -05:00
2016-09-21 19:10:34 -05:00
/// <summary>
/// Try rotating through all 26 possible combinations.
/// </summary>
2016-08-04 12:10:50 -05:00
public void BruteForce(String workSpace) {
String[] line = new String[3];
line[0] = "rot";
line[1] = "encrypt";
for (int i=0; i<26; i++) {
line[2]=i.ToString();
Console.Write(String.Format("{0,2}: {1}",i,Encrypt(workSpace,null,line)));
}
}
}
}