当前位置: 代码迷 >> C语言 >> [讨论]有关提高DES效率的问题
  详细解决方案

[讨论]有关提高DES效率的问题

热度:308   发布时间:2007-11-02 12:51:44.0
[讨论]有关提高DES效率的问题

下面是我写的DES算法,可以实现对任意文件的加解密。。
但是效率太低大约150kb/s。。。。
下面我把代码贴出来,大家帮我看下,是甚么地方影响效率啊。。我好改改。。
我用的编译环境是vs2005 vc

// DES.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "conio.h"
#include "time.h"
#include "iostream"
unsigned int *shift(unsigned int *K1,int i)
{//将存在2个unsigned int里的56位数据,左右两半分别循环左移i位
unsigned int m1,m2;unsigned int *K12;K12=new unsigned[2];int t;
for(t=0;t<2;t++)
{
m1=K1[t]<<4;//K1[t]中的做i位提出
m1=m1>>(32-i);
m2=K1[t]<<(4+i);//将K[t]中的右28-i位提出
m2=m2>>4;
m2=m2+m1;//将两部分整合在一块
K12[t]=m2;
}
return K12;
}
unsigned int *key(unsigned int *K0)//生成16个子密钥,保存在32 个unsigned int中
{ unsigned int temp,K11[2];unsigned int *K;int t;int i,j,k,m;unsigned *K1,*K00;K1=new unsigned[2];K00=new unsigned[2];K=new unsigned[32];
K1[0]=0;K1[1]=0;K11[0]=0;K11[1]=0;
char A[56]={57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36,
63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4};
char B[48]={14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,
41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};
int C[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
for(i=0;i<56;i++)//IP置换
{
t=A[i]; j =t/32;//决定是将该位保存在哪个unsigned int 中
if(t<32)//将56位密钥逐位取出,右移到最低位
{
temp=K0[j]>>(32-t);
temp=temp&1;

}
else
{
t=t-32;
temp=K0[j]>>(32-t);
temp=temp&1;


}
if(i<24)//将置换后的密钥整合到2个无符号整型字符中
{
K1[0]=K1[0]<<1;
K1[0]=K1[0]+temp;
}
else
{

K1[1]=K1[1]<<1;
K1[1]=K1[1]+temp;

}
}
K11[1]=K1[1]<<4;//将56位密钥有效位分别存放在两个unsigned中,一边存28位
K11[1]=K11[1]>>4;
temp=K1[1]>>28;
K11[0]=K1[0]<<4;
K11[0]=K11[0]+temp;
K1[0]=K11[0];K1[1]=K11[1];
for(k=0;k<16;k++)
{
K00=shift(K1,C[k]);//进行循环移位
K1[0]=K00[0];K1[1]=K00[1];
for(i=0;i<48;i++)//进行压缩置换
{
j=i/24;
t=B[i];//用t定位是在那个unsigned int 中
if(t<29)//将指定的一位右移到字符的最低位
{
temp=K00[j]>>(28-t);
temp=temp&1;
}
else
{
t=t-28;
temp=K00[j]>>(28-t);
temp=temp&1;
}
if(i<24)//将置换后的数据整合在2个unsigned int变量中,一个保存24位
{
K11[0]=K11[0]<<1;
K11[0]=K11[0]+temp;
}
else

{
K11[1]=K11[1]<<1;
K11[1]=K11[1]+temp;
}

}
K11[0]=K11[0]<<8;K11[0]=K11[0]>>8;//将密钥的高位清空,只保留有效位
K11[1]=K11[1]<<8;K11[1]=K11[1]>>8;
m=2*k;
K[m]=K11[0];K[m+1]=K11[1];//生成一个子密钥
}
return K;
}
unsigned int fk(unsigned int K0,unsigned int K1,unsigned int L,unsigned int R)
{//FK函数 完成S_BOX的运算
unsigned int S3;
unsigned int S0[8],M11[2]={0},temp,S2;unsigned int h,l,t;int *S1;S1=new int[8];int i;//unsigned int *S3;S3=new int[1];
unsigned int S[8][4][16]={{{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},{0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}},

{{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}},

{{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}},

{{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}},

{{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},{14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},{4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},{11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}},

{{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}},

{{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},{1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}},

{{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}}};
char C[48]={32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15,16,17,
16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1};
int P[32]={16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25};
S2=0;S3=0;
for(i=0;i<48;i++)//查表,为进行扩充置换做准备
{
t=C[i];//逐个将需要的位,右移到最低位
temp=R>>(32-t);
temp=temp&1;
if(i<24)//置换后的结果分别保存在两个unsigned int的低24位
{
M11[0]=M11[0]<<1;
M11[0]=M11[0]+temp;
}
else
{
M11[1]=M11[1]<<1;
M11[1]=M11[1]+temp;
}
}
M11[0]=M11[0]<<8;M11[0]=M11[0]>>8;//将M1中的高8位清空
M11[1]=M11[1]<<8;M11[1]=M11[1]>>8;
M11[0]=M11[0]^K0;M11[1]=M11[1]^K1;//将置换后的结果分别与K0合K1按位与
for(i=0;i<4;i++)
{//将置换后的结果分成8份,进入8个S盒进行操作
temp=M11[0]<<(8+6*i);
S0[i]=temp>>26;
temp=M11[1]<<(8+6*i);
S0[i+4]=temp>>26;
}
for(i=0;i<8;i++)
{
h=S0[i]&1;//用S0[i]决定S盒的行
t=S0[i]<<26;
t=t>>31;
t=t<<1;
h=t+h;
l=S0[i]<<27;//用S0[i]决定S盒的列
l=l>>28;
S1[i]=S[i][h][l];//进入S盒进行查询
}
for(i=0;i<8;i++)//在S盒输出的数据整合在一个S2中
{
temp=S1[i]<<(28-4*i);
S2=temp+S2;
}
for(i=0;i<32;i++)//将S2进行一次置换
{
t=P[i];
temp=S2>>(32-t);
temp=temp&1;
S3=S3<<1;
S3=S3+temp;
}

S3=S3^L;//将置换后的结果与左半部分进行按位异或
return S3;
}
unsigned int *cipher(unsigned int *K,unsigned int *M0)
{
unsigned int temp,L,R,S3;int i,j,t;unsigned int *M1;M1=new unsigned int[2];
unsigned int *M11;M11=new unsigned int[2];
M1[0]=M1[1]=0;
char A[64]={58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7};
char B[64]={40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25};
for(i=0;i<64;i++)//IP置换
{
t=A[i]; j =(t-1)/32;//定位一个bit数据在IP置换后的位置
if(t<32)
{
temp=M0[j]>>(32-t);
temp=temp&1;
}
else
{
t=t-32;
temp=M0[j]>>(32-t);
temp=temp&1;
}
if(i<32)//将置换后的64个bit整合到两个无符号字符中
{
M1[0]=M1[0]<<1;
M1[0]=M1[0]+temp;
}
else
{

M1[1]=M1[1]<<1;
M1[1]=M1[1]+temp;

}
}
L=M1[0];R=M1[1];//将置换后的结果分成左右两部分,分别进行运算
for(i=0;i<16;i++)
{
S3=fk(K[2*i],K[2*i+1],L,R);//进行S盒运算
L=R;R=S3;//进行左右互换
}
M0[1]=L;M0[0]=R;
for(i=0;i<64;i++)//IP逆置换
{
t=B[i]; j =(t-1)/32;//定位一个bit数据在IP逆置换后的位置
if(t<32)
{
temp=M0[j]>>(32-t);
temp=temp&1;
}
else
{
t=t-32;
temp=M0[j]>>(32-t);
temp=temp&1;
}
if(i<32)//将置换后的64个bit整合到两个无符号字符中
//达到的就是密文
{
M1[0]=M1[0]<<1;
M1[0]=M1[0]+temp;
}
else
{

M1[1]=M1[1]<<1;
M1[1]=M1[1]+temp;

}
}
return M1;
}
unsigned int encipherfile(char *K00,char *Pname,char *Cname)
{ unsigned int *K;K=new unsigned int[32];int count;unsigned char readBuff[8];//变量声明
unsigned int temp;unsigned int M0[2]={0},*M1;M1=new unsigned int[2];unsigned int *K0;K0=new unsigned int[2];
time_t start, finish;double duration;
start=clock(); //开始计时
for(int i=0;i<8;i++)//将8个字符的密钥整合在两个unsigned int里面
{
temp = 0;
temp = (temp | K00[i]) << (3 - i % 4)*8;
K0[i/4] = K0[i/4] | temp;
}
K=key(K0);//生成子密钥
FILE * plaintextFile,* ciphertextFile;
if((ciphertextFile=fopen(Cname,"wb"))==NULL)//判断密文文件
{
printf("密文文件打开出错");
exit(0);
}
if((plaintextFile=fopen(Pname,"rb"))==NULL)//判断明文文件
{
printf("明文文件打开出错");
exit(0);
}
while(!feof(plaintextFile))
{
count = fread(readBuff,sizeof(char),8,plaintextFile); //逐个取出字符
if(count!=0)
{
if(count!=8)//判断是否取出了8个字符
{

for(int i = count;i<8;i++)
readBuff[i] = ' ';
}
M0[0] = M0[1] =0;//初始化M0
for(int i=0;i<8;i++)//将8个字符的密钥整合在两个unsigned int里面
{
temp = 0;
temp = (temp | readBuff[i]) << (3 - i % 4)*8;
M0[i/4] = M0[i/4] | temp;
}
M1=cipher(K,M0);
for(int i=0;i<8;i++)//将两个无符号字符分成8个字符
readBuff[i] = M1[i/4] >> (3 - i % 4)*8;
fwrite(readBuff,sizeof(char),8,ciphertextFile);//将字符逐个写入文件中
}
}

fclose(ciphertextFile);//关闭密文文件
fclose(plaintextFile);//关闭明文文件

printf("encipher is completed!\n");
finish=clock();//结束计时
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "算法执行时间:%f seconds\n", duration );
return 0;
}
unsigned int decipherfile(char *K00,char *Cname,char *Pname)
{ unsigned int *K;K=new unsigned int[32];unsigned int *K0;K0=new unsigned int[2];
int i;unsigned int temp;unsigned int M0[2]={0},*M1;M1=new unsigned int[2];int count;
unsigned char readBuff[8];
time_t start, finish;double duration;
start=clock();//开始计时
for(int i=0;i<8;i++)//将8个字符的密钥整合在两个unsigned int里面
{
temp = 0;
temp = (temp | K00[i]) << (3 - i % 4)*8;
K0[i/4] = K0[i/4] | temp;
}
K=key(K0);//生成子密钥
for(i=0;i<8;i++)
{
temp=K[2*i];K[2*i]=K[30-2*i];K[30-2*i]=temp;
temp=K[2*i+1];K[2*i+1]=K[31-2*i];K[31-2*i]=temp;
}
FILE * plaintextFile,* ciphertextFile;
if((ciphertextFile=fopen(Cname,"rb"))==NULL)//判断密文文件是否存在
{
printf("密文文件打开出错");
exit(0);
}
if((plaintextFile=fopen(Pname,"wb"))==NULL)//判断明文文件是否存在
{
printf("明文文件打开出错");
exit(0);
}
while(!feof(ciphertextFile))
{
count = fread(readBuff,sizeof(char),8,ciphertextFile); //逐个取出字符
if(count!=0)
{
if(count!=8)//判断是否取出了8个字符
{
for(int i = count;i<8;i++)
readBuff[i] = ' ';
}
M0[0] = M0[1] =0;
for(int i=0;i<8;i++)//将8个字符整合在2个unsigned int数据类型中
{
temp = 0;
temp = (temp | readBuff[i]) << (3 - i % 4)*8;
M0[i/4] = M0[i/4] | temp;
}
M1=cipher(K,M0);//对64位的数据进行加密
for(int i=0;i<8;i++)//将两个无符号字符分成8个字符
readBuff[i] = M1[i/4] >> (3 - i % 4)*8;
fwrite(readBuff,sizeof(char),8,plaintextFile);//将字符逐个写入文件中
}
}

fclose(ciphertextFile);//关闭密文文件
fclose(plaintextFile);//关闭明文文件
printf("decipher is completed!\n");
finish=clock();//结束计时
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "算法执行时间:%f seconds\n", duration );
return 0;
}
int main(int argc, char* argv[])
{

if(argc!=6)//判断是不是输入了6个参数
{
printf("input error!Format:\n");//报错,退出
printf("DES -e/-d -k key plainttextnanme ciphernametextname\n");exit(0);
}
if(strcmp(argv[1],"-e")==0||strcmp(argv[1],"-E")==0) //判断是否要加密
{
if(strcmp(argv[2],"-k")==0||strcmp(argv[2],"-K")==0)//判断是否输对了格式
{
if(strlen(argv[2])>=8)//判断米要长度是否够长
encipherfile(argv[3],argv[4],argv[5]);//调用函数对文件加密
else
{ printf("the length of the key is too short!");//报错,退出
printf("please input 8 char!\n");
exit(0);
}
}
else
{ printf("input error!Format:\n");//报错,退出
printf("cipher -e/-d -k key plainttextnanme ciphernametextname\n");
exit(0);
}
}
if(strcmp(argv[1],"-d")==0||strcmp(argv[1],"-D")==0) //判断是否要加密
{
if(strcmp(argv[2],"-k")==0||strcmp(argv[2],"-K")==0)//判断是否输对了格式
{
if(strlen(argv[2])>=8)//判断米要长度是否够长
decipherfile(argv[3],argv[4],argv[5]);//调用函数对文件加密
else
{ printf("the length of the key is too short!\n");//报错,退出
printf("please input 8 char!\n");
exit(0);
}
}
else
{ printf("input error!the Format is:\n");//报错,退出
printf("DES -e/-d -k key plainttextnanme ciphernametextname\n");

exit(0);
}
}
else
{
printf("input error!the Format is:\n");//报错,退出
printf("DES -e/-d -k key plainttextnanme ciphernametextname\n");
exit(0);//报错退出
}
getch();
return 0;
}

搜索更多相关的解决方案: DES  int  效率  unsigned  

----------------解决方案--------------------------------------------------------

我菜鸟水平,程序很乱。。。大侠忍一下哈


----------------解决方案--------------------------------------------------------
怎么没人帮我看一下啊
----------------解决方案--------------------------------------------------------
  相关解决方案