Matrix operation
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 907 Accepted Submission (s): 384
Problem Description M_0 is interested in cryptography. Recently he learned the Advanced Encryption Standard (AES). AES operates on a 4x4 column-major order matrix of bytes and he found it's there is a step called the Mixcolumns step I n the AES.
In the Mixcolumns step, there was a state matrix, the four bytes of each column of the state being combined using an INVERTIB Le linear transformation. The Mixcolumns function takes four bytes as input and outputs four bytes, where each input byte affects all four output by Tes. Together with Shiftrows, Mixcolumns provides diffusion in the cipher.
During This operation, each of the multiplied by the known matrix, the-the-bit key is:
The addition operation is defined As:xor.
The multiplication operation is defined as:
Multiplication by 1 means no
Multiplication by 2 means shifting
Multiplication by 3 means shifting to the left and then performing XOR with the initial unshifted value.
Notice:after each shifting, a conditional XOR with 0x1B should being performed if the shifted value is larger than 0xFF.
Inputthere is several cases.
The first line was an integer t (T <= 20000), indicating the test cases.
Then are the state matrix, with each case followed by four lines, each of the line contains four bytes, and separated by spaces.
Outputfor each case, output the new matrix of the state matrix after the Mixcolumns step. The output data for the different test cases should is separated by a empty line.
Sample Input100 0304 0708 0A 0b0c 0D 0E 0F
Sample Output08 0A 0b1c 1D 1E 1f00 01 02 0314 15 16 17
Authorfzu
Source2012 multi-university Training Contest 7 The main idea: two matrix "multiply", output the matrix after multiplying. This gives a key matrix that represents the secret key matrix. The following gives the T-group data, each group is a matrix of 4*4, with the key matrix multiplied by the matrix to get the result, the matrix does not change, just use the values in the matrix operation, where the addition with XOR instead, if the key matrix is 1, then the matrix in the value of the operation, if it is 2, Let the values in the matrix move one bit to the left, and if it is 3, let the values in the matrix move one bit to the left, and the values with no left shift are different. The left shift may exceed the maximum value represented by the 8-bit, so if the result is greater than 0xFF, it will be the same as 0x1b, and then the remainder will be 256. The result is an uppercase letter, so the output should be%02x output and cannot be output with%02x.
#include <bits/stdc++.h>using namespace Std;int key[4][4]={2,3,1,1,1,2,3,1,1,1,2,3,3,1,1,2};int Ini[4][4],ans [4] [4];int Main () {int t; scanf ("%d", &t); while (t--) {for (int. i=0;i<4;i++) {for (int j=0;j<4;j++) {scanf ("%x", &ini[i][j]); }} for (int i=0;i<4;i++) {for (int j=0;j<4;j++) {int a,tmp=0; for (int k=0;k<4;k++) {if (key[i][k]==1) {a=ini[k][j]; }else if (key[i][k]==2) {a=ini[k][j]; a<<=1; if (a>0xff) {a^=0x1b; a%= (0xff+1); }}else if (key[i][k]==3) {a=ini[k][j]; a<<=1; if (a>0xff) {a^=0x1B; a%= (0xff+1); } A^=ini[k][j]; } tmp^=a; } ans[i][j]=tmp; }} for (int i=0;i<4;i++) {for (int j=0;j<4;j++) {printf ("%02x%c", ans[i][j],j= =3? ' \ n ': '); }} if (T) puts (""); } return 0;}
HDU 4364--matrix Operation —————— "simulation problem"