1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| /* Programme de cryptage IDEA */
#include <stdio.h>
#define maxim 65537
#define fuyi 65536
#define un 65535
#define etape 8
void chiffrer(unsigned IN[5],unsigned OUT[5], unsigned Z[7][10]);
void cle(short unsigned uscle[9], unsigned Z[7][10]);
void de_cle(unsigned Z[7][10], unsigned DK[7][10]);
unsigned inv(unsigned xin);
unsigned mul(unsigned a, unsigned b);
void main()
{
int i,j,x;
unsigned Z[7][10], DK[7][10], XX[5], TT[5], YY[5];
short unsigned uscle[9];
for(i=1;i<=8;i++) uscle[i]=i;
cle(uscle,Z); /* intgrer les sous-cle Z[i][r] */
printf("\nencryption cles\t Z1\t Z2\t Z3\t Z4\t Z5\t Z6");
for(j=1;j<=9;j++) { printf("\n %3d-ieme etape",j);
if(j==9) for(i=1;i<=4;i++) printf("\t%6d",Z[i][j]);
else for(i=1;i<=6;i++) printf("\t%6d",Z[i][j]);
}
de_cle(Z,DK); /* compute decryption subcles DK[i][r] */
printf("\n\ndecryption cles\t DK1\t DK2\t DK3\t DK4\t DK5\t DK6");
for(j=1;j<=9;j++) { printf("\n %3d-ieme etape",j);
if(j==9) for(i=1;i<=4;i++) printf("\t%6d",DK[i][j]);
else for(i=1;i<=6;i++) printf("\t%6d",DK[i][j]);
}
for(x=1;x<=4;x++) XX[x]=x-1; /*ici on pose le text a chiffrer est : 0, 1, 2, 3 ,4*/
printf("\n\n texte en claire X\t%6u\t%6u\t%6u\t%6u\n",
XX[1],XX[2],XX[3],XX[4]);
chiffrer(XX,YY,Z); /* chiffrer XX to YY avec la cle Z */
printf("\n\n texte crypter Y\t%6u\t%6u\t%6u\t%6u\n",
YY[1],YY[2],YY[3],YY[4]);
chiffrer(YY,TT,DK); /* dechiffrer YY to TT avec les cle DK */
printf("\n\n result T\t%6u\t%6u\t%6u\t%6u\n",
TT[1],TT[2],TT[3],TT[4]);
}
/* Algorithme de cryptage */
void chiffrer(unsigned IN[5],unsigned OUT[5],unsigned Z[7][10])
{
unsigned r,x1,x2,x3,x4,kk,t1,t2,a;
x1=IN[1]; x2=IN[2]; x3=IN[3]; x4=IN[4];
for(r=1;r<=8;r++) /* les etapes */
{
/* les operations effectuer en bloc de 64bits */
x1 = mul(x1,Z[1][r]); x4 = mul(x4,Z[4][r]);
x2 = (x2 + Z[2][r]) & un; x3 = (x3 + Z[3][r]) & un;
kk = mul(Z[5][r],(x1^x3));
t1 = mul(Z[6][r],(kk+(x2^x4)) & un);
t2 = (kk+t1) & un;
/* la permutation PI */
x1 = x1^t1; x4=x4^t2;
a = x2^t2; x2=x3^t1; x3=a;
printf("\n\t%1u-ieme etape %6u\t%6u\t%6u\t%6u",r,x1,x2,x3,x4);
}
/* le resultat */
OUT[1] = mul(x1,Z[1][etape+1]);
OUT[4] = mul(x4,Z[4][etape+1]);
OUT[2] = (x3+Z[2][etape+1]) & un;
OUT[3] = (x2+Z[3][etape+1]) & un;
}
/* la fonction de multiplication utiliser dans le cryptage */
unsigned mul(unsigned a,unsigned b)
{
long int p;
long unsigned q;
if(a==0) p=maxim-b;
else
if(b==0) p=maxim-a;
else {
q=(unsigned long)a*(unsigned long)b;
p=(q & un) - (q>>16);
if(p<=0) p=p+maxim;
}
return (unsigned)(p&un);
}
/* compute inverse of xin by Euclidean gcd alg. */
unsigned inv(unsigned xin)
{
long n1,n2,q,r,b1,b2,t;
if(xin==0) b2=0;
else
{ n1=maxim; n2=xin; b2=1; b1=0;
do { r = (n1 % n2); q = (n1-r)/n2;
if(r==0) { if(b2<0) b2=maxim+b2; }
else { n1=n2; n2=r; t=b2; b2=b1-q*b2; b1=t; }
} while (r!=0);
}
return (unsigned)b2;
}
/* generer les sous-cles Z */
void cle(short unsigned uscle[9], unsigned Z[7][10])
{
short unsigned S[54];
int i,j,r;
for(i=1;i<9;i++) S[i-1]=uscle[i];
for(i=8;i<54;i++)
{
if((i+2)%8 == 0) /* pour S[14],S[22],... */
S[i] = ((S[i-7]<<9) ^ (S[i-14]>>7)) & un;
else if((i+1)%8==0) /* pour S[15],S[23],... */
S[i] = ((S[i-15]<<9) ^ (S[i-14]>>7)) & un;
else
S[i] = ((S[i-7]<<9) ^ (S[i-6]>>7)) & un;
}
/* donnée les sous-cles */
for(r=1;r<=etape+1;r++)
for(j=1;j<7;j++)
Z[j][r]=S[6*(r-1)+j-1];
}
/* compute decryption subcles DK's */
void de_cle(unsigned Z[7][10],unsigned DK[7][10])
{
int j;
for(j=1;j<=etape+1;j++)
{
DK[1][etape-j+2] = inv(Z[1][j]);
DK[4][etape-j+2] = inv(Z[4][j]);
if (j==1 || j==etape+1) {
DK[2][etape-j+2] = (fuyi-Z[2][j]) & un;
DK[3][etape-j+2] = (fuyi-Z[3][j]) & un;
} else {
DK[2][etape-j+2] = (fuyi-Z[3][j]) & un;
DK[3][etape-j+2] = (fuyi-Z[2][j]) & un;
}
}
for(j=1;j<=etape+1;j++)
{ DK[5][etape+1-j]=Z[5][j];
DK[6][etape+1-j]=Z[6][j];
}
} |
Partager