public RunDES() { this.kg = null; this.mg = null; this.f = null; msgPart = null; }
public RunDES(string key, string msg) { // constructor that encrypts or decrypts (single) this.kg = new KeyGenerators(key); this.f = new Functions(); if (Helper.IsHexString(msg)) // if it is a hex string { if (msg.Length % 2 != 0) { Console.WriteLine("WARNING !!! odd number of hex's"); } msg = Helper.AddPaddingToMsg(msg, 16, "0"); // padding of 16 for hex block this.msgPart = Helper.SplitMsgBySize(msg, 16); } else // if not a hex string { msg = Helper.AddPaddingToMsg(msg, 8, "0"); // padding of 8 for character block this.msgPart = Helper.SplitMsgBySize(msg, 8); } //this.mg = new MsgGenerator(msg); // initial permutation happens in the MsgGenerator constructor }
public string RunEncrypt(string key, string msg) { // use this encryption ONLY if the string is in HEX // else use the Constuctor and the RunEncrypt() methods above string output = ""; kg = new KeyGenerators(key); f = new Functions(); bool[] fcn; // result for the f function bool[] lXORfcn; // temporary storage for the Lside and Rside swap msg = Helper.AddPaddingToMsg(msg, 16, "0"); List <string> msgPart = Helper.SplitMsgBySize(msg, 16); for (int i = 0; i < msgPart.Count; i++) // loop for msg { mg = new MsgGenerator(msgPart[i]); for (int j = 0; j <= 15; j++) // loop for rounds { Console.Write("L" + j + ": " + PrintBoolArray(mg.GetLeft())); Console.WriteLine(" R" + j + ": " + PrintBoolArray(mg.GetRight())); fcn = f.fFunction(mg.GetRight(), kg.GetKKeyNumber(j + 1)); Console.WriteLine("fcn: " + PrintBoolArray(fcn)); lXORfcn = mg.L_XOR_f(fcn); mg.SetLeft(mg.GetRight()); mg.SetRight(lXORfcn); } mg.IPinvPermutation(this.mg.RconcatL()); output += mg.GetMsgAsHexString(); } return(output); }