zoukankan      html  css  js  c++  java
  • HDU 4364——Matrix operation——————【模拟题】

    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 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:

      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.
     
    Input
    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.
     
    Output
    For each case, output the new matrix of the state matrix after the MixColumns step. The output data for two different test cases should be separated by an empty line.
     
    Sample Input
    1
    00 01 02 03
    04 05 06 07
    08 09 0A 0B
    0C 0D 0E 0F
     
    Sample Output
    08 09 0A 0B
    1C 1D 1E 1F
    00 01 02 03
    14 15 16 17
     
    Author
    FZU
     
    Source
     
     
    题目大意:两个矩阵“相乘”,输出相乘后的矩阵。这里给了一个key矩阵,表示秘钥矩阵。下面给出t组数据,每组都是4*4的矩阵,用key矩阵乘以matrix得到结果,矩阵不改变,只是用矩阵中的值进行操作,这里加法用Xor代替,如果key矩阵中是1,那么就不对matrix中的值操作,如果是2,就让matrix中的值左移一位,如果是3,就让matrix中的值左移一位,并且与没有左移的值进行异或。左移可能会超出8位能表示的最大值,所以如果结果大于0xFF,就跟0x1B异或,然后对256取余。结果是大写字母,所以输出时应该是%02X输出,不能用%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?'
    ':' ');
                }
            }
            if(t)
                puts("");
        }
        return 0;
    }
    

      

     
     
  • 相关阅读:
    centos 7 搭建 LNMP ( Linux+Nginx+MySQL+PHP )
    centos 7 安装 redis-5.0.5
    centos 7 安装 Oracle 12c
    centos 7 SVN安装脚本搭建主从同步灵活切换
    Windwos Java‘bat 环境变量配置脚本
    centso 7 Keepalived 配置脚本
    centos 7 私有云盘 OwnCloud 安装搭建脚本
    Linux fing cd 查找文件/文件夹并进入目录命令
    Linux grep命令 -- 三剑客老三
    基础脚本
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4775265.html
Copyright © 2011-2022 走看看