首页 > ACM题库 > HDU-杭电 > HDU 4364-Matrix operation-位运算-[解题报告]HOJ
2015
05-23

HDU 4364-Matrix operation-位运算-[解题报告]HOJ

Matrix operation

问题描述 :

  M_0 is interested in cryptography. Recently he learned the Advanced Encryption Standard (AES). AES operates on a 4×4 column-major order matrix of bytes and he found it that there is a step called the MixColumns step in the AES.
  In the MixColumns step, there is a state matrix, the four bytes of each column of the state are combined using an invertible linear transformation. The MixColumns function takes four bytes as input and outputs four bytes, where each input byte affects all four output bytes. Together with ShiftRows, MixColumns provides diffusion in the cipher.
  During this operation, each column is multiplied by the known matrix that for the 128 bit key is:
Draw and paint

  The addition operation is defined as: xor.
  The multiplication operation is defined as:
  multiplication by 1 means no change
  multiplication by 2 means shifting to the left
  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 be performed if the shifted value is larger than 0xFF.

输入:

There are several cases.
The first line is an integer T (T <= 20000), indicating the test cases.
Then is the state matrix, each case followed by four lines, each line contains four bytes, separated by spaces.

输出:

There are several cases.
The first line is an integer T (T <= 20000), indicating the test cases.
Then is the state matrix, each case followed by four lines, each line contains four bytes, separated by spaces.

样例输入:

1
00 01 02 03
04 05 06 07
08 09 0A 0B
0C 0D 0E 0F

样例输出:

08 09 0A 0B
1C 1D 1E 1F
00 01 02 03
14 15 16 17

题意是给定一个状态矩阵,用该矩阵右乘一个已知的矩阵,该已知矩阵的元素是2或3或1,定义运算加法为按位异或,乘法有三种,当乘的数字是1时,不变,当乘的数字是2时,进行左移位运算,当乘的数字是3时,先进行左移位运算,然后与原来没进行移位操作的时候的这个数进行按位异或操作。每次移位操作后,如果所得的数字大于0xFF,则还要和0x1B进行按位异或操作。  要注意的一点就是进行完按位异或操作后,所得结果还可能超过0xFF,而题目要求每个矩阵元素只占1个字节,即8个二进制位,2个十六进制位,所以所得结果不能超出0xFF,所以当值大于0xFF时,要取余。还有一点注意的,空行只需在两种测试数据之间输出,最后不用输出。

#include <iostream>
#include <cstdio>
using namespace std;
int mat[4][4]={2,3,1,1,1,2,3,1,1,1,2,3,3,1,1,2};
int state[4][4],ans[4][4];

void work()
{
    int i,j,k,temp1,temp2[4];
    for(i=0;i<4;i++)
    for(j=0;j<4;j++)
    {
        for(k=0;k<4;k++)
        {
            if(mat[i][k]==2)
            {
                temp1=state[k][j]<<1;
                if(temp1>0xFF)
                temp1^=0x1B;
                if(temp1>0xFF)
                temp1%=(0xFF+1);
            }
            else if(mat[i][k]==3)
            {
                temp1=state[k][j]<<1;
                if(temp1>0xFF)
                temp1^=0x1B;
                temp1^=state[k][j];
                if(temp1>0xFF)
                temp1%=(0xFF+1);
            }
            else
            temp1=state[k][j];
            temp2[k]=temp1;
        }
        for(k=1;k<4;k++)
        temp2[0]^=temp2[k];
        ans[i][j]=temp2[0];
    }
}

int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        for(i=0;i<4;i++)
        for(j=0;j<4;j++)
        scanf("%X",&state[i][j]);
        work();
        for(i=0;i<4;i++)
        for(j=0;j<4;j++)
        if(j!=3)
        printf("%02X ",ans[i][j]);
        else
        printf("%02X\n",ans[i][j]);
        if(t!=0)
        printf("\n");
    }
    return 0;
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/sky1203850702/article/details/7877751