IDEA加密算法的C语言实现
发布网友
发布时间:2022-04-22 11:34
我来回答
共3个回答
热心网友
时间:2023-11-03 10:14
1、数据加密的基本过程就是对原来为明文的文件或数据按某种算法进行处理,使其成为不可读的一段代码,通常称为“密文”,使其只能在输入相应的密钥之后才能显示出本来内容,通过这样的途径来达到保护数据不被非法人窃取、阅读的目的。
2、常见加密算法
DES(Data Encryption Standard):数据加密标准,速度较快,适用于加密大量数据的场合;
3DES(Triple DES):是基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高;
RC2和 RC4:用变长密钥对大量数据进行加密,比 DES 快;
IDEA(International Data Encryption Algorithm)国际数据加密算法:使用 128 位密钥提供非常强的安全性;
RSA:由 RSA 公司发明,是一个支持变长密钥的公共密钥算法,需要加密的文件块的长度也是可变的;
DSA(Digital Signature Algorithm):数字签名算法,是一种标准的 DSS(数字签名标准);
AES(Advanced Encryption Standard):高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法;
BLOWFISH,它使用变长的密钥,长度可达448位,运行速度很快;
其它算法,如ElGamal、Deffie-Hellman、新型椭圆曲线算法ECC等。
比如说,MD5,你在一些比较正式而严格的网站下的东西一般都会有MD5值给出,如安全焦点的软件工具,每个都有MD5。
3、例程:
#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<stdlib.h>
#define maxim 65537
#define fuyi 65536
#define one 65536
#define round 8
unsigned int inv(unsigned int xin);
unsigned int mul(unsigned int a,unsigned int b);
void cip(unsigned int IN[4],unsigned int OUT[4],unsigned int Z[7][10]);
void key(unsigned int uskey[9],unsigned int Z[7][10]);
void de_key(unsigned int Z[7][10],unsigned int DK[7][10]);
void main()
{
int i,j,k,x;
unsigned int Z[7][10],DK[7][10],XX[5],TT[5],YY[5];
unsigned int uskey[9];
FILE *fpout,*fpin;
printf("\n Input Key");
for(i=1;i<=8;i++)
scanf("%6u",&uskey[i]);
for(i=0;i<9;i++)
uskey[i]=100+i*3;
key(uskey,Z);/*产生加密子密钥*/
de_key(Z,DK);/*计算解密子密钥*/
if((fpin=fopen("ekey.txt","w"))==NULL)
{
printf("cannot open file!");
exit(EXIT_FAILURE);
}
for(i=0;i<7;i++)
{
for(j=0;j<10;j++)
fprintf(fpin,"%6u",Z[i][j]);
fprintf(fpin,"\n");
}
fclose(fpin);
/*XX[1..5]中为明文*/
for(i=0;i<4;i++) XX[i]=2*i+101;
clrscr();
printf("Ming wen %6u %6u %6u %6u \n",XX[0],XX[1],XX[2],XX[3]);
if((fpin=(fopen("ideaming.txt","w")))==NULL)
{printf("cannot 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<=30000;i++)
cip(XX,YY,Z);/*用密钥Z加密XX中的明文并存在YY中*/
printf("\n\n Mingwen %6u %6u %6u %6u \n",YY[0],YY[1],YY[2],YY[3]);
if((fpin=fopen("ideamiwn.txt","w"))==NULL)
{
printf("cannot open file!");
exit(EXIT_FAILURE);
}
fprintf(fpout,"%6u %6u %6u %6u\n",YY[0],YY[1],YY[2],YY[3]);
{
printf("cannot open file!");
exit(EXIT_FAILURE);
}
fprintf(fpout,"%6u %6u %6u %6u \n",YY[0],YY[1],YY[2],YY[3]);
fclose(fpout);
for(i=1;i<=30000;i++)
cip(YY,TT,DK);/*encipher YY to TT with Key DK*/
printf("\n Jie Mi %6u %6u %6u %6u \n",TT[0],TT[1],TT[2],TT[3]);
if((fpout=fopen("dideaout.txt","w"))==NULL)
{
printf("cannot open file!");
exit(EXIT_FAILURE);
}
fprintf(fpout,"%6u %6u %6u %6u \n",TT[0],TT[1],TT[2],TT[3]);
fclose(fpout);
}
/* 此函数执行IDEA算法中的加密过程*/
void cip(unsigned int IN[4],unsigned int OUT[4],unsigned int Z[7][10])
{
unsigned int r,x1,x2,x3,x4,kk,t1,t2,a;
x1=IN[0];x2=IN[1];x3=IN[2];x4=IN[3];
for(r=1;r<=8;r++)
{
/* 对64位的块进行分组运算*/
x1=mul(x1,Z[1][r]);x4=mul(x4,Z[4][r]);
x2=x2+Z[2][r]&one;x3=(x3+Z[3][r])&one;
/* MA结构的函数 */
kk=mul(Z[5][r],(x1^x3));
t1=mul(Z[6][r],(kk+(x2^x4))&one;
/* 随机变换PI*/
x1=x1^t1;x4=x4^t2;a=x2^t2;x2=x3^t1;x3=a;
}
/* 输出转换*/
OUT[0]=mul(x1,Z[1][round+1]);
OUT[3]=mul(x4,Z[1][round+1]);
OUT[1]=(x3+Z[2][round+1])&one;
OUT[2]=(x2+Z[3][round+1])&one;
}
/* 用高低算法上实现乘法运算*/
unsigned int mul(unsigned int a,unsigned int 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&one)-(q>>16);
if(p<=0) p=p+maxim;
{
return (unsigned) (p&one);
}
/*通过Euclidean *算法计算xin的倒数*/
unsigned int inv(unsigned int 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 long int)b2;
}
/*产生加密子密钥Z*/
void key(unsigned int uskey[9],unsigned int Z[7][10])
{
unsigned int S[54];
int i,j,r;
for(i=1;i<9;i++)
S[i-1]=uskey[i];
/* shifts */
for(i=8;i<54;i++)
{
if(i+2)%8==0)/* 对于S[14],S[22],...进行计算 */
S[i]=((S[i-7]<<0)^(S[i-14]>>7)&one;
else if((i+1)%8==0)/* 对于S[15],S[23],...进行计算 */
S[i]=((S[i-15]<<9)^(S[i-14]>>7)&one;
else
S[i]=((S[i-7]<<9)^(S[i-6]>>7)&one;
}
/*取得子密钥*/
for(r=1;r<=round+1;r++)
for(j=1;j<7;j++)
Z[j][r]=S[6*(r-1)+j-1];
}
/* 计算解子密钥DK */
void de_key(unsigned int Z[7][10],unsigned int DK[7][10])
{
int j;
for(j=1;j<=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]=(fuyi-Z[2][j])&one;
DK[3][round-j+2]=(fuyi-Z[3][j])&one;
}
else
{
DK[2][round-j+2]=inv(Z[3][j]);
DK[3][round-j+2]=inv(Z[2][j]);
}
}
for(j=1;j<=round+1;j++)
{
DK[5][round-j+2]=inv(Z[5][j]);
DK[6][round-j+2]=inv(Z[6][j]);
}
}
热心网友
时间:2023-11-03 10:14
你可以参考一下下面:
#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<stdlib.h>
#define maxim 65537
#define fuyi 65536
#define one 65536
#define round 8
unsigned int inv(unsigned int xin);
unsigned int mul(unsigned int a,unsigned int b);
void cip(unsigned int IN[4],unsigned int OUT[4],unsigned int Z[7][10]);
void key(unsigned int uskey[9],unsigned int Z[7][10]);
void de_key(unsigned int Z[7][10],unsigned int DK[7][10]);
void main()
{
int i,j,k,x;
unsigned int Z[7][10],DK[7][10],XX[5],TT[5],YY[5];
unsigned int uskey[9];
FILE *fpout,*fpin;
printf("\n Input Key");
for(i=1;i<=8;i++)
scanf("%6u",&uskey[i]);
for(i=0;i<9;i++)
uskey[i]=100+i*3;
key(uskey,Z);/*产生加密子密钥*/
de_key(Z,DK);/*计算解密子密钥*/
if((fpin=fopen("ekey.txt","w"))==NULL)
{
printf("cannot open file!");
exit(EXIT_FAILURE);
}
for(i=0;i<7;i++)
{
for(j=0;j<10;j++)
fprintf(fpin,"%6u",Z[i][j]);
fprintf(fpin,"\n");
}
fclose(fpin);
/*XX[1..5]中为明文*/
for(i=0;i<4;i++) XX[i]=2*i+101;
clrscr();
printf("Ming wen %6u %6u %6u %6u \n",XX[0],XX[1],XX[2],XX[3]);
if((fpin=(fopen("ideaming.txt","w")))==NULL)
{printf("cannot 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<=30000;i++)
cip(XX,YY,Z);/*用密钥Z加密XX中的明文并存在YY中*/
printf("\n\n Mingwen %6u %6u %6u %6u \n",YY[0],YY[1],YY[2],YY[3]);
if((fpin=fopen("ideamiwn.txt","w"))==NULL)
{
printf("cannot open file!");
exit(EXIT_FAILURE);
}
fprintf(fpout,"%6u %6u %6u %6u\n",YY[0],YY[1],YY[2],YY[3]);
{
printf("cannot open file!");
exit(EXIT_FAILURE);
}
fprintf(fpout,"%6u %6u %6u %6u \n",YY[0],YY[1],YY[2],YY[3]);
fclose(fpout);
for(i=1;i<=30000;i++)
cip(YY,TT,DK);/*encipher YY to TT with Key DK*/
printf("\n Jie Mi %6u %6u %6u %6u \n",TT[0],TT[1],TT[2],TT[3]);
if((fpout=fopen("dideaout.txt","w"))==NULL)
{
printf("cannot open file!");
exit(EXIT_FAILURE);
}
fprintf(fpout,"%6u %6u %6u %6u \n",TT[0],TT[1],TT[2],TT[3]);
fclose(fpout);
}
/* 此函数执行IDEA算法中的加密过程*/
void cip(unsigned int IN[4],unsigned int OUT[4],unsigned int Z[7][10])
{
unsigned int r,x1,x2,x3,x4,kk,t1,t2,a;
x1=IN[0];x2=IN[1];x3=IN[2];x4=IN[3];
for(r=1;r<=8;r++)
{
/* 对64位的块进行分组运算*/
x1=mul(x1,Z[1][r]);x4=mul(x4,Z[4][r]);
x2=x2+Z[2][r]&one;x3=(x3+Z[3][r])&one;
/* MA结构的函数 */
kk=mul(Z[5][r],(x1^x3));
t1=mul(Z[6][r],(kk+(x2^x4))&one;
/* 随机变换PI*/
x1=x1^t1;x4=x4^t2;a=x2^t2;x2=x3^t1;x3=a;
}
/* 输出转换*/
OUT[0]=mul(x1,Z[1][round+1]);
OUT[3]=mul(x4,Z[1][round+1]);
OUT[1]=(x3+Z[2][round+1])&one;
OUT[2]=(x2+Z[3][round+1])&one;
}
/* 用高低算法上实现乘法运算*/
unsigned int mul(unsigned int a,unsigned int 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&one)-(q>>16);
if(p<=0) p=p+maxim;
{
return (unsigned) (p&one);
}
/*通过Euclidean *算法计算xin的倒数*/
unsigned int inv(unsigned int 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 long int)b2;
}
/*产生加密子密钥Z*/
void key(unsigned int uskey[9],unsigned int Z[7][10])
{
unsigned int S[54];
int i,j,r;
for(i=1;i<9;i++)
S[i-1]=uskey[i];
/* shifts */
for(i=8;i<54;i++)
{
if(i+2)%8==0)/* 对于S[14],S[22],...进行计算 */
S[i]=((S[i-7]<<0)^(S[i-14]>>7)&one;
else if((i+1)%8==0)/* 对于S[15],S[23],...进行计算 */
S[i]=((S[i-15]<<9)^(S[i-14]>>7)&one;
else
S[i]=((S[i-7]<<9)^(S[i-6]>>7)&one;
}
/*取得子密钥*/
for(r=1;r<=round+1;r++)
for(j=1;j<7;j++)
Z[j][r]=S[6*(r-1)+j-1];
}
/* 计算解子密钥DK */
void de_key(unsigned int Z[7][10],unsigned int DK[7][10])
{
int j;
for(j=1;j<=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]=(fuyi-Z[2][j])&one;
DK[3][round-j+2]=(fuyi-Z[3][j])&one;
}
else
{
DK[2][round-j+2]=inv(Z[3][j]);
DK[3][round-j+2]=inv(Z[2][j]);
}
}
for(j=1;j<=round+1;j++)
{
DK[5][round-j+2]=inv(Z[5][j]);
DK[6][round-j+2]=inv(Z[6][j]);
}
}
热心网友
时间:2023-11-03 10:14
gogogo!