Current location - Quotes Website - Signature design - C language implementation of IDEA encryption algorithm
C language implementation of IDEA encryption algorithm
1. The basic process of data encryption is to process the original plaintext file or data according to some algorithm, making it an unreadable code, usually called "ciphertext", so that the original content can be displayed only after inputting the corresponding key, thus protecting the data from being stolen and read by illegal people.

2. Common encryption algorithms

DES (Data Encryption Standard): Data encryption standard, which is fast and suitable for encrypting a large amount of data;

3DES(Triple DES): Based on DES, a piece of data is encrypted three times with three different keys, which is more powerful;

RC2 and RC4: It is faster to encrypt a large amount of data with variable-length keys than DES;

Idea (international data encryption algorithm) International data encryption algorithm: 128-bit key is used to provide very strong security;

RSA: Invented by RSA Company, it is a public key algorithm that supports variable-length keys, and the length of the file block to be encrypted is also variable.

DSA (Digital Signature Algorithm): Digital signature algorithm, which is a standard DSS (Digital Signature Standard);

AES(Advanced Encryption Standard): Advanced encryption standard, which is the next generation high-speed and high-security encryption algorithm standard. At present, one implementation of AES standard is Rijndael algorithm;

BLOWFISH, which uses a variable-length key, can reach 448 bits in length and runs very fast;

Other algorithms, such as ElGamal, Deffie-Hellman, new elliptic curve algorithm ECC, etc.

For example, MD5, the things you do under some more formal and strict websites generally have MD5 values, such as the software tools of security focus, each of which has MD5.

3. General:

# include & ltstdio.h & gt

# include & ltprocess.h & gt

# include & ltconio.h & gt

# include & ltstdlib.h & gt

# Definition? Maxim? 65537

# Definition? Yves? 65536

# Definition? One? 65536

# Definition? Round? eight

Not signed? int? Invi (unsigned? int? Xin);

Not signed? int? Mul (unsigned? int? A, unsigned? int? b);

Invalid? Cip (unsigned? int? In [4], unsigned? int? OUT[4], unsigned? int? z[7][ 10]);

Invalid? Key (unsigned? int? Uskey[9], unsigned? int? z[7][ 10]);

Invalid? De_key (unsigned? int? Z[7][ 10], unsigned? int? DK[7][ 10]);

Invalid? Master ()

{?

int? I,j,k,x;

Not signed? int? Z[7][ 10],DK[7][ 10],XX[5],TT[5],YY[5];

Not signed? int? Uski [9];

Documents? *fpout,* fpin

printf("\n? Input? Key ");

for(I = 1; I < = 8; i++)

scanf("%6u ",& ampuskey[I]);

for(I = 0; I<9; i++)

uskey[I]= 100+I * 3;

key(uskey,Z); /* Generate encryption subkey */

de_key(Z,DK); /* Calculate the decryption subkey */

if((fpin=fopen("ekey.txt "," w ")= = NULL)

{

Printf ("No? Open? File! ”);

Exit (exit _ failure);

}

for(I = 0; I<7; i++)

{

for(j = 0; j & lt 10; j++)

fprintf(fpin," %6u ",Z[I][j]);

fprintf(fpin," \ n ");

}

fclose(fpin);

/* xx [1...5] is plain text */

for(I = 0; I<4; i++)? XX[I]= 2 * I+ 10 1;

clr SCR();

Printf ("Ming? Text? %6u? %6u? %6u? %6u? \n ",XX[0],XX[ 1],XX[2],XX[3]);

if((fpin=(fopen("ideaming.txt "," w "))= = NULL)

{printf ("No? Open? File! ”);

Exit (exit _ failure);

}

fprintf(fpin," %6u,%6u,%6u,%6u? \n ",XX[0],XX[ 1],XX[2],XX[3]);

fclose(fpin);

for(I = 1; I & lt=30000; i++)

cip(XX,YY,Z); /* Encrypt the plaintext in XX with the key Z and store it in YY */

printf("\n\n? Wen Ming? %6u? %6u? %6u? %6u? \n ",YY[0],YY[ 1],YY[2],YY[3]);

if((fpin=fopen("ideamiwn.txt "," w ")= = NULL)

{

Printf ("No? Open? File! ”);

Exit (exit _ failure);

}

fprintf(fpout," %6u? %6u? %6u? %6u\n ",YY[0],YY[ 1],YY[2],YY[3]);

{

Printf ("No? Open? File! ”);

Exit (exit _ failure);

}

fprintf(fpout," %6u? %6u? %6u? %6u? \n ",YY[0],YY[ 1],YY[2],YY[3]);

fclose(FP out);

for(I = 1; I & lt=30000; i++)

Chief inspector (YY, TT, Denmark); /* encryption? YY? Where to? TT? With what? Keys? DK*/

printf("\n? Jay? Rice? %6u? %6u? %6u? %6u? \n ",TT[0],TT[ 1],TT[2],TT[3]);

if((FP out = fopen(" dide out . txt "," w"))==NULL)

{

Printf ("No? Open? File! ”);

Exit (exit _ failure);

}

fprintf(fpout," %6u? %6u? %6u? %6u? \n ",TT[0],TT[ 1],TT[2],TT[3]);

fclose(FP out);

}

/*? This function performs the encryption process in the IDEA algorithm */

Invalid? Cip (unsigned? int? In [4], unsigned? int? OUT[4], unsigned? int? Z[7][ 10])

{

Not signed? int? r,x 1,x2,x3,x4,kk,t 1,t2,a;

x 1 = IN[0]; x2 = IN[ 1]; x3 = IN[2]; x4 = IN[3];

for(r = 1; r & lt=8; r++)

{

/*? Grouping operations on 64-bit blocks */

x 1=mul(x 1,Z[ 1][r]); x4=mul(x4,Z[4][r]);

x2 = x2+Z[2][r]& amp; One; x3 =(x3+Z[3][r])& amp; One;

/*? What is the function of MA structure? */

kk=mul(z[5][r],(x 1^x3));

t 1=mul(z[6][r],(kk+(x2^x4))&; One;

/*? Random transformation PI*/

x 1=x 1^t 1; x4=x4^t2; a=x2^t2; x2=x3^t 1; x3 = a;

}

/*? Output conversion */

OUT[0]=mul(x 1,Z[ 1][round+ 1]);

OUT[3]=mul(x4,Z[ 1][round+ 1]);

OUT[ 1]=(x3+Z[2][round+ 1])& amp; One;

OUT[2]=(x2+Z[3][round+ 1])& amp; One;

}

/*? High-low algorithm multiplication */

Not signed? int? Mul (unsigned? int? A, unsigned? int? b)

{

Dragon? int? p;

Dragon? Not signed? q;

if(a==0)? p = maxim-b;

Or what? if(b==0)? p = maxim-a;

other

{

Q= (unsigned? Long)a* (unsigned? long)b;

p =(q & amp; I)-(q > & gt 16);

What if (p & lt=0)? p = p+maxim;

{

Return? (unsigned)? P&I);

}

/* Through Euclid? Gcd algorithm calculates the reciprocal of xin */

Not signed? int? Invi (unsigned? int? Xin)

{

Dragon? n 1,n2,q,r,b 1,b2,t;

if(xin==0)

B2 = 0;

other

{ n 1 = maxim; n2 = xinB2 = 1; b 1 = 0;

Do {

r =(n 1% N2); q =(n 1-r)/N2;

If (r==0)

if(B2 & lt; 0)? B2 = maxim+B2;

other

{ n 1 = N2; N2 = r;

t = b2

B2 = b 1-q * B2; b 1 = t;

}

}while(r! =0);

}

Return? (unsigned? Dragon? int)B2;

}

/* Generate encryption subkey Z*/

Invalid? Key (unsigned? int? Uskey[9], unsigned? int? Z[7][ 10])

{

Not signed? int? s[54];

int? I,j,r;

for(I = 1; I<9; i++)

s[I- 1]= uskey[I];

/*? Shift? */

for(I = 8; I & lt54; i++)

{

if(i+2)%8==0)/*? Calculate S[ 14], S[22], ... */

S[I]=((S[I-7]& lt; & lt0)^(s[i- 14]>; & gt7)& amp; One;

Or what? If ((i+ 1)%8==0)/*? Calculate S[ 15], S[23], ... */

S[I]=((S[I- 15]& lt; & lt9)^(s[i- 14]>; & gt7)& amp; One;

other

S[I]=((S[I-7]& lt; & lt9)^(s[i-6]>; & gt7)& amp; One;

}

/* Get subitems */

for(r = 1; r & lt= round+ 1; r++)

for(j = 1; j & lt7; j++)

z[j][r]= S[6 *(r- 1)+j- 1];

}

/*? Calculate the decryption key DK? */

Invalid? De_key (unsigned? int? Z[7][ 10], unsigned? int? DK[7][ 10])

{

int? j;

for(j = 1; j & lt= round+ 1; j++)

{ DK[ 1][round-j+2]= inv(Z[ 1][j]);

DK[4][round-j+2]= inv(Z[4][j]);

if(i== 1|j==round+ 1)

{

DK[2][round-j+2]= (Yves-z [2] [j])&; One;

DK[3][round-j+2]= (Yves-z [3] [j])&; One;

}

other

{

DK[2][round-j+2]= inv(Z[3][j]);

DK[3][round-j+2]= inv(Z[2][j]);

}

}

for(j = 1; j & lt= round+ 1; j++)

{

DK[5][round-j+2]= inv(Z[5][j]);

DK[6][round-j+2]= inv(Z[6][j]);

}

}