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. 对XML文档进行修改

    怎样对XML文档时行修改.Insus.NET在此举个简单的例子.XML文档,就以这篇博文:http://www.cnblogs.com/insus/p/3274220.html 如果我们想对其中一个节 ...

  2. 个人JS体系整理(二)

    一. eval eval()函数计算JavaScript字符串,并把它作为脚本代码来执行.如果参数是一个表达式,eval()函数将执行表达式.如果参数是Javascript语句,eval()将执行Ja ...

  3. CF525E Anya and Cubes(meet in the middle)

    题面 给你\(n\)个数,\(n\le 26\)初始序列为\(a_i,0\le a_i\le 10^9\) 你有\(k\)个\(!\),每个\(!\)可以使序列中的一个数变成\(a_i!\) 例如\( ...

  4. IdentityServer4 学习笔记[2]-用户名密码验证

    回顾 上一篇介绍了IdentityServer4客户端授权的方式,今天来看看IdentityServer4的基于密码验证的方式,与客户端验证相比,主要是配置文件调整一下,让我们来看一下 配置修改 pu ...

  5. windows下eclipse远程连接hadoop集群开发mapreduce

    转载请注明出处,谢谢 2017-10-22 17:14:09  之前都是用python开发maprduce程序的,今天试了在windows下通过eclipse java开发,在开发前先搭建开发环境.在 ...

  6. [POI2009]KAM-Pebbles BZOJ1115 [ 待填坑 ] 博弈

    有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子,但是要保证操作后仍然满足初始时的条件谁没有石子可移时输掉游戏.问先手是否必胜. 感谢 ...

  7. P4824 [USACO15FEB]Censoring (Silver) 审查(银)

    传送门 一个串的匹配肯定考虑KMP 那就暴力KMP 记录一下到每个字符时匹配的位置 找到一个符合的串就标记然后暴力回跳 感觉好像太暴力了... #include<iostream> #in ...

  8. 新磁盘创建lvm并挂载

    ### .查看硬盘 fdisk -l ### 删除分区 fdisk /dev/sdc ### 按d删除,按w保存并退出 ### 创建pv pvcreate /dev/sdc ### 创建 vg vgc ...

  9. NET Core 不错教程***************

    Twinhead 当前标签: .Net Core   RabbitMQ教程 Twinhead 2019-01-26 20:02 阅读:5 评论:0     缓存击穿.缓存穿透和缓存雪崩 Twinhea ...

  10. php数组·的方法3-数组和变量之间的转换

    /* * 数组和变量之间的转换 * */ //extract() 使用数组定义一组变量 // 键名为变量名 键值为变量值(类似于js的解构赋值) // 返回值是数组的长度 echo '<hr&g ...