Ejemplo n.º 1
0
 public BinaryNumber(BinaryNumber bn)
 {
     binary = new List<int>();
     binary.AddRange(bn.Value);
 }
Ejemplo n.º 2
0
 public static BinaryNumber operator +(BinaryNumber lhs, BinaryNumber rhs)
 {
     BinaryNumber res = new BinaryNumber();
     res.Add(lhs);
     res.Add(rhs);
     return res;
 }
Ejemplo n.º 3
0
 public void Add(BinaryNumber bn)
 {
     binary.AddRange(bn.Value);
 }
Ejemplo n.º 4
0
 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;
 }
Ejemplo n.º 5
0
 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));
 }
Ejemplo n.º 6
0
 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);
 }
Ejemplo n.º 7
0
 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)));
     }
 }
Ejemplo n.º 8
0
 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);
 }
Ejemplo n.º 9
0
 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;
 }
Ejemplo n.º 10
0
        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();
        }
Ejemplo n.º 11
0
 public DES(TextBox textBox)
 {
     TEST = textBox;
     BinaryNumber key = new BinaryNumber(KeyLength, r);
     GenerateKeys(key);
 }