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;
}

  

 
 

最新文章

  1. 异常:java.lang.LinkageError: loader constraint violation: when resolving interface method
  2. [转]Oracle 12c多租户特性详解:PDB 的创建、克隆与维护
  3. 向 div 元素添加圆角边框:
  4. CentOS安装视频播放器SMPlayer
  5. ios专题 - objc runtime 动态增加属性
  6. fedora下体验gentoo安装
  7. HP quality center 9.0 邮件设置
  8. samba server 设置
  9. Chapter 2 Open Book——19
  10. JPlayer Jquery video视频插件
  11. 网页中嵌入百度地图报错:The request has been blocked,the content must served over Https
  12. mac os high sierra下搭建php多版本-php5.2+php5.6-nginx
  13. MicroMsg.SDK.WXApiImplV10: register app failed for wechat app signature check failed
  14. Latex基本用法
  15. linux基础命令---tr
  16. Java Socket TCP编程
  17. GameObject.DestroyImmediate(go, true)会使磁盘资源数据丢失,导致不可用
  18. 【题解】洛谷P1941 [NOIP2014TG] 飞扬的小鸟(背包DP)
  19. SpringMVC拦截器实现登录认证(转发)
  20. Java编程思想学习(一)----对象导论中多态的理解

热门文章

  1. winform播放视频(windows media player)
  2. 在控制台使用MySQL数据库
  3. 浅识J2EE十三个规范
  4. Zookeeper基础使用
  5. c#静态变量赋值问题
  6. kuangbin专题十六 KMP&amp;&amp;扩展KMP HDU3294 Girls&#39; research
  7. gitflow工作流简介
  8. ubuntu14.04 apt-get install找不到软件,更换源解决
  9. matplotlib学习笔记(四)
  10. C#校验手机端或客户端