/// <summary> /// Converts a BitBlock into an array of bytes. /// The number of items in the BitBlock must be divisible by 8. /// </summary> /// <param name="input"></param> /// <returns></returns> public static byte[] ConvertToByteArray(BitBlock input) { int size = input.Length; if (size % 8 != 0) { throw new Exception("Input lenght must be divisible and greater by/than 8"); } byte[] byteArray = new byte[size / 8]; int b = 0; BitBlock bb = new BitBlock(8); for (int i = 0; i < size; i++) { bb[i % 8] = input[i]; if ((i + 1) % 8 == 0) { byte _byte = BitBlock.ConvertBinaryToDecimal(bb); byteArray[b++] = _byte; } } return(byteArray); }
private BitBlock S(BitBlock input, int sreferenceTableNumber) { BitBlock outerBits = new BitBlock(2); BitBlock middleBits = new BitBlock(4); outerBits[0] = input[0]; outerBits[1] = input[5]; middleBits[0] = input[1]; middleBits[1] = input[2]; middleBits[2] = input[3]; middleBits[3] = input[4]; int middleNumber = BitBlock.ConvertBinaryToDecimal(middleBits); int outerNumber = BitBlock.ConvertBinaryToDecimal(outerBits); int tableValue = 0; switch (sreferenceTableNumber) { case 1: tableValue = tables.SBox1Table[outerNumber, middleNumber]; break; case 2: tableValue = tables.SBox2Table[outerNumber, middleNumber]; break; case 3: tableValue = tables.SBox3Table[outerNumber, middleNumber]; break; case 4: tableValue = tables.SBox4Table[outerNumber, middleNumber]; break; case 5: tableValue = tables.SBox5Table[outerNumber, middleNumber]; break; case 6: tableValue = tables.SBox6Table[outerNumber, middleNumber]; break; case 7: tableValue = tables.SBox7Table[outerNumber, middleNumber]; break; case 8: tableValue = tables.SBox8Table[outerNumber, middleNumber]; break; } BitBlock bitBlock = BitBlock.ConvertDecimalToBinary(tableValue, 4); return(bitBlock); }