private static BitArray ApplyKey(BitArray input, BitArray key) { var e = input.ApplyPermutation(E_SELECT); var xor = e.Xor(key); var output = new BitArray(32); for (int i = 0; i < 8; i++) { var bits = xor.SubArray(i * 6, 6); output.Insert(i * 4, SBoxEnumerator.Enumerate(bits, i)); } return(output.ApplyPermutation(P)); }
private static BitArray EncryptDecryptBlock(BitArray block, List <BitArray> keys) { var ip = block.ApplyPermutation(IP); int mid = ip.Count / 2; var left = ip.SubArray(0, mid); var right = ip.SubArray(mid, mid); for (int i = 0; i < 16; i++) { var temp = left; left = right; right = temp.Xor(ApplyKey(right, keys[i])); } var concat = right.Concat(left); return(concat.ApplyPermutation(IP_INVERSE)); }
public static List <BitArray> GetKeyPermutations(BitArray key) { var pc1 = key.ApplyPermutation(PC_1); int mid = pc1.Count / 2; var left = pc1.SubArray(0, mid); var right = pc1.SubArray(mid, mid); var keys = new List <BitArray>(); for (int i = 0; i < 16; i++) { left = left.ShiftLeft(ShiftCounts[i]); right = right.ShiftLeft(ShiftCounts[i]); var concat = left.Concat(right); var pc2 = concat.ApplyPermutation(PC_2); keys.Add(pc2); } return(keys); }