public static string Decrypt(string keyHex, string cipher) { var subKeys = SplitKey(keyHex); var bitBlocks = SplitToBitBlocks(cipher); var decryptedBits = new List <int[]>(); for (int i = 0; i < bitBlocks.Count; i++) { DES.Encrypt(subKeys.Item3, bitBlocks[i]); int[] decrypted3 = DES.Decrypt(subKeys.Item3, bitBlocks[i]); int[] decrypted2 = DES.Encrypt(subKeys.Item2, decrypted3); int[] decrypted1 = DES.Decrypt(subKeys.Item1, decrypted2); decryptedBits.Add(decrypted1); } // Remove padding int paddingValue = decryptedBits.Last().First(); int lastIndex = decryptedBits.Count - 1; decryptedBits.RemoveAt(lastIndex--); // Remove last additional block contains only padding value decryptedBits[lastIndex] = decryptedBits[lastIndex].TrimEnd(paddingValue); string decryptedHex = HexConverter.BitsToHex(decryptedBits); return(HexConverter.HexStrToStr(decryptedHex)); }
public static string Decrypt(string keyHex, string cipherHex) { int[] cipherBits = GetBitsFromHexStr(cipherHex); int[] keyBits = GetBitsFromHexStr(keyHex); int[] decryptedBits = Permute(cipherBits, keyBits, true); return(HexConverter.BitsToHex(decryptedBits)); }
public static string Encrypt(string keyHex, string strHexToEncrypt) { var subKeys = SplitKey(keyHex); var bitBlocks = SplitToBitBlocksUsingTBC(strHexToEncrypt); var encryptedBits = new List <int[]>(); for (int i = 0; i < bitBlocks.Count; i++) { int[] cipher1 = DES.Encrypt(subKeys.Item1, bitBlocks[i]); int[] cipher2 = DES.Decrypt(subKeys.Item2, cipher1); int[] cipher3 = DES.Encrypt(subKeys.Item3, cipher2); encryptedBits.Add(cipher3); } return(HexConverter.BitsToHex(encryptedBits)); }
public string Encrypt(string keyHex, string strHexToEncrypt) { // inputBits will store the 64 bits of the input as a an int array of // size 64. This program uses int arrays to store bits, for the sake // of simplicity. For efficient programming, use long data type. But // it increases program complexity which is unnecessary for this // context. int[] inputBits = GetBitsFromHexStr(strHexToEncrypt); int[] keyBits = GetBitsFromHexStr(keyHex); // permute(int[] inputBits, int[] keyBits, boolean isDecrypt) // method is used here. This allows encryption and decryption to be // done in the same method, reducing code. int[] cipher = Permute(inputBits, keyBits, false); return(HexConverter.BitsToHex(cipher)); }