public int[] Decrypt(int[] input, int[] key) { //Need to be implemented //Decryption is simply the inverse of encryption, follwing the same steps as above, but reversing the order in which the subkeys are applied. //Taking Input class to store the message DES_implementation3.Input inputClass = new DES_implementation3.Input(); //input after initial permutation; inputClass.getInput(initialPermutation(input)); //Getting all the subkeys int[,] subkeys = generateKeys(key, 2); //There are only 2 round in this encryption so //making 2 rounds of for() for (int i = 2; i > 0; i--) { //The function for each round should be R[i+1] = des.xor(L[i], des.F(R[i], des.getSubKet(subkeys, i+1)); //Then L[1] = R[0]; int[] tempR0 = inputClass.R; inputClass.R = xor(inputClass.L, F(inputClass.R, getSubKey(subkeys, i))); inputClass.L = tempR0; } //R2 goes to Left inputClass.R.CopyTo(inputClass.input, 0); //L2 goes to Right inputClass.L.CopyTo(inputClass.input, inputClass.L.Length); //Reverse IP inputClass.getInput(reverseInitialPermutation(inputClass.input)); return inputClass.input; }
/// <summary> /// This method will need: /// 1. a codeblock of 16bits /// 2. 16bits key /// /// </summary> /// <returns>Encrypted binary 16bits message </returns> public int[] Encrypt(int[] input, int[] key) { //Taking Input class to store the message DES_implementation3.Input inputClass = new DES_implementation3.Input(); //input after initial permutation; inputClass.getInput(initialPermutation(input)); //Getting all the subkeys int[,] subkeys = generateKeys(key, 2); //There are only 2 round in this encryption so //making 2 rounds of for() for (int i = 0; i < 2; i++) { //The function for each round should be R[i+1] = des.xor(L[i], des.F(R[i], des.getSubKet(subkeys, i+1)); //Then L[1] = R[0]; int[] tempR0 = inputClass.R; inputClass.R = xor(inputClass.L, F(inputClass.R, getSubKey(subkeys, i + 1))); inputClass.L = tempR0; } //R2 goes to Left inputClass.R.CopyTo(inputClass.input, 0); //L2 goes to Right inputClass.L.CopyTo(inputClass.input, inputClass.L.Length); //Reverse IP inputClass.getInput(reverseInitialPermutation(inputClass.input)); return inputClass.input; }
public void testFullEncryption2() { //Input "JZ" int[] input = new int[] { 0,1,0,0,1,0,1,0, 0,1,0,1,1,0,1,0 }; DES_implementation3.Input inputclass = new DES_implementation3.Input(); //input after initial permutation; inputclass.getInput(des.initialPermutation(input)); //Random key int[] key = new int[] { 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1 }; //Getting all the subkeys int[,] subkeys = des.generateKeys(key, 2); //There are only 2 round in this encryption so //making 2 rounds of for() for (int i = 0; i < 2; i++) { //By the formula F(R[i], K[i+1] //des.F(r, des.getSubKey(subkeys, )); //The function for each round should be R[i+1] = des.xor(L[i], des.F(R[i], des.getSubKet(subkeys, i+1)); //Then L[1] = R[0]; int[] subkey = des.getSubKey(subkeys, i + 1); int[] funkcija = des.F(inputclass.R, subkey); int[] tempR0 = inputclass.R; inputclass.R = des.xor(inputclass.L, funkcija); inputclass.L = tempR0; } //before reverse initial permutation int[] resultarrayR2 = new int[] { 1, 1, 1, 0, 1, 1, 0, 1 }; //R2 goes to Left inputclass.R.CopyTo(input, 0); //L2 goes to Right inputclass.L.CopyTo(input, inputclass.L.Length); //Reverse IP inputclass.getInput(des.reverseInitialPermutation(input)); //after encryption int[] resultarray2 = new int[]{ 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0 }; CollectionAssert.AreEqual(resultarray2, inputclass.input); }