public static sbyte[] decryptBloc(sbyte[] input) { sbyte[] tmp = new sbyte[input.Length]; sbyte[][] state = RectangularArrays.ReturnRectangularSbyteArray(4, Nb); for (int i = 0; i < input.Length; i++) { state[i / 4][i % 4] = input[i % 4 * 4 + i / 4]; } state = AddRoundKey(state, w, Nr); FileHelper.GetDescription("start round initialisation from " + (Nr - 1) + " to " + 1); for (int round = Nr - 1; round >= 1; round--) { FileHelper.GetDescription("round: {0}", round.ToString()); state = InvSubBytes(state); FileHelper.GetDescription("{0,18}", "InvSubBytes state"); FileHelper.GetMatrixState(state); state = InvShiftRows(state); FileHelper.GetDescription("{0, 19}", "InvShiftRows state"); FileHelper.GetMatrixState(state); state = AddRoundKey(state, w, round); FileHelper.GetDescription("{0, 21}", "AddRoundKey state"); FileHelper.GetMatrixState(state); state = InvMixColumns(state); FileHelper.GetDescription("{0, 20}", "InvMixColumns state"); FileHelper.GetMatrixState(state); } FileHelper.GetDescription("round: {0}", "0"); state = InvSubBytes(state); FileHelper.GetMatrixState(state); state = InvShiftRows(state); FileHelper.GetDescription("{0, 19}", "InvShiftRows state"); FileHelper.GetMatrixState(state); state = AddRoundKey(state, w, 0); FileHelper.GetDescription("{0, 21}", "AddRoundKey state"); FileHelper.GetMatrixState(state); for (int i = 0; i < tmp.Length; i++) { tmp[i % 4 * 4 + i / 4] = state[i / 4][i % 4]; } return(tmp); }
public static sbyte[][] SubBytes(sbyte[][] state) { sbyte[][] tmp = RectangularArrays.ReturnRectangularSbyteArray(state.Length, state[0].Length); for (int row = 0; row < 4; row++) { for (int col = 0; col < Nb; col++) { tmp[row][col] = unchecked ((sbyte)(sbox[(state[row][col] & 0x000000ff)] & 0xff)); } } return(tmp); }
public static sbyte[][] AddRoundKey(sbyte[][] state, sbyte[][] w, int round) { sbyte[][] tmp = RectangularArrays.ReturnRectangularSbyteArray(state.Length, state[0].Length); for (int c = 0; c < Nb; c++) { for (int l = 0; l < 4; l++) { tmp[l][c] = (sbyte)(state[l][c] ^ w[round * Nb + c][l]); } } return(tmp); }
private static sbyte[][] generateSubkeys(sbyte[] key) { sbyte[][] tmp = RectangularArrays.ReturnRectangularSbyteArray(Nb * (Nr + 1), 4); int i = 0; while (i < Nk) { tmp[i][0] = key[i * 4]; tmp[i][1] = key[i * 4 + 1]; tmp[i][2] = key[i * 4 + 2]; tmp[i][3] = key[i * 4 + 3]; i++; } i = Nk; while (i < Nb * (Nr + 1)) { sbyte[] temp = new sbyte[4]; for (int k = 0; k < 4; k++) { temp[k] = tmp[i - 1][k]; } if (i % Nk == 0) { temp = SubWord(rotateWord(temp)); temp[0] = (sbyte)(temp[0] ^ (Rcon[i / Nk] & 0xff)); } else if (Nk > 6 && i % Nk == 4) { temp = SubWord(temp); } tmp[i] = xor_func(tmp[i - Nk], temp); i++; } return(tmp); }