#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define N 64
void GetInverseSbox(int Sbox[N],int inverseSbox[N]){
for (int i=0; i<N; i++)
{
inverseSbox[Sbox[i]]=i;
}
//这里方便你查看数组的逆,如果确认没问题可以注释掉以下三行,加快运行速度
//printf("Lannister will printf inverseSbox here");
//for(int i=0;i<N;i++){printf("%d,",inverseSbox[i]);}
//printf("\n");
}
int main(void){
int count =0;
int Sbox[N];
int loop =0;
int maxvalue = 0;
FILE *fp=fopen("/home/edward/桌面/TIAN/all_ea.txt","r");
FILE *fr=fopen("/home/edward/桌面/TIAN/done_ea.txt","w");
printf("Lannister will get sbox here\n");
printf("******************************************\n");
if (fp==NULL)
{
printf("fail to open the txt.\n");
return -1;
}
while(!feof(fp)){
//这里的fscanf会按照标准的格式进行不断的读取,一次读一个sbox
fscanf(fp,"[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]\n",Sbox,Sbox+1,Sbox+2,Sbox+3,Sbox+4,Sbox+5,Sbox+6,Sbox+7,Sbox+8,Sbox+9,Sbox+10,Sbox+11,Sbox+12,Sbox+13,Sbox+14,Sbox+15,Sbox+16,Sbox+17,Sbox+18,Sbox+19,Sbox+20,Sbox+21,Sbox+22,Sbox+23,Sbox+24,Sbox+25,Sbox+26,Sbox+27,Sbox+28,Sbox+29,Sbox+30,Sbox+31,Sbox+32,Sbox+33,Sbox+34,Sbox+35,Sbox+36,Sbox+37,Sbox+38,Sbox+39,Sbox+40,Sbox+41,Sbox+42,Sbox+43,Sbox+44,Sbox+45,Sbox+46,Sbox+47,Sbox+48,Sbox+49,Sbox+50,Sbox+51,Sbox+52,Sbox+53,Sbox+54,Sbox+55,Sbox+56,Sbox+57,Sbox+58,Sbox+59,Sbox+60,Sbox+61,Sbox+62,Sbox+63);
//这里方便你查看读取的sbox是否正确,如果确认没问题可以注释掉以下三行,加快运行速度
//printf("Lannister will printf Sbox here");
//for(int i=0;i<N;i++) printf("%d,",Sbox[i]);
//printf("\n");
//这里将求sbox的逆拿出去执行了
int inverseSbox[N]={0};
GetInverseSbox(Sbox,inverseSbox);
//不太清楚你这里的主体逻辑,你自己可以加一些打印确保这里是按照你的想法执行的,可以先以小份数据进行验证
int BCT[N][N]= {0};
int detain, detaout, Sin;
for (detain= 0; detain < N; detain++){
for (detaout =0; detaout< N; detaout++){
for (Sin = 0; Sin < N; Sin++){
if (detain == ((inverseSbox[Sbox[Sin] ^ detaout]) ^ (inverseSbox[Sbox[Sin^detain] ^ detaout]))){
BCT[detain][detaout] ++;
//下面这个打印是方便你看每一次计算BCT值的过程,确认正确后可以注释掉
//printf("Lannister will know BCT[%d][%d] increase himslef once,and BCT values is %d. \n",detain,detaout,BCT[detain][detaout]);
}
}
}
}
//下面是求BCT中的最大值
maxvalue = 0;
printf("The boomerang commutative table is as follows loop = %d:\n", loop++);
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
if ((BCT[i][j] > maxvalue)&&(i!=0)&&(j!=0)){
maxvalue = BCT[i][j];
//printf("glad to find a BCT value is 4.\n");
}
}
}
if (maxvalue<=11){
printf("If this BCT is 11 then Lannister will output it to file.\n");
count++;
}
}
printf("The amount with BCT <11 is: %d\n",count);
fclose(fr);
fclose(fp);
printf("******************************************\n");
return 0;
}
网友评论