这个des算法错在哪
写了一个des算法,请高手帮我看看
#include<iostream>
using namespace std;
void getSubKey( unsigned char (*subK)[48], unsigned char* k );
void des( unsigned char* m );
void _des( unsigned char* m );
unsigned char k[]="program";
int main() {
unsigned char m[]="computer";
unsigned char subK[16][48];
getSubKey(subK,k);
/*cout<<"子密钥:"<<endl;
for ( int i = 0; i < 48; ++i ) {
cout<<int(subK[0][i]);
if ((i+1) % 8 == 0 ) cout<<endl;
}*/
//des(m);
cout<<m<<endl;
des(m);
cout<<m<<endl;
_des(m);
cout<<m<<endl;
system("pause");
}
void getSubKey( unsigned char (*subK)[48], unsigned char* k ) {
unsigned char a[56];
unsigned char c[28],d[28];
unsigned char LS[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
unsigned char PC_1[] = {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 };
unsigned char PC_2[] = {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 };
for ( int i = 0; i < 56; ++i )
{ if ( PC_1[i] < 8 ) continue;
else if ( PC_1[i] < 16 ) PC_1[i] -= 1;
else if ( PC_1[i] < 24 ) PC_1[i] -= 2;
else if ( PC_1[i] < 32 ) PC_1[i] -= 3;
else if ( PC_1[i] < 40 ) PC_1[i] -= 4;
else if ( PC_1[i] < 48 ) PC_1[i] -= 5;
else if ( PC_1[i] < 56 ) PC_1[i] -= 6;
else if ( PC_1[i] < 64 ) PC_1[i] -= 7;
}
for ( int i = 0; i < 7; ++i )
for ( int j = 0; j < 8; ++j )
a[8*i+j] = (k[i]>>7-j) & 1;
cout<<"program:"<<endl;
for ( int i = 0; i < 7; ++i )
{ for ( int j = 0; j < 8; ++j )
cout<<int(a[8*i+j]);
cout<<endl;} //test miwen
for ( int i = 0; i < 28; ++i )
c[i] = a[ PC_1[i]-1 ];
cout<<"C0:"<<endl;
for ( int i = 0; i < 28; ++i )
{ cout<<int(c[i]);
if ( (i+1) % 8 == 0 ) cout<<" ";
}
cout<<endl; //test c0
system("pause");
for ( int i = 28; i < 56; ++i )
d[ i-28 ] = a[ PC_1[i]-1 ]; //PC_1
cout<<"D0:"<<endl;
for ( int i = 0; i < 28; ++i )
{ cout<<int(d[i]);
if ( (i+1) % 8 == 0 ) cout<<" ";
}
cout<<endl; //test d0
system("pause");
/*cout<<"a[3]="<<int(a[3])<<endl;
for ( int j = 0; j < 28; ++j )
cout<<int(d[j])<<" ";
cout<<endl; //test d[28]*/
for ( int i = 0; i < 16; ++i ) {
if ( LS[i] == 1 ) {
unsigned tmp = c[0];
for ( int j = 0; j < 27; ++j )
c[j] = c[j+1];
c[27] = tmp;
/*for ( int j = 0; j < 28; ++j )
cout<<int(c[j])<<" ";
cout<<endl;*/
tmp = d[0];
for ( int j = 0; j < 27; ++j )
d[j] = d[j+1];
d[27] = tmp;
}
else {
unsigned char tmp1 = c[0];
unsigned char tmp2 = c[1];
for ( int j = 0; j < 26; ++j )
c[j] = c[j+2];
c[26] = tmp1;
c[27] = tmp2;
tmp1 = d[0];
tmp2 = d[1];
for ( int j = 0; j < 26; ++j )
d[j] = d[j+2];
d[26] = tmp1;
d[27] = tmp2;
}
for ( int i = 0; i < 28; ++i )
a[i] = c[i];
for ( int i = 28; i < 56; ++i )
a[i] = d[ i-28 ];
/*for ( int i = 0 ; i< 28; ++i )
{ cout<<int(c[i])<<" ";
if ( (i+1) % 8 == 0 ) cout<<endl;
}
system("pause");
*/
for ( int j = 0; j < 48; ++j )
subK[i][j] = a[ PC_2[j]-1 ]; //PC_2
}
} //getSubKey