?? desdlg.cpp
字號:
// DesDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Cipher.h"
#include "DesDlg.h"
#include "math.h"
#include <iostream>
#include<fstream>
#include<string>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDesDlg dialog
CDesDlg::CDesDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDesDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDesDlg)
m_strDesInput = _T("");
m_strDesCipherStream = _T("");
m_strDesPlaintStream = _T("");
m_strDesKey = _T("");
m_strDesKeyStream = _T("");
//}}AFX_DATA_INIT
}
void CDesDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDesDlg)
DDX_Text(pDX, IDC_DESINPUT, m_strDesInput);
DDX_Text(pDX, IDC_DESCIPHERSTREAM, m_strDesCipherStream);
DDX_Text(pDX, IDC_DESPLAINTSTREAM, m_strDesPlaintStream);
DDX_Text(pDX, IDC_DESKEY, m_strDesKey);
DDX_Text(pDX, IDC_KEYSTREAM1, m_strDesKeyStream);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDesDlg, CDialog)
//{{AFX_MSG_MAP(CDesDlg)
ON_COMMAND(IDM_EDIT_LOAD, OnEditLoad)
ON_COMMAND(IDM_EDIT_CLEAR, OnEditClear)
ON_COMMAND(IDM_EDIT_ENCIPHER, OnEditEncipher)
ON_COMMAND(IDM_EDIT_DECIPHER, OnEditDecipher)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDesDlg message handlers
void CDesDlg::OnEditLoad()
{
// TODO: Add your command handler code here
CFileDialog m_ldFile(TRUE);
if(m_ldFile.DoModal()==IDOK)
{
m_strDesInput=m_ldFile.GetFileName();
CFile file;
file.Open(m_strDesInput,CFile::modeRead);
char *pBuf;//存放數據的Buff
DWORD dwFileLen;//Buff的長度
dwFileLen=file.GetLength();//實際的長度
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;//結尾為零
file.Read(pBuf,dwFileLen);
m_strDesInput=pBuf;
UpdateData(FALSE);
file.Close();
}
}
void CDesDlg::OnEditClear() //清空窗口顯示的明文
{
// TODO: Add your command handler code here
m_strDesInput="";
UpdateData(FALSE);
}
void CDesDlg::ReadIN_Initial() //明文和密鑰的讀入處理
{
CString Plaint;
CString Key;
Plaint=m_strDesInput;
Key=m_strDesKey;
KeyLength=Key.GetLength();
if(Plaint.GetLength()%8!=0)
PlaintLength=Plaint.GetLength()+8-Plaint.GetLength()%8;
else
PlaintLength=Plaint.GetLength();
DesPlaintText=new char[PlaintLength+1];
DesPlaintText[PlaintLength]=0;
DesKey=new char[Key.GetLength()+1];
DesKey[Key.GetLength()]=0;
for(int i=0;i<Plaint.GetLength();i++)
DesPlaintText[i]=Plaint[i];
for(i=Plaint.GetLength();i<PlaintLength;i++)
DesPlaintText[i]=' ';
for(i=0;i<Key.GetLength();i++)
DesKey[i]=Key[i];
}
void CDesDlg::ReadIN_Key() //讀入密鑰供解密時用
{
CString Key;
Key=m_strDesKey;
KeyLength=Key.GetLength();
DesKey=new char[Key.GetLength()+1];
DesKey[Key.GetLength()]=0;
for(int i=0;i<Key.GetLength();i++)
DesKey[i]=Key[i];
}
void CDesDlg::Plaint_Key_Stream()
{
StreamUnion=new int[8];
PlaintStream=new int[PlaintLength*8];
KeyStream=new int[KeyLength*8];
for(int i=0;i<PlaintLength;i++)
{
StreamUnion=change(DesPlaintText[i]);
for(int j=0;j<8;j++)
PlaintStream[8*i+j]=StreamUnion[j];
}
for(i=0;i<KeyLength;i++)
{
StreamUnion=change(DesKey[i]);
for(int j=0;j<8;j++)
KeyStream[8*i+j]=StreamUnion[j];
}
//delete []StreamUnion;////////////////////////
}
void CDesDlg::Key_Stream()
{
StreamUnion=new int[8];
KeyStream=new int[KeyLength*8];
for(int i=0;i<KeyLength;i++)
{
StreamUnion=change(DesKey[i]);
for(int j=0;j<8;j++)
KeyStream[8*i+j]=StreamUnion[j];
}
}
void CDesDlg::Plaint_IPSolve(int *p)
{
InitialIP();
for(int i=0;i<64;i++)
{
if(i<32)
{
L[i]=p[IPArray[i]-1];
}
else
{
R[i%32]=p[IPArray[i]-1];
}
}
}
void CDesDlg::Key_PC1_Solve(int *p)//////one bit :the 33th
{
InitialPC();
int j=0;
for(int i=0;i<64;i++)
{
if((i+1)%8!=0)
{
if(j<28)
{
C[j]=p[PC1[j]-1];
}
else
{
D[j%28]=p[PC1[j]-1];
}
j++;
}
}
}
void CDesDlg::LeftShift(int *C, int *D, int n) //C,D左移n(n=1,2)位
{
int temp[28];
for(int i=0;i<28;i++)
temp[i]=C[i];
for(i=0;i<28;i++)
C[i]=temp[(i+n)%28];
for(i=0;i<28;i++)
temp[i]=D[i];
for(i=0;i<28;i++)
D[i]=temp[(i+n)%28];
}
void CDesDlg::Key_PC2_Solve(int *C, int *D) //左移后選擇PC2得到48位密鑰
{
int p[56];
for(int i=0;i<28;i++)
p[i]=C[i];
for(i=28;i<56;i++)
p[i]=D[i%28];
for(i=0;i<48;i++)
KeyUnion[i]=p[PC2[i]-1];
}
void CDesDlg::Produce_Key() //產生密鑰
{
for(int j=0;j<16;j++)
{
Key_PC1_Solve(KeyStream);
if(j==0||j==1||j==8||j==15)
LeftShift(C, D, 1);
else
LeftShift(C, D, 2);
Key_PC2_Solve(C, D);
for(int k=0;k<48;k++)
KeyArray[j][k]=KeyUnion[k]; //保存16組密鑰待用
}
}
void CDesDlg::Expand(int *R) //32位擴展到48位明文流
{
Initial_EBox();
for(int i=0;i<48;i++)
RUnion[i]=R[EBox[i]-1];
}
void CDesDlg::XOR(int *RUnion, int *KeyUnion) //異或E(R)和K
{
for(int i=0;i<48;i++)
XORArray[i]=RUnion[i]^KeyUnion[i];
}
void CDesDlg::XOR2(int *L, int *SBoxOut) //32位異或,結果存在L中
{
for(int i=0;i<32;i++)
L[i]=L[i]^SBoxOut[i];
}
void CDesDlg::SBoxOut_Solve(int *p) //48位轉32位,結果在SBoxOut[48]中
{
InitialSbox();
StreamUnion=new int[4];
for(int i=0;i<8;i++)/////////
{
int sum1=0;
int sum2=0;
int result=0;
for(int j=0;j<6;j++)
{
if(j==0)
sum1+=2*p[6*i+j];
else if(j==5)
sum1+=p[6*i+j];
else
sum2+=Pow(p[6*i+j],4-j);
}
result=SBoxArray[i][sum1][sum2];
StreamUnion=change2(result);
for(int k=0;k<4;k++)
SBoxOut[4*i+k]=StreamUnion[k];
}
}
void CDesDlg::SBox_P_Solve(int *p) //result is in p
{
InitialPBox();
int q[32];
for(int i=0;i<32;i++)
q[i]=p[i];
for(i=0;i<32;i++)
p[i]=q[PBox[i]-1];
}
void CDesDlg::XOR_Change(int *L, int *SBoxOut) //異或并交換左右兩邊L,R
{
int temp[32];
for(int i=0;i<32;i++)
temp[i]=R[i];
XOR2(L,SBoxOut);
for(i=0;i<32;i++)
R[i]=L[i];
for(i=0;i<32;i++)
L[i]=temp[i];
}
void CDesDlg::Inverse_IP_Solve(int *p)
{
InitialIP();
int temp[64];
for(int i=0;i<64;i++)
temp[i]=p[i];
for(i=0;i<64;i++)
p[i]=temp[InverseIPArray[i]-1];
}
void CDesDlg::OnEditEncipher()
{
// TODO: Add your command handler code here
UpdateData(TRUE);
ReadIN_Initial();
int count=0;
DesCipherStream=new int[PlaintLength*8];//存放密文流
if(KeyLength!=8)
MessageBox("你輸入的密鑰長度有錯誤!請重新輸入!");
else
{
Plaint_Key_Stream();
Produce_Key();//產生密鑰保存在KeyArray中
while(count<PlaintLength*8)
{
for(int j=0;j<64;j++)
PlaintUnion[j]=PlaintStream[count+j]; //每次取64位進行16次迭代
Plaint_IPSolve(PlaintUnion);
for(j=0;j<16;j++)
{
Expand(R);
XOR(RUnion, KeyArray[j]); //result is in XORArray[48]
SBoxOut_Solve(XORArray);
SBox_P_Solve(SBoxOut);
XOR_Change(L,SBoxOut);
for(int i=0;i<48;i++)
KeyStream[i]=KeyUnion[i]; //更新密鑰
for(i=0;i<32;i++)
PlaintUnion[i]=R[i];
for(i=32;i<64;i++)
PlaintUnion[i]=L[i%32];
}
Inverse_IP_Solve(PlaintUnion);
for(int i=0;i<64;i++) //保存密文流
DesCipherStream[count+i]=PlaintUnion[i];
count+=64;
}
Output();
OutputToWindow() ;
}
}
void CDesDlg::OnEditDecipher()
{
// TODO: Add your command handler code here
UpdateData(TRUE);//////////
ReadIN_Key();/////////
int count=0;
PlaintStream=new int[PlaintLength*8];//存放明文流
if(KeyLength!=8)
MessageBox("你輸入的密鑰長度有錯誤!請重新輸入!");
else
{
Key_Stream();
Produce_Key();//產生密鑰保存在KeyArray中
count=0;
while(count<PlaintLength*8)
{
for(int j=0;j<64;j++)
PlaintUnion[j]=DesCipherStream[count+j]; //每次取64位進行16次迭代
Plaint_IPSolve(PlaintUnion);
for(j=0;j<16;j++)
{
Expand(R);
XOR(RUnion, KeyArray[15-j]); //result is in XORArray[48],密鑰逆著用
SBoxOut_Solve(XORArray);
SBox_P_Solve(SBoxOut);
XOR_Change(L,SBoxOut);
for(int i=0;i<32;i++)
PlaintUnion[i]=R[i];
for(i=32;i<64;i++)
PlaintUnion[i]=L[i%32];
}
Inverse_IP_Solve(PlaintUnion);
for(int i=0;i<64;i++) //保存密文流
PlaintStream[count+i]=PlaintUnion[i];
count+=64;
}
Out_Plaint_To_Text();
}
}
void CDesDlg::Swap(int &x, int &y) //交換函數
{
int temp;
temp=x;
x=y;
y=temp;
}
void CDesDlg::Output() //結果輸出到文本中
{
ofstream out1("1.txt");
for(int i=0;i<PlaintLength/4;i++)
{
for(int j=0;j<32;j++)
{
out1<<DesCipherStream[32*i+j];
}
out1<<endl;
}
ofstream out2("2.txt");
for(i=0;i<PlaintLength/4;i++)
{
for(int j=0;j<32;j++)
{
out2<<PlaintStream[32*i+j];
}
out2<<endl;
}
ofstream out3("3.txt");
for(i=0;i<KeyLength/4;i++)
{
for(int j=0;j<32;j++)
{
out3<<KeyStream[32*i+j];
}
out3<<endl;
}
}
void CDesDlg::OutputToWindow() //內容顯示到窗口中
{
CFile file;
file.Open("1.txt",CFile::modeRead);
char *pBuf;//存放數據的Buff
DWORD dwFileLen;//Buff的長度
dwFileLen=file.GetLength();//實際的長度
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;//結尾為零
file.Read(pBuf,dwFileLen);
m_strDesCipherStream=pBuf;
UpdateData(FALSE);
file.Close();
file.Open("2.txt",CFile::modeRead);
dwFileLen=file.GetLength();//實際的長度
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;//結尾為零
file.Read(pBuf,dwFileLen);
m_strDesPlaintStream=pBuf;
UpdateData(FALSE);
file.Close();
file.Open("3.txt",CFile::modeRead);
dwFileLen=file.GetLength();//實際的長度
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;//結尾為零
file.Read(pBuf,dwFileLen);
m_strDesKeyStream=pBuf;
UpdateData(FALSE);
file.Close();
}
int * CDesDlg::change(int n) //數N轉換成對應的2進制流(8位)存到數組中
{
for(int j=0;j<8;j++)
NumicAlphaUnion[j]=0;
int count=7;
while(n!=0)
{
NumicAlphaUnion[count]=n%2;
count--;
n=n/2;
}
return NumicAlphaUnion;
}
int * CDesDlg::change2(int n) //數N轉換成對應的二進制流(4位)(SBox壓縮用)
{
for(int j=0;j<4;j++)
NumicAlphaUnion2[j]=0;
int count=3;
while(n!=0)
{
NumicAlphaUnion2[count]=n%2;
count--;
n=n/2;
}
return NumicAlphaUnion2;
}
int CDesDlg::Pow(int i, int n) //計算I的2的N次方的值
{
if(i==0)
return 0;
else if(n==0&& i!=0)
return 1;
else
return 2*Pow(i,n-1);
}
void CDesDlg::INT_To_CHAR(char *p, int *PlaintStream)
{
int count=0;
for(int i=0;i<PlaintLength;i++)
{
count=0;
for(int j=0;j<8;j++)
{
count+=Pow(PlaintStream[8*i+j],7-j);
}
p[i]=count;
}
}
void CDesDlg::InitialSbox() //初始化SBox
{
int p[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}
},
};
int i,j=0,z=0;
for(i=0;i<8;i++)
{
for(j=0;j<4;j++)
{
for(z=0;z<16;z++)
SBoxArray[i][j][z]=p[i][j][z];
}
}
}
void CDesDlg::InitialPBox() //初始化PBox
{
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
};
for(int i=0;i<32;i++)
{
PBox[i]=p[i];
}
}
void CDesDlg::InitialPC() //初始化PC1,PC2
{
int p[56]=
{
57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,33,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
};
for(int i=0;i<56;i++)
PC1[i]=p[i];
int q[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
};
for(i=0;i<48;i++)
PC2[i]=q[i];
}
void CDesDlg::InitialIP() //初始化IP和IP逆
{
int p[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
};
for(int i=0;i<64;i++)
IPArray[i]=p[i];
int q[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++)
InverseIPArray[i]=q[i];
}
void CDesDlg::Initial_EBox()
{
int p[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
};
for(int i=0;i<48;i++)
EBox[i]=p[i];
}
void CDesDlg::Out_Plaint_To_Text()
{
int count;
result=new char[PlaintLength+1];
result[PlaintLength]=0;
ofstream out4("Plaint.txt");
for(int i=0;i<PlaintLength;i++)
{
count=0;
for(int j=0;j<8;j++)
{
count+=Pow(PlaintStream[8*i+j],7-j);
}
result[i]=count;
out4<<result[i];
}
MessageBox("解密后得到的明文已經保存在Plaint.txt 文本文件中!"
"如果你以手動方式輸入明文的話,該文件在本程序的文件夾中;"
"如果你以載入文件的方式輸入明文的話,該文件在你載入文件的路徑文件夾中.");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -