}
void GetPeerKey(void)
{
printf("Please input your 8 bytes key\n");
gets(key);
byte_to_bit(64,key,peerkey);//change byte into bit
}
//change byte into bit
//change the 64-bit key into 56-bit
void Transform(int * out,int * in,const int * table,int n)
{
int i,j;
for (i=0,j=0;i<n;i++)
out[j++]=in[table[i]-1];
}
//Rotate funtion
void RotateL(int *In, int len, int loop)
{
static int Tmp[64];
memcpy(Tmp, In, loop);//Store the first (loop) bits to Tmp
memcpy(In, In+loop, len-loop);//Copy the end of the (len-loop) bits to the front of In
memcpy(In+len-loop, Tmp, loop);//Move the Stored (loop) bits to the end of Nes In
}
//get every round key
void Des_SetKey(void)
{
static int *KL = &first_round_key[0], *KR = &first_round_key[28];
int i;
GetPeerKey();
Transform(first_round_key,peerkey,PC1_Table,56);
for (i=0; i<16; i++)
{
RotateL(KL, 28, LOOP_Table[i]); //Left shift circle
RotateL(KR, 28, LOOP_Table[i]); //Left shift circle
Transform(subkey[i], first_round_key, PC2_Table, 48);//PC2 Transform
}
}
-------------------------------华丽的分割线--------------------------
Exchange(Li,Ri);//exchange Li and Ri
memcpy(tmp,text,64*sizeof(int));
Transform(text,tmp,IPR_Table,64);//permute text at last
}
-------------------------------华丽的分割线--------------------------
-----------------------------analyse.c-------------------------------
#include "des.h"
#define TEXT_NUM 256
const int getbit=0x01;
extern const int IP_Table[64];
extern int subkey[16][48];
static int randbox[8][4][16];
extern int S_Box[8][4][16];
static int linebox[8][4][16];
const static int From16_Prime[32]={3, 5, 7, 9, 11, 13, 15, 17, 23, 19, 25, 21, 33, 31, 27, 29, 39, 37, 41, 35, 43
, 49, 45, 47, 53, 57, 51, 55, 63, 65, 61, 59};
void Print_File(const int *p,FILE * fp)
{
int i;
for (i=0;i<64;i++)
{
fprintf(fp,"%d",p[i]);
if (!((i+1)%8))
fputc(' ',fp);
}
fputc('\r',fp);
fputc('\n',fp);
}
void Snow_Plaintext(int *p1,int *p2)
{
int i,anybit=rand()%64;
for (i=0;i<64;i++)
{
p1[i] = rand()%2;
p2[i] = p1[i];
}
p2[anybit] = p1[anybit] ^ getbit;
}
void SnowSlip(void)
{
int p1[64],p2[64];
FILE *fp;
int i;
int *L1i = &p1[0],*R1i = &p1[32],*L2i = &p2[0],*R2i = &p2[32],tmp1[64],tmp2[64];
memcpy(tmp1,p1,64*sizeof(int));
Transform(p1,tmp1,IP_Table,64);//permute plaintext first
memcpy(tmp2,p2,64*sizeof(int));
Transform(p2,tmp2,IP_Table,64);//permute plaintext first
if ((fp=fopen("Snowslip_Analyse.txt","wb"))==NULL)
{
printf("Can't open the Snowslip_Analyse.txt\r\n");
exit(1);
}
Snow_Plaintext(p1,p2);
fprintf(fp,"\t\t\t###Snowslip_Analyse###\r\n");
fprintf(fp,"The plaintexts :\r\n");
Print_File(p1,fp);
Print_File(p2,fp);
for (i=0;i<16;i++)
{
if (i==0)
fprintf(fp,"\r\nThis is the 1st round.\r\n");
else if (i==1)
fprintf(fp,"\r\nThis is the 2nd round.\r\n");
else if (i==2)
fprintf(fp,"\r\nThis is the 3rd round.\r\n");
else
fprintf(fp,"\r\nThis is the %dth round.\r\n",i+1);
Every_Round_Des(L1i,R1i,subkey,i);
Print_File(p1,fp);
Every_Round_Des(L2i,R2i,subkey,i);
Print_File(p2,fp);
int j,count=0;
for (j=0;j<64;j++)
if (p1[j] != p2[j])
count++;
fprintf(fp,"%d bits different.\r\n",count);
}
printf("Snow Slip Analyse Complete!\nThe result is in the Snowslip_Analyse.txt\n\n");
fclose(fp);
}
void Integrality_Plaintext(int * p,const int anybit,const int bit)
{
int i;
for (i=0;i<64;i++)
p[i]=rand()%2;
p[anybit]=bit;
}
void Integrality(void)
{
int anybit=rand()%64,bit=rand()%2,plain[64];
int i,count[64];
memset(count,0,64*sizeof(int));
for (i=0;i<TEXT_NUM;i++)
{
Integrality_Plaintext(plain,anybit,bit);
DES(plain,1);
int j;
for (j=0;j<64;j++)
if (plain[j])
count[j]++;
}
printf("Integrality Analyse Complete!\nThe result is in the Integrality_Analyse.txt.\n\n");
FILE * fp=fopen("Integrality_Analyse.txt","wb");
fprintf(fp,"Every message is the same as %d in %d.\r\n",bit,anybit);
fprintf(fp,"Every bit is 1 of the frequency.\r\n");
fprintf(fp,"bit\tfrequency\r\n");
for (i=0;i<64;i++)
fprintf(fp,"%d\t%.3f\r\n",i+1,(float)count[i]/TEXT_NUM);
fclose(fp);
}
int bit_to_ten(int * in,int n)
{
int i,j,num=0;
for (j=0,i=n-1;i>=0;j++,i--)
{
num += (getbit<<j)*in[i];
}
return num;
}
void Differential(void)
{
int count[8][64][16];
memset(count,0,8*64*16*sizeof(int));
int i, j, k;
FILE * fp=fopen("Differential.txt","wb");
fprintf(fp,"The Differential analysis of every S-box.\r\n");
for (i=0;i<8;i++)
{
for (j=0;j<64;j++)
{
int in1[6]={0},in2[6]={0},out1[4]={0},out2[4]={0};
for (k=0;k<64;k++)
{
ten_to_bit(in1,j,6);//j-->bit(in1)
S_single(in1,out1,i);
ten_to_bit(in2,k,6);//k-->bit(in2)
S_single(in2,out2,i);
Xor_func(in1,in2,6);//in1 ^ in2 --> in1
Xor_func(out1,out2,4);//out1 ^ out2 --> out1
count[i][bit_to_ten(in1,6)][bit_to_ten(out1,4)]++;
}
}
}
for (i=0;i<8;i++)
{
fprintf(fp,"\r\nS-box %d Differential Table:\r\n\t",i+1);
for (k=0;k<16;k++)
fprintf(fp,"%d\t",k);
fprintf(fp,"max-number\tlocation");
for (j=0;j<64;j++)
{
int max=-1,local;
fprintf(fp,"\r\n%d\t",j);
for (k=0;k<16;k++)
{
if (max<count[i][j][k])
{
max=count[i][j][k];
local=k;
}
fprintf(fp,"%d\t",count[i][j][k]);
}
fprintf(fp,"%d\t\t%d",max,local);
}
}
printf("\nDifferential Analyse Comlpete!\nThe result is in the Differential.txt.\n\n");
fclose(fp);
}
void Rand_S_Box(void)
{
int i,j,k,flag[16],temp;
for (i=0;i<8;i++)
{
for (j=0;j<4;j++)
{
memset(flag,0,16);
for (k=0;k<16;k++)
{
for (temp=rand()%16;flag[temp];temp=rand()%16);
randbox[i][j][k]=temp;
}
}
}
memcpy(S_Box,randbox,8*4*16*sizeof(int));
}
void Line_S_Box(void)
{
int i,j,k,index,a=17;
for (i=0;i<8;i++)
{
for (j=0;j<4;j++)
{
int flag[16]={0};
for (k=0;k<16;a++,k++)
{
for (index=(a*From16_Prime[i*4+j])%16;flag[index];a++,index=(a*From16_Prime[i*4+j])%16);
flag[index]=1;
linebox[i][j][k]=index;
}
}
}
memcpy(S_Box,linebox,8*4*16*sizeof(int));
}
void Control(void)
{
char ch;
int choise;
while (1)
{
printf("\t\t1.Snow Slip Analyse.\n");
printf("\t\t2.Integrality Analyse.\n");
printf("\t\t3.Differences Analyse.\n");
printf("\t\t0.Quit.\n");
printf("Your choice:");
for (;!isdigit(ch=getchar()););
choise = ch -'0';
switch (choise)
{
case 1:
SnowSlip();
break;
case 2:
Integrality();
break;
case 3:
Differential();
break;
default:
return;
}
}
}
void Other_S_Box_Analyse(void)
{
char ch;
int choise;
while (1)
{
printf("\t\t1.Use S-Line-Box\n\t\t2.Use S-Rand-Box\n\t\t0.Quit.\n");
printf("Your choice:");
for (;!isdigit(ch=getchar()););
choise = ch -'0';
switch (choise)
{
case 1:
Line_S_Box();
Control();
break;
case 2:
Rand_S_Box();
Control();
break;
default :
return;
}
}
}