📄 dlg_caesar.cpp
字号:
// Dlg_Caesar.cpp : implementation file
//
#include "stdafx.h"
#include "Decode.h"
#include "Dlg_Caesar.h"
#include "time.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Dlg_Caesar dialog
Dlg_Caesar::Dlg_Caesar(CWnd* pParent /*=NULL*/)
: CDialog(Dlg_Caesar::IDD, pParent)
{
//{{AFX_DATA_INIT(Dlg_Caesar)
m_s = 0;
m_t = 0;
m_sc = _T("");
m_sp = _T("");
m_cfpath = _T("");
m_pfpath = _T("");
m_time = 0;
//}}AFX_DATA_INIT
}
void Dlg_Caesar::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Dlg_Caesar)
DDX_Control(pDX, IDC_OPEN_PLAIN_BUTTON, m_openp);
DDX_Control(pDX, IDC_OPEN_CIPHER_BUTTON, m_openc);
DDX_Control(pDX, IDC_STATIC_FP, m_fp);
DDX_Control(pDX, IDC_STATIC_SC, m_ssc);
DDX_Control(pDX, IDC_STATIC_SP, m_ssp);
DDX_Control(pDX, IDC_STATIC_String, m_string);
DDX_Control(pDX, IDC_STATIC_File, m_file);
DDX_Control(pDX, IDC_STATIC_FC, m_fc);
DDX_Control(pDX, IDC_PLAIN_EDIT, m_PFP);
DDX_Control(pDX, IDC_CIPHER_EDIT, m_CFP);
DDX_Control(pDX, IDC_EDIT_SC, m_SC);
DDX_Control(pDX, IDC_EDIT_SP, m_SP);
DDX_Radio(pDX, IDC_RADIO_String, m_s);
DDX_Radio(pDX, IDC_RADIO_B, m_t);
DDX_Text(pDX, IDC_EDIT_SC, m_sc);
DDX_Text(pDX, IDC_EDIT_SP, m_sp);
DDX_Text(pDX, IDC_CIPHER_EDIT, m_cfpath);
DDX_Text(pDX, IDC_PLAIN_EDIT, m_pfpath);
DDX_Text(pDX, IDC_EDIT_Time, m_time);
//}}AFX_DATA_MAP
}
BOOL Dlg_Caesar::OnInitDialog()
{
CDialog::OnInitDialog();
Dict.Setdict();
return TRUE;
}
BEGIN_MESSAGE_MAP(Dlg_Caesar, CDialog)
//{{AFX_MSG_MAP(Dlg_Caesar)
ON_BN_CLICKED(IDC_BUTTON_Exit, OnButtonEXIT)
ON_BN_CLICKED(IDC_RADIO_String, OnRADIOString)
ON_BN_CLICKED(IDC_RADIO_File, OnRADIOFile)
ON_BN_CLICKED(IDC_OPEN_CIPHER_BUTTON, OnOpenCipherButton)
ON_BN_CLICKED(IDC_OPEN_PLAIN_BUTTON, OnOpenPlainButton)
ON_BN_CLICKED(IDC_DECIPHER_BUTTON, OnDecipherButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Dlg_Caesar message handlers
void Dlg_Caesar::OnButtonEXIT()
{
// TODO: Add your control notification handler code here
CDialog::OnOK();
}
void Dlg_Caesar::OnRADIOString()
{
// TODO: Add your control notification handler code here
m_s=0;
m_ssc.EnableWindow(true);
m_ssp.EnableWindow(true);
m_SC.EnableWindow(true);
m_SP.EnableWindow(true);
m_string.EnableWindow(true);
m_file.EnableWindow(false);
m_PFP.EnableWindow(false);
m_CFP.EnableWindow(false);
m_fp.EnableWindow(false);
m_fc.EnableWindow(false);
m_openc.EnableWindow(false);
m_openp.EnableWindow(false);
UpdateData(true);
m_pfpath="";
m_cfpath="";
UpdateData(false);
}
void Dlg_Caesar::OnRADIOFile()
{
// TODO: Add your control notification handler code here
m_s=1;
m_ssc.EnableWindow(false);
m_ssp.EnableWindow(false);
m_SC.EnableWindow(false);
m_SP.EnableWindow(false);
m_string.EnableWindow(false);
m_file.EnableWindow(true);
m_PFP.EnableWindow(true);
m_CFP.EnableWindow(true);
m_fp.EnableWindow(true);
m_fc.EnableWindow(true);
m_openc.EnableWindow(true);
m_openp.EnableWindow(true);
UpdateData(true);
m_sp="";
m_sc="";
UpdateData(false);
}
void Dlg_Caesar::OnOpenCipherButton()
{
// TODO: Add your control notification handler code here
UpdateData(true);
file.Path(m_cfpath);
UpdateData(false);
}
void Dlg_Caesar::OnOpenPlainButton()
{
// TODO: Add your control notification handler code here
UpdateData(true);
file.Path(m_pfpath);
UpdateData(false);
}
bool Dlg_Caesar::Search(char *x)
{
int lr,left=0,right;
lr=strlen(x);
right=Dict.num[lr-1]-1;
while(left<=right)
{
int mid=(left+right)/2;
if(strcmp(x,Dict.c[lr-1][mid])==0)
return true;
else if(strcmp(x,Dict.c[lr-1][mid])>0)
left=mid+1;
else
right=mid-1;
}
return false;
}
char * Dlg_Caesar::DeCipher(char *x,int k)
{
char *plain;
int i,lr;
lr=strlen(x);
plain=new char[lr+1];
for(i=0;i<lr;i++)
plain[i]='a'+(x[i]-'a'-k+26)%26;
plain[lr]='\0';
return plain;
}
void Dlg_Caesar::OnDecipherButton()
{
// TODO: Add your control notification handler code here
double t,max=0;
bool fear=true;
int Max,lr,i=0,j,n,num=0,s=0,v,w,k=0,apha[26],
mod[26]={4,19,0,14,8,13,18,7,17,3,11,2,20,12,22,5,6,24,15,1,21,10,9,23,16,25};
//启发式搜索时遵循的词频次序e,t,a,o,i,n,s,h,r,d,l,c,u,m,w,f,g,y,p,b,v,k,j,x,q,z
char **x;
clock_t start,finish;
start=clock();
x=new char * [300];
for(i=0;i<300;i++)
x[i]=new char[20];
UpdateData(true);
m_cipher="";
if(m_s==0){
m_plain="";
m_cipher=m_sc;
}
else
{
if(!file.Fread(m_cipher,m_cfpath))
return;
}
i=0;
m_plain="";
n=strlen(m_cipher);
while(i<n) //从输入文本中分解出单词
{
if(fear&&isalpha(m_cipher[i])){
num++;
x[k][s]='\0';
s=0;
k++;
strcpy(x[k],"");
x[k][s]=tolower(m_cipher[i]);
s++;
fear=false;
}
else if(isalpha(m_cipher[i])){
x[k][s]=tolower(m_cipher[i]);
s++;
}
else{
fear=true;
}
i++;
}
x[k][s]='\0';
if(m_t==0){//从1到26盲目搜索
for(j=1;j<=26;j++)
{
v=0;
for(k=1;k<=num;k++){
if(Search(DeCipher(x[k],j)))
v++;
}
t=1.0*v/num;
if(t>max)
{
max=t;
key=j;
if(max>0.6)
break;
}
}
}
else{//根据词频采用启发式搜索
memset(apha,0,sizeof(apha));
Max=0;
for(i=1;i<=num;i++)
{
lr=strlen(x[i]);
for(j=0;j<lr;j++)
apha[x[i][j]-'a']++;
}
for(j=0;j<26;j++){
if(apha[j]>Max)
{
Max=apha[j];
w=j;
}
}
for(j=1;j<=26;j++)
{
v=0;
for(k=1;k<=num;k++){
key=(26+w-mod[j-1]);
if(Search(DeCipher(x[k],key)))
v++;
}
t=1.0*v/num;
if(t>max)
{
max=t;
if(max>0.6)
break;
}
}
}
if(max<=0.5){
m_plain="";
UpdateData(false);
AfxMessageBox("所给信息不足!");
return ;
}
for(i=0;i<=n-1;i++)
{
if(m_cipher[i]>='a'&&m_cipher[i]<='z')
m_plain+='a'+(m_cipher[i]-'a'-key+26)%26;
else if(m_cipher[i]>='A'&&m_cipher[i]<='Z')
m_plain+='A'+(m_cipher[i]-'A'-key+26)%26;
else
m_plain+=m_cipher[i];
}
if(m_s==0)
{
m_sp=m_plain;
}
else
{
if(!file.Fwrite(m_plain,m_pfpath))
return;
}
finish=clock();
m_time=finish-start;
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -