private static byte[] s_func(byte[] ina) { ina = Feistel.separateBytes(ina, 6); byte[] outa = new byte[ina.Length / 2]; int halfByte = 0; unchecked { for (int b = 0; b < ina.Length; b++) { byte valByte = ina[b]; int r = 2 * (valByte >> 7 & 0x0001) + (valByte >> 2 & 0x0001); int c = valByte >> 3 & 0x000F; int val = sboxes[b, r, c]; if (b % 2 == 0) { halfByte = val; } else { outa[b / 2] = (byte)(16 * halfByte + val); } } } return(outa); }
public static byte[] decrypt(byte[] input, byte[] K1) { byte[] key_des = Feistel.form_des_key(K1); byte[][] K = Feistel.generateSubKeys(key_des); byte[] dec = Feistel.encrypt64Bloc(input, K, true); return(dec); }
private static byte[] encrypt64Bloc(byte[] bloc, byte[][] subkeys, bool isDecrypt) { byte[] R = new byte[4]; byte[] L = new byte[4]; //tmp = Feistel.permutFunc(bloc, IP); L = Utilities.extractBits(bloc, 0, 32); R = Utilities.extractBits(bloc, 32, 32); for (int i = 0; i < 4; i++) { byte[] tmpR = R; if (isDecrypt) { R = Feistel.f_func(R, subkeys[3 - i]); } else { R = Feistel.f_func(R, subkeys[i]); } R = Utilities.xor(L, R); L = tmpR; } bloc = Utilities.concatBits(R, 32, L, 32); // tmp = Feistel.permutFunc(tmp, invIP); return(bloc); }
public static byte[] encrypt(byte[] input, byte[] K1) { //HttpContext.Current.Trace.Warn("Feistel.encrypt K1 = " + System.Text.Encoding.UTF8.GetString(K1)); byte[] key_des = Feistel.form_des_key(K1); byte[][] K = Feistel.generateSubKeys(key_des); byte[] enc = Feistel.encrypt64Bloc(input, K, false); return(enc); }
private static byte[] f_func(byte[] R, byte[] K) { byte[] tmp; tmp = Feistel.permutFunc(R, expandTbl); tmp = Utilities.xor(tmp, K); tmp = Feistel.s_func(tmp); tmp = Feistel.permutFunc(tmp, P); return(tmp); }
private static byte[][] generateSubKeys(byte[] key) { byte[][] tmp = new byte[16][]; byte[] tmpK = Feistel.permutFunc(key, PC1); byte[] C = Utilities.extractBits(tmpK, 0, PC1.Length / 2); byte[] D = Utilities.extractBits(tmpK, PC1.Length / 2, PC1.Length / 2); for (int i = 0; i < 16; i++) { C = Feistel.rotLeft(C, 28, keyShift[i]); D = Feistel.rotLeft(D, 28, keyShift[i]); byte[] cd = Utilities.concatBits(C, 28, D, 28); tmp[i] = Feistel.permutFunc(cd, PC2); } return(tmp); }
private static byte[] decryptBloc(byte[] ina, byte[] key_array) { byte[] tmp = new byte[ina.Length]; byte[] tmp1 = new byte[ina.Length / 2]; byte[] tmp2 = new byte[ina.Length / 2]; // 2D State Array Declaration byte[][] state = new byte[4][]; int j = 0; while (j < 4) { state[j] = new byte[Nb]; j++; } //Form 2D Array of State for (int i = 0; i < ina.Length; i++) { state[i / 4][i % 4] = ina[i % 4 * 4 + i / 4]; } //Last Round Decrypted state = AESFeistel.AddRoundKey(state, w, Nr); state = AESFeistel.InvShiftRows(state); state = AESFeistel.InvSubBytes(state); //Rounds 6 to 9 Decrypted for (int round = Nr - 1; round >= 6; round--) { state = AESFeistel.AddRoundKey(state, w, round); state = AESFeistel.InvMixColumns(state); state = AESFeistel.InvShiftRows(state); state = AESFeistel.InvSubBytes(state); } //Convert Back to 1D Array for (int i = 0; i < tmp.Length; i++) { tmp[i % 4 * 4 + i / 4] = state[i / 4][i % 4]; } tmp1 = Utilities.extractBits(tmp, 0, 64); tmp2 = Utilities.extractBits(tmp, 64, 64); //FEISTEL CIPHER tmp1 = Feistel.decrypt(tmp1, key_array); tmp2 = Feistel.decrypt(tmp2, key_array); //Join 2 arrays tmp = Utilities.concatBits(tmp2, 64, tmp1, 64); //Covert back to 2D Array for further AES rounds for (int i = 0; i < tmp.Length; i++) { state[i / 4][i % 4] = tmp[i % 4 * 4 + i / 4]; } //Rounds 1 to 5 Decrypted for (int round = 5; round >= 1; round--) { state = AESFeistel.AddRoundKey(state, w, round); state = AESFeistel.InvMixColumns(state); state = AESFeistel.InvShiftRows(state); state = AESFeistel.InvSubBytes(state); } //Initial Round Decryption state = AESFeistel.AddRoundKey(state, w, 0); //Convert back to 1D Array for (int i = 0; i < tmp.Length; i++) { tmp[i % 4 * 4 + i / 4] = state[i / 4][i % 4]; } return(tmp); }