📄 encryptdlg.cpp
字号:
/*****************************************************************************
FileName : EncryptDlg.cpp
Author : GongHuiBin(devvy@21cn.com)
Date : 13/June/2003
******************************************************************************/
// EncryptDlg.cpp : implementation file
//
#include "stdafx.h"
#include "math.h"
#include "Encrypt.h"
#include "EncryptDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ENCRYPT_KEY 95
#define ENCRYPT_P 13
#define ENCRYPT_Q 15
//设置用于明键加密的两个质数
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEncryptDlg dialog
CEncryptDlg::CEncryptDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEncryptDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEncryptDlg)
m_Target = _T("");
m_Source = _T("");
m_Key = _T("");
m_Decode = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEncryptDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEncryptDlg)
DDX_Text(pDX, IDC_EDIT_TARGET, m_Target);
DDX_Text(pDX, IDC_EDIT_SOURCE, m_Source);
DDX_Text(pDX, IDC_EDIT_KEY, m_Key);
DDX_Text(pDX, IDC_EDIT_DECODE, m_Decode);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEncryptDlg, CDialog)
//{{AFX_MSG_MAP(CEncryptDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_ENCRYPT1, OnBtnEncrypt1)
ON_BN_CLICKED(IDC_BTN_ENCRYPT2, OnBtnEncrypt2)
ON_BN_CLICKED(IDC_BTN_ENCRYPT3, OnBtnEncrypt3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEncryptDlg message handlers
BOOL CEncryptDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CEncryptDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CEncryptDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CEncryptDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEncryptDlg::OnBtnEncrypt1()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if (m_Source.GetLength()<=0)
{
AfxMessageBox("对不起,加密源文不能为空!");
return;
}
if (m_Key.GetLength()<=0)
{
AfxMessageBox("对不起,加密键不能为空!");
return;
}
m_Target=Encrypt_Key(m_Source,m_Key);
//m_Title="算法:密键加密";
SetWindowText("算法:密键加密");
UpdateData(false);
}
/*******************************************************************
函数名称:Encrypt_Key
函数说明:明键加密算法
参数说明:
Source : 加密源文
Key : 加密键
返回结果:加密处理后的密文
********************************************************************/
CString CEncryptDlg::Encrypt_Key(CString Source,CString Key)
{
int iSourceLen,iKeyLen;
int iCount;
div_t div_Result;
char strTarget[255];
char pKey[255],pSource[255],pMid[255];
CString strTmp;
int i,j,n;
BYTE PWD_key[255];
j=0;
for (i=32;i<=126;i++)
{
PWD_key[i]=j;
j++;
}//取ASCII值在32~126之间的可视字符
iSourceLen=Source.GetLength();
iKeyLen=Key.GetLength();
div_Result=div(iSourceLen,iKeyLen);
if (div_Result.rem)
iCount=div_Result.quot+1;
else
iCount=div_Result.quot;
wsprintf(pKey,"%s",m_Key);
n=0;
for (i=1;i<=iCount;i++)
{
strTmp=Source.Mid((i-1)*iKeyLen,iKeyLen);
if (strTmp.GetLength()<iKeyLen)
{
for (j=1;j<=iKeyLen-strTmp.GetLength();j++)
strTmp+=" ";
}
wsprintf(pSource,"%s",strTmp);
for (j=1;j<=iKeyLen;j++)
{
int k1;
int k2;
k1=PWD_key[pKey[j-1]];
k2=PWD_key[pSource[j-1]];
int k=k1+k2;
div_Result=div(k,ENCRYPT_KEY);
k=div_Result.rem;
pMid[j-1]=k;
strTarget[n++]=k;
}//对源文进行替换加密处理
}
for (j=0;j<n;j++)
{
strTarget[j]+=32;
if (strTarget[j]==32) strTarget[j]='*'; //用“*”替代密文中的空格
}
strTarget[n]='\0';
CString strResult;
strResult.Format("%s",strTarget);
return(strResult);
}
DWORD Exp(int k,int e,int r)
{
DWORD dwResult;
div_t div_Result;
dwResult=k;
div_Result=div(dwResult,r);
dwResult=div_Result.rem;
for (int i=1;i<e;i++)
{
dwResult*=k;
div_Result=div(dwResult,r);
dwResult=div_Result.rem;
}
return(dwResult);
}
void CEncryptDlg::OnBtnEncrypt2()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if (m_Source.GetLength()<=0)
{
AfxMessageBox("对不起,加密源文不能为空!");
return;
}
m_Target=Encrypt_KnownKey(m_Source);
SetWindowText("算法:明键加密");
UpdateData(false);
}
CString CEncryptDlg::Encrypt_KnownKey(CString Source)
{
int r=ENCRYPT_P*ENCRYPT_Q;
int e=101;
//设置加密键,一般比P与Q大的质数就可以选作加密键
int k=(ENCRYPT_P-1)*(ENCRYPT_Q-1);
int d=5;
//求得解密键,即满足公式:(d*11) mod k=1
char pSource[255];
char pTarget[255];
int iLen;
int i;
DWORD dw1;
wsprintf(pSource,"%s",m_Source);
iLen=m_Source.GetLength();
for (i=0;i<iLen;i++)
{
div_t div_Result;
dw1=Exp(pSource[i],e,r);
div_Result=div(dw1,r);
pTarget[i]=div_Result.rem;
//获取密文
}
pTarget[iLen]='\0';
CString strTarget;
strTarget.Format("%s",pTarget);
return(strTarget);
}
CString CEncryptDlg::Decode_KnownKey(CString Source)
{
int r=ENCRYPT_P*ENCRYPT_Q;
int e=101;
//设置加密键,一般比P与Q大的质数就可以选作加密键
int k=(ENCRYPT_P-1)*(ENCRYPT_Q-1);//k=168
int d=5;
//求得解密键,即满足公式:(d*29) mod k=1
int iLen=m_Source.GetLength();
char pSource[255],pTarget[255];
wsprintf(pSource,"%s",m_Source);
for (int i=0;i<iLen;i++)
{
DWORD dw1;
dw1=Exp(pSource[i],d,r);
div_t div_Result;
div_Result=div(dw1,r);
pTarget[i]=div_Result.rem;
}
pTarget[iLen]='\0';
CString strTarget;
strTarget.Format("%s",pTarget);
return(strTarget);
}
void CEncryptDlg::OnBtnEncrypt3()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if (m_Source.GetLength()<=0)
{
AfxMessageBox("对不起,解密源文不能为空!");
return;
}
m_Decode=Decode_KnownKey(m_Source);
SetWindowText("算法:明键解密");
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -