?? des.cpp
字號:
/*********************************DES加密實驗************************************/
//////////////////////////////////////////////////////////////////////////////////
//
//Initial_Permutation:存放初始置換的表
//Final_Permutation:存放逆置換的表
//S_Box:S盒
//Expansion_Permutation:擴展置換
//Key_Permutation:密鑰置換
//Move_Bits1: 每輪移動的位數(shù)(用于加密過程)
//Move_Bits2: 每輪移動的位數(shù)(用于解密過程)
//Compression_Permutation:壓縮置換
//P_Box:p盒
//Plaintext:原文
//Ciphertext:密文
//Key:密鑰
//
//編寫日期: 2003.6.22
//作者:李德
//////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <bitset>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
typedef unsigned __int8 byte;
#define Encrypt 0
#define Decrypt 1
///////////////////////////////////////////////////////////////////////////////////
// 下面是DES所用到數(shù)據(jù)表的定義:
/* 初始置換表 */
static byte Initial_Permutation[] =
{
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
};
/* 末置換表 */
static byte Final_Permutation[] =
{
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
};
/* 擴展置換(將數(shù)據(jù)的右半部分從32位擴展到48位) */
static byte Expansion_Permutation[] =
{
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
};
/* S盒 */
static byte S_Box[8][64] =
{
/* S1 */
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,
/* S2 */
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,
/* S3 */
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,
/* S4 */
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,
/* S5 */
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,
/* S6 */
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,
/* S7 */
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,
/* S8 */
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
};
/* p盒 */
static byte P_Box[] =
{
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
};
/* 密鑰置換 */
static const byte Key_Permutation[] =
{
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
};
/* 壓縮置換 */
static const byte Compression_Permutation[] =
{
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
};
/* 每輪移動的位數(shù)(用于加密過程) */
static const unsigned int Move_Bits1[] =
{
1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
};
/* 每輪移動的位數(shù)(用于解密過程) */
static const unsigned int Move_Bits2[] =
{
0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
};
//數(shù)據(jù)表定義結(jié)束
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//函數(shù)定義
static bool isdetialprint=false;
/* 打印 */
void print64(bitset<64> bitvec)
{
for(int i=0;i<64;i++)
{
cout<<bitvec[i];
if((i+1)%8==0) cout<<" ";
}
cout<<endl;
};
void print56(bitset<56> bitvec)
{
for(int i=0;i<56;i++)
{
cout<<bitvec[i];
if((i+1)%8==0) cout<<" ";
}
cout<<endl;
};
void print48(bitset<48> bitvec)
{
for(int i=0;i<48;i++)
{
cout<<bitvec[i];
if((i+1)%8==0) cout<<" ";
}
cout<<endl;
};
void print32(bitset<32> bitvec)
{
for(int i=0;i<32;i++)
{
cout<<bitvec[i];
if((i+1)%8==0) cout<<" ";
}
cout<<endl;
};
void move_right(bitset<28> & bitvec,unsigned int i)
{
bitset<1> k;
for(int j=0;j<i;j++)
{
k[0]=bitvec[27];
bitvec<<=1;
bitvec[0]=k[0];
}
}
void move_left(bitset<28> & bitvec,unsigned int i)
{
bitset<1> k;
for(int j=0;j<i;j++)
{
k[0]=bitvec[0];
bitvec>>=1;
bitvec[27]=k[0];
}
}
/*分組并分別對左半部分和右半部分進行循環(huán)移位*/
void rotate(bitset<56> & bitvec,unsigned int num,unsigned int flag)
{
bitset<28> key_left,key_right;
int i;
/* 分組 */
for(i=0;i<28;i++)//將密鑰左半部分取出
key_left[i]=bitvec[i];
for(i=0;i<28;i++)//將密鑰右半部分取出
key_right[i]=bitvec[i+28];
/* 移位 */
if(flag==Encrypt)
{
move_left(key_left,Move_Bits1[num-1]);
move_left(key_right,Move_Bits1[num-1]);
}
else if(flag==Decrypt)
{
move_right(key_left,Move_Bits2[num-1]);
move_right(key_right,Move_Bits2[num-1]);
}
/* 合并 */
for(i=0;i<28;i++)
{
bitvec[i]=key_left[i];
bitvec[i+28]=key_right[i];
}
}
void DES_Function(bitset<64> &text1,bitset<64> &text2,bitset<64> Key,unsigned int flag)
{
unsigned int i,j,k;
int row,col;
byte temp;
bitset<64> text64;
bitset<56> key56;
bitset<32> text32_left[17],text32_right[17],S_result,P_result;
bitset<48> text48,key48,textXorKey;
bitset<2> S_box2bits;
bitset<4> S_box4bits;
bitset<6> S_box6bits;
/* 初始置換 */
for(i=0;i<64;i++)
text64[i]=text1[Initial_Permutation[i]-1];
if(isdetialprint)
{
cout<<"IP:";
print64(text64);
cout<<endl;
}
/* 密鑰置換 */
/* 去掉奇偶校驗位,將密鑰從64位變到56位*/
for(i=0;i<56;i++)
key56[i]=Key[Key_Permutation[i]-1];
/* 對明文分組 */
for(i=0;i<32;i++)
{
text32_left[0][i]=text64[i];//將明文左半部分取出
text32_right[0][i]=text64[i+32];//將明文右半部分取出
}
/*f函數(shù)的實現(xiàn)*/
for(i=1;i<17;i++)
{
/* 擴展置換,將數(shù)據(jù)的右半部分R[i]從32位擴展到48位 */
if(isdetialprint)
{
cout<<"第"<<i<<"輪:"<<endl;
}
for(j=0;j<48;j++)
text48[j]=text32_right[i-1][Expansion_Permutation[j]-1];
if(isdetialprint)
{
cout<<"E(R"<<i-1<<")=";
print48(text48);
}
/* 對密鑰分組 */
rotate(key56,i,flag);//分組并分別對左半部分和右半部分移位
/* 壓縮置換 */
for(j=0;j<48;j++)
key48[j]=key56[Compression_Permutation[j]-1];
if(isdetialprint)
{
cout<<"K"<<i<<"=";
print48(key48);
}
/* 將48位的密鑰和48位的明文的右半部分進行異或 */
for(j=0;j<48;j++)
textXorKey[j]=key48[j]^text48[j];
if(isdetialprint)
{
cout<<"E(R"<<i-1<<")⊕K"<<i<<"=";
print48(textXorKey);
}
/*S盒替換*/
for(j=0;j<8;j++)
{
for(k=0;k<6;k++)
S_box6bits[k]=textXorKey[j*6+k];
for(k=1;k<5;k++)
S_box4bits[k-1]=S_box6bits[5-k];
S_box2bits[0]=S_box6bits[5];
S_box2bits[1]=S_box6bits[0];
row=S_box2bits.to_ulong();
col=S_box4bits.to_ulong();
temp=S_Box[j][row*16+col];
for(k=0;k<4;k++)
{
S_result[j*4+3-k]=temp&1;
temp>>=1;
}
}//end of S盒替換
if(isdetialprint)
{
cout<<"S_Box is:";
print32(S_result);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -