public Matrix AddRoundKey(Matrix state, Keys key, int Round) { // AddRoundKey if (Round > key.RoundKeys.Count - 1) { throw new IndexOutOfRangeException( "The round key is must between 0 and 10 in 128 bit AES."); } return(MatrixMultiplication.XOR(state, key.RoundKeys[Round])); }
public Keys KeyExpansion(Keys key, bool IsReverse) { for (int i = 4; i < key.RoundKeys.Count * 4; i++) { string[] Wi_1 = key.RoundKeys[(i - 1) / 4].getWord((i - 1) % 4); Matrix mat_Wi_1 = new Matrix(4, 1); mat_Wi_1.setWord(Wi_1, 0); if (i % 4 == 0) { Wi_1 = this.RotWord(Wi_1); mat_Wi_1.setWord(Wi_1, 0); mat_Wi_1 = this.SubBytes(mat_Wi_1, false); mat_Wi_1 = MatrixMultiplication.XOR(mat_Wi_1, TransformTables.Rcon[(i - 1) / 4]); } Matrix Wi_4 = new Matrix(4, 1); Wi_4.setWord(key.RoundKeys[(i - 4) / 4].getWord((i - 4) % 4), 0); Matrix temp = MatrixMultiplication.XOR(mat_Wi_1, Wi_4); string[] Wi = temp.getWord(0); key.RoundKeys[i / 4].setWord(Wi, i % 4); } return(key); }