public BinaryNumber(BinaryNumber bn) { binary = new List<int>(); binary.AddRange(bn.Value); }
public static BinaryNumber operator +(BinaryNumber lhs, BinaryNumber rhs) { BinaryNumber res = new BinaryNumber(); res.Add(lhs); res.Add(rhs); return res; }
public void Add(BinaryNumber bn) { binary.AddRange(bn.Value); }
public List<BinaryNumber> Divide(int n) { List<BinaryNumber> parts = new List<BinaryNumber>(); BinaryNumber part = new BinaryNumber(); for (int i = 0; i < Length; i++) { part.Add(binary[i]); if ((i + 1)%(Length/n) == 0) { parts.Add(new BinaryNumber(part)); part.Clear(); } } return parts; }
private BinaryNumber Process(BinaryNumber input) { List<BinaryNumber> ipOut = Block(IP, input).Divide(2); BinaryNumber left = new BinaryNumber(ipOut[0]); BinaryNumber right = new BinaryNumber(ipOut[1]); for (int i = 0; i < DesSteps; i++) { BinaryNumber fOut = Function(new BinaryNumber(right), i); BinaryNumber rTemp = new BinaryNumber(right); right = new BinaryNumber(fOut^left); left = new BinaryNumber(rTemp); } return Block(IP_1, new BinaryNumber(right + left)); }
private BinaryNumber SBlock(BinaryNumber input, int id) { BinaryNumber row = new BinaryNumber(); row.Add(input[0]); row.Add(input.Last); BinaryNumber col = new BinaryNumber(); for(int i = 1; i < input.Length - 1; i++) col.Add(input[i]); return ToBinaryNumber(S[id][row.ToInt()][col.ToInt()], 4); }
private void GenerateKeys(BinaryNumber key) { keys = new List<BinaryNumber>(); BinaryNumber cKey = Block(LeftPC1, key); BinaryNumber dKey = Block(RightPC1, key); for (int i = 0; i < DesSteps; i++) { cKey.ShiftLeft(Rotation[i]); dKey.ShiftLeft(Rotation[i]); keys.Add(Block(PC2, new BinaryNumber(cKey + dKey))); } }
private BinaryNumber Function(BinaryNumber input, int stepId) { BinaryNumber eOut = Block(E, input); List<BinaryNumber> sInputs = (eOut ^ GetKey(stepId)).Divide(8); BinaryNumber sOut = new BinaryNumber(); for(int i = 0; i < sInputs.Count; i++) { sOut.Add(SBlock(sInputs[i], i)); } return Block(P, sOut); }
private BinaryNumber Block(int[][] block, BinaryNumber input) { BinaryNumber output = new BinaryNumber(); for (int i = 0; i < block.Length; i++) { for (int j = 0; j < block[0].Length; j++) { output.Add(input[block[i][j] - 1]); } } return output; }
public string Encript(string message) { mode = DESMode.Encription; byte[] byteMessage = PreprocessMessage(message); List<BinaryNumber> ipInput = new List<BinaryNumber>(); BinaryNumber temp = new BinaryNumber(); for (int i = 0; i < byteMessage.Length; i++) { temp.Add(ToBinaryNumber(byteMessage[i], 8)); if ((i + 1)%8 == 0) { ipInput.Add(new BinaryNumber(temp)); temp.Clear(); } } encrypted = new List<BinaryNumber>(); for (int i = 0; i < ipInput.Count; i++) { encrypted.Add(Process(ipInput[i])); } StringBuilder sb = new StringBuilder(); foreach (var binaryNumber in encrypted) { sb.Append(binaryNumber); } return sb.ToString(); }
public DES(TextBox textBox) { TEST = textBox; BinaryNumber key = new BinaryNumber(KeyLength, r); GenerateKeys(key); }