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?'\n':' ');
}
}
if(t)
puts("");
}
return 0;
}

  

 
 

HDU 4364——Matrix operation——————【模拟题】的更多相关文章

  1. HDU 1262 寻找素数对 模拟题

    题目描述:输入一个偶数,判断这个偶数可以由哪两个差值最小的素数相加,输出这两个素数. 题目分析:模拟题,注意的是为了提高效率,在逐个进行判断时,只要从2判断到n/2就可以了,并且最好用打表法判断素数. ...

  2. HDU 2093 考试排名 模拟题

    解题报告: 题目描述:写一个程序给一个编程考试C++实时提交系统排名,给你的数据是题目的总数,每次错误提交罚的时间分,每位用户的姓名,然后是输入用户每题的完成情况,有一下几种情况,第一,输入只有一个正 ...

  3. HDU 2521 反素数 模拟题

    解题报告:水题,直接附上代码,只是觉得这题的作者是不是吃饱了饭撑的,反素数的概念跟这题一点关系都没有. #include<cstdio> int judge1(int k) { ; ;i& ...

  4. HDU 1256 画8 模拟题

    解题报告:这题我觉得题目有一个没有交代清楚的地方就是关于横线的字符的宽度的问题,题目并没有说,事实上题目要求的是在保证下面的圈高度不小于上面的圈的高度的情况下,横线的宽度就是等于下面的圈的高度. #i ...

  5. HDU 4022 Bombing STL 模拟题

    人工模拟.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector ...

  6. hdu 4515 年月份模拟题

    小Q系列故事——世界上最遥远的距离 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  7. HDU 3787 A+B 模拟题

    解题报告:就是输入两个用逗号隔开的数字,求出这两个数字的和,并且用正常的方式输出来.直接写一个函数将一个包含逗号的数字转换成十进制的数返回就行了.这里推荐一个函数atoi(),参数是char*型的,然 ...

  8. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  9. HDU 4452 Running Rabbits (模拟题)

    题意: 有两只兔子,一只在左上角,一只在右上角,两只兔子有自己的移动速度(每小时),和初始移动方向. 现在有3种可能让他们转向:撞墙:移动过程中撞墙,掉头走未完成的路. 相碰: 两只兔子在K点整(即处 ...

随机推荐

  1. Repeater控件的分隔线

    在Repeater控件中,很容易实现在行与行之间使用分隔线.因为此控件有内置的SeparatorTemplate模版.举个例子吧: 运行时,可以看到效果: 说句实在的话,Insus.NET做一条水平线 ...

  2. VMWare 装mac os x 一个必备优化神器 beamoff

    https://blog.csdn.net/whitehack/article/details/47074403/ VMWare 装mac os x 一个必备优化神器 beamoff 2015年07月 ...

  3. 【bzoj1965】: [Ahoi2005]SHUFFLE 洗牌 数论-快速幂-扩展欧几里得

    [bzoj1965]: [Ahoi2005]SHUFFLE 洗牌 观察发现第x张牌 当x<=n/2 x=2x 当x>n/2 x=2x-n-1 好像就是 x=2x mod (n+1)  就好 ...

  4. mysql 保存23:59:59 自动加一秒

    mysql保存系统传递的时间会诡异的增加一秒钟使用如下工具类: public static Date getEndOfDay(Date date) { Calendar calendarEnd = C ...

  5. 内核启动后,lcd显示logo失败

    针对-s5pv210,但对其他平台也使用 lcd显示logo失败,若显示成功默认的logo是一只企鹅,但是串口打印“Start display and show logo”,但是LCD屏没有显示    ...

  6. IE下 checkbox、radio等标签的label中的img点击无效(点击不能选中)问题解决

    类似以下代码,点击图片不能选中checkbox <input id="dw_1" name="shape[]" value="Round&quo ...

  7. MySQL 关联查询 内连接

    内连接    [INNER| CROSS] JOIN无条件内连接:无条件内连接,又名交叉连接/笛卡尔连接第一张表种的每一项会和另一张表的每一项依次组合#例:mysql>  select  *  ...

  8. ssh-keygen生成公私钥免密码登录远程服务器

    1.终端输入命令:ssh-keygen -t rsa ssh-keygen命令专门是用来生成密钥的.该命令有很多选项,这里列出了最基本的四个: -t 用来指定密钥类型(dsa | ecdsa | ed ...

  9. Linux内核模块简单示例

    1. Linux 内核的整体结构非常庞大,其包含的组件也非常多,使用这些组件的方法有两种: ① 直接编译进内核文件,即zImage或者bzImage(问题:占用内存过多) ② 动态添加 * 模块本身并 ...

  10. pgadmin-linux-centos7.3-连接pgsql

    每次远程连接linux-centos上面的pgsql的时候,需要修改ip,命令如下: -->cd /var/lib/pgsql/data -->vi pg_hba.conf 添加ip如下, ...