93 lines
4.1 KiB
Plaintext
93 lines
4.1 KiB
Plaintext
using System;
|
|
using System.Linq;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Collections.Generic;
|
|
|
|
namespace AniNIX.Crypto {
|
|
public class Vigenere : Cipher {
|
|
|
|
public override String Description() { return "The basic Vigenere cipher\nKey format is any word to use for the passphrase"; }
|
|
public override String Command() { return "vig"; }
|
|
|
|
public Vigenere(Workbench w) : base (w) {}
|
|
|
|
/// <summary>
|
|
/// Encrypt a string with the cipher. Encryption and decryption work like a substitution but is rotated with a key.
|
|
/// </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) {
|
|
if (line == null || line.Length != 3) {
|
|
Console.Error.WriteLine("Malformed!");
|
|
return workSpace;
|
|
}
|
|
char[] changed = workSpace.ToCharArray();
|
|
try {
|
|
// line[2] is the key
|
|
int index = 0;
|
|
for (int i =0; i < line[2].Length; i++) {
|
|
if (!Char.IsLetter(line[2][i])) {
|
|
Console.Error.WriteLine("Keys must be only letters.");
|
|
return workSpace;
|
|
}
|
|
}
|
|
// For each letter in the workspace, substitute with an offset given by the key. Rotate which letter in the key is used.
|
|
for (int i = 0; i < changed.Length; i++) {
|
|
if (Char.IsLetter(changed[i])) {
|
|
int baseC = (Char.IsUpper(changed[i])) ? (int)'A' : (int)'a';
|
|
int modC = (int)changed[i] - baseC;
|
|
int baseK = (Char.IsUpper(line[2][index])) ? (int)'A' : (int)'a';
|
|
int modK = (int)line[2][index] - baseK;
|
|
changed[i] = (char)(((modC+modK)%26)+baseC);
|
|
index = (index+1)%(line[2].Length);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
Console.Error.WriteLine(String.Format("Failed!\n{0}",e.Message));
|
|
return workSpace;
|
|
}
|
|
return new String(changed);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decrypt a string with the cipher. Like the encryption method, we decrypt by removing the offset as given by the key.
|
|
/// </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 decrypted string</returns>
|
|
public override String Decrypt(String workSpace,String inputText,String[] line) {
|
|
if (line == null || line.Length != 3) {
|
|
Console.Error.WriteLine("Malformed!");
|
|
return workSpace;
|
|
}
|
|
char[] changed = workSpace.ToCharArray();
|
|
try {
|
|
// line[2] is the key
|
|
int index=0;
|
|
for (int i = 0; i < changed.Length; i++) {
|
|
if (Char.IsLetter(changed[i])) {
|
|
int baseC = (Char.IsUpper(changed[i])) ? (int)'A' : (int)'a';
|
|
int modC = (int)changed[i] - baseC;
|
|
int baseK = (Char.IsUpper(line[2][index])) ? (int)'A' : (int)'a';
|
|
int modK = (int)line[2][index] - baseK;
|
|
int modResult = (modC - modK)%26;
|
|
modResult = (modResult < 0) ? modResult+26 : modResult; // In case modResult is negative, add 26 back
|
|
changed[i] = (char)(modResult+baseC);
|
|
index = (index+1)%(line[2].Length);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
Console.Error.WriteLine(String.Format("Failed!\n{0}",e.Message));
|
|
return workSpace;
|
|
}
|
|
return new String(changed);
|
|
|
|
}
|
|
|
|
}
|
|
}
|