#include<iostream>
#include"des.h"
#include"permutation.h"
using namespace std;
bool key_56[57];
bool key_48[49];
bool text_64[65];
bool text_48[49];
bool text_32[33];
void _copy( bool* from, bool* to, int size );
void display( bool* b, int size )
{
for( int i = 1; i<=size; i++ )
{
cout<<b[i];
}
}
//将64位明文作初始置换
void ini_per()
{
bool temp[TEXT_L+1];
for( int i = 1; i<=TEXT_L; i++ )
{
temp[ i ] = text_64[ IP[i] ];
}
for( int i = 1; i<=TEXT_L; i++ )
{
text_64[i] = temp[i];
}
}
//密钥置换
void key_per()
{
bool temp[KEY_L+1];
for( int i = 1; i<=KEY_L; i++ )
{
temp[ i ] = key_56[ KP[i]-KP[i]/8 ];
//cout<<KP[i]-KP[i]/8 <<endl;
}
for( int i = 1; i<=KEY_L; i++ )
{
key_56[i] = temp[i];
}
}
//左28位密码左移一位
void lkey_lshift( )
{
bool temp = key_56[1];
for( int i = 2; i <= KEY_L/2; i++ )
{
key_56[i-1] = key_56[i];
}
key_56[KEY_L/2] = temp;
}
//左28位密码右移一位
void lkey_rshift()
{
bool temp = key_56[KEY_L/2];
for( int i = KEY_L/2; i >= 2; i-- )
{
key_56[i] = key_56[i-1];
}
key_56[1] = temp;
}
//右28位密码左移一位
void rkey_lshift( )
{
bool temp = key_56[29];
for( int i = 30; i <= KEY_L; i++ )
{
key_56[i-1] = key_56[i];
}
key_56[KEY_L] = temp;
//display(key_56+28, 28);
//cout<<endl;
//system("pause");
}
//右28位密码右移一位
void rkey_rshift()
{
bool temp = key_56[KEY_L];
for( int i = KEY_L; i >= KEY_L/2+2; i-- )
{
key_56[i] = key_56[i-1];
}
key_56[KEY_L/2+1] = temp;
/*display(key_56+28, 28);
cout<<endl;*/
}
//加密过程中第n轮密钥移位
void encrypt_key_shift( int n )
{
for( int i = 0; i<KS[n]; i++ )
{
lkey_lshift();
rkey_lshift();
}
//cout<<n<<": ";
//display( key_56, 56 );
//cout<<endl;
}
//解密过程中第n轮密钥移位
void decrypt_key_shift( int n )
{
if( n == 1 )
return;
for( int i = 0; i<KS[n]; i++ )
{
lkey_rshift();
rkey_rshift();
}
//cout<<n<<": ";
//display( key_56, 56 );
//cout<<endl;
}
//密码压缩置换
void key_comp()
{
for( int i = 1; i<=COMMON_L; i++ )
{
key_48[i] = key_56[ KCP[i] ];
}
}
//加密过程中,得到第n轮密钥,保存在key_48数组中
void encrypt_get_key( int n )
{
//key_per();
encrypt_key_shift(n);
key_comp();
//cout<<n<<": ";
//display( key_48, 48 );
//cout<<endl;
}
//解密过程中,得到第n轮密钥,保存在key_48数组中
void decrypt_get_key( int n )
{
//key_per();
decrypt_key_shift(n);
key_comp();
//cout<<n<<": ";
//display( key_48, 48 );
//cout<<endl;
}
//扩展置换,将数据的右半部分从32位扩展为48位,结果保存在text_48中
void text_expan()
{
for( int i = 1; i<= COMMON_L; i++ )
{
text_48[i] = text_64[ EBOX[i] + 32 ];
}
}
//将text_48与key_48异或,结果保存在明文数组中
void exclusive()
{
for( int i = 1; i<COMMON_L; i++)
{
text_48[i] = ((key_48[i] == text_48[i]) ? 0 : 1);
}
}
//s盒置换,由text_48的48位生成32位,保存在text_64的右32位
void s_box_per()
{
for( int i = 0; i<8; i++ )
{
//取相应的位置的数
bool* temp1 = text_48+(i*6)+1;
int r = temp1[0]*2 + temp1[5];
int c = temp1[1]*8 + temp1[2]*4 + temp1[3]*2 + temp1[4];
int res = SBOX[i][(r-1)*16+(c-1)];
//将结果填text_64的右32位
bool* temp2 = &text_64[TEXT_L/2+1] + (i*4);
temp2[3] = bool( res%2 );
temp2[2] = bool( (res/2)%2 );
temp2[1] = bool( (res/4)%2 );
temp2[0] = bool( res/8 );
}
}
//P盒置换
void p_per()
{
bool temp[33];
for( int i = 1; i<=32; i++ )
{
temp[i] = text_64[ PBOX[i] +32 ];
}
for( int i = 1; i<= 32; i++ )
{
text_64[i+32] = temp[i];
}
}
//左32位与右32位异或,结果保存在左32位中
void exclusiveLR()
{
for( int i = 1; i<=32; i++ )
{
text_64[i] = ((text_64[i+32] == text_64[i]) ? 0 : 1);
}
}
//交换左右32位
void swap()
{
for( int i = 1; i<=32; i++ )
{
bool t = text_64[i+32];
text_64[i+32] = text_64[i];
text_64[i] = t;
}
}
//加密过程中第n轮置换
void encrypt_per( int n )
{
_copy( text_64 + 33, text_32+1, 32 );
encrypt_get_key(n);
text_expan();
exclusive();
s_box_per();
p_per();
exclusiveLR();
_copy( text_32+1, text_64+33, 32 );
if( n != 16 )
{
swap();
}
}
//解密过程中第n轮置换
void decrypt_per( int n )
{
_copy( text_64 + 33, text_32+1, 32 );
decrypt_get_key(n);
text_expan();
exclusive();
s_box_per();
p_per();
exclusiveLR();
_copy( text_32+1, text_64+33, 32 );
if( n != 16 )
{
swap();
}
}
//末置换
void end_per()
{
bool temp[TEXT_L+1];
for( int i = 1; i<=TEXT_L; i++ )
{
temp[ i ] = text_64[ EP[i] ];
}
for( int i = 0; i<TEXT_L; i++ )
{
text_64[i] = temp[i];
}
}
//DES加密过程
void encrypt()
{
//ini_per();
for( int i = 1; i<=16; i++ )
{
encrypt_per(i);
}
//end_per();
}
//DES解密过程
void decrypt()
{
//end_per();
for( int i = 1; i<=16; i++ )
{
decrypt_per(i);
}
//ini_per();
}
void _copy( bool* from, bool* to, int size )
{
for( int i = 0; i<size; i++ )
{
to[i] = from[i];
}
}
//des一轮加密过程
//输入参数:plain 保存明文的bool数组指针
//输入参数:key 保存密钥的bool数组指针
//输出参数:cypher 输出加密后的密文
void des_encrypt(
bool* plain,
bool* cypher,
bool* key
)
{
_copy( plain, text_64, 65 );
_copy( key, key_56, 57 );
encrypt();
_copy( text_64, cypher, 65);
}
//des一轮解密过程
//输入参数:cypher 保存密文的bool数组指针
//输入参数:key 保存密钥的bool数组指针
//输出参数:plain 输出解密后的明文
void des_decrypt(
bool* plain,
bool* cypher,
bool* key
)
{
_copy( cypher, text_64, 65 );
_copy( key, key_56, 57 );
decrypt();
_copy( text_64, plain, 65);
}