📄 rsaencrpdlg.cpp
字号:
// RSAencrpDlg.cpp : implementation file
//
#include "stdafx.h"
#include "RSAencrp.h"
#include "RSAencrpDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CRSAencrpDlg dialog
CRSAencrpDlg::CRSAencrpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRSAencrpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRSAencrpDlg)
m_loadpubkey = _T("");
m_loadplaintext = _T("");
m_cipherpath = _T("");
KEYLENGTH = 1024;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRSAencrpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRSAencrpDlg)
DDX_Text(pDX, IDC_EDIT1, m_loadpubkey);
DDX_Text(pDX, IDC_EDIT3, m_loadplaintext);
DDX_Text(pDX, IDC_EDIT4, m_cipherpath);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRSAencrpDlg, CDialog)
//{{AFX_MSG_MAP(CRSAencrpDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_LoadKey, OnLoadKey)
ON_BN_CLICKED(IDC_LoadPlaint, OnLoadPlaint)
ON_BN_CLICKED(IDC_LoadPlaint2, OnLoadPlaint2)
ON_BN_CLICKED(IDC_DoCrypt, OnDoCrypt)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRSAencrpDlg message handlers
BOOL CRSAencrpDlg::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 CRSAencrpDlg::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 CRSAencrpDlg::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 CRSAencrpDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CRSAencrpDlg::OnLoadKey()
{
// TODO: Add your control notification handler code here
UpdateData();
CFileDialog fileDlg(TRUE);
fileDlg.m_ofn.lpstrTitle="导入公钥";
fileDlg.m_ofn.lpstrFilter="Pub Files(*.txt)\0*.txt\0\0\0";
fileDlg.m_ofn.lpstrDefExt="txt";
if(IDOK==fileDlg.DoModal())
{
m_loadpubkey = fileDlg.GetPathName();
}
UpdateData(false);
}
void CRSAencrpDlg::OnLoadPlaint()
{
// TODO: Add your control notification handler code here
UpdateData();
CFileDialog fileDlg(TRUE);
fileDlg.m_ofn.lpstrTitle="导入明文";
fileDlg.m_ofn.lpstrFilter="Pub Files(*.txt)\0*.txt\0\0\0";
fileDlg.m_ofn.lpstrDefExt="txt";
if(IDOK==fileDlg.DoModal())
{
m_loadplaintext = fileDlg.GetPathName();
}
UpdateData(false);
}
void CRSAencrpDlg::OnLoadPlaint2()
{
// TODO: Add your control notification handler code here
UpdateData();
CFileDialog fileDlg(false);
fileDlg.m_ofn.lpstrTitle="保存密文";
fileDlg.m_ofn.lpstrFilter="txt Files(*.txt)\0*.txt\0\0\0";
fileDlg.m_ofn.lpstrDefExt="txt";
if(IDOK==fileDlg.DoModal())
{
m_cipherpath = fileDlg.GetPathName();
}
UpdateData(false);
}
void CRSAencrpDlg::OnDoCrypt()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_cipherpath.IsEmpty() || m_loadplaintext.IsEmpty() || m_loadpubkey.IsEmpty())
{
MessageBox("请输入路径");
return;
}
RSAkey prvkey(KEYLENGTH);
//导入公钥
fstream fout2 (m_loadpubkey,ios::in | ios::binary);
CString pBuf("");
int pos;
while(!fout2.eof())
{
fout2.read(pBuf.GetBuffer(0),5);
if(memcmp(pBuf, "PUBK|", 5) == 0)
{
pos = fout2.tellg();
break;
}
pBuf.ReleaseBuffer(0);
fout2.seekg(-4, ios::cur);
}
if(fout2.eof())
{
MessageBox("公钥格式错误");
return ;
}
char* pubklen1 = new char[4];
char* pubklen2 = new char[4];
memset(pubklen1, 0, 4);
memset(pubklen2, 0, 4);
int count1 = 0;
int count2 = 0;
fout2.read(pubklen1, 3);
fout2.read(pubklen2, 3);
count1 = atoi(pubklen1);
count2 = atoi(pubklen2);
char* pubkp = new char [count1+1];
char* pubkm = new char [count2+1];
memset(pubkp, 0, count1);
memset(pubkm, 0, count2);
fout2.read(pubkp, count1);
fout2.read(pubkm, count2);
LINT p = LINT(pubkp, 16);
LINT mod = LINT(pubkm, 16);
PKEYSTRUCT key;
key.mod = mod;
key.pubexp = p;
key.bitlen_mod = ld (key.mod);
key.bytelen_mod = key.bitlen_mod >> 3;
if ((key.bitlen_mod % 8) > 0)
{
++key.bytelen_mod;
}
RSApub pubkey(key);
delete []pubklen1;
delete []pubklen2;
delete []pubkp;
delete []pubkm;
fout2.close ();
//导入明文
CFile fp(m_loadplaintext, CFile::modeRead);
DWORD dw_length;
dw_length = fp.GetLength();
deskey = new char [dw_length+1];
memset(deskey, 0, dw_length+1);
fp.Read(deskey, dw_length);
fp.Close();
//加密
fstream fout1 (m_cipherpath,ios::out | ios::binary);
CString char_flag("CIPH|");
fout1.write(char_flag, char_flag.GetLength());
int LenEncryptionBlock = pubkey.keyvalue().bytelen_mod - 12;
////分组的长度最大为为mod-1-2-8
UCHAR* EncryptionBlock = new UCHAR[LenEncryptionBlock];
int round = dw_length/LenEncryptionBlock;
if( dw_length % LenEncryptionBlock != 0)
round++;
for( int i = 0; i < round; i++ )
{
memcpy(EncryptionBlock, deskey + i*LenEncryptionBlock, LenEncryptionBlock);
LINT CIPHER = pubkey.crypt(EncryptionBlock, LenEncryptionBlock);
CString m_lint2str(CIPHER.lint2str(16));
int count = m_lint2str.GetLength();
CString char_count("");
char_count.Format("%d", count);
fout1.write(char_count, char_count.GetLength());
fout1.write(m_lint2str, m_lint2str.GetLength());
}
MessageBox("加密成功");
// fout1 << cipher;
/* long count = strlen(cipher.lint2str(16)) ;
CString countlength("");
char* flag = "CIPH|";
countlength.Format("%s", flag);
CString countlength1("");
countlength1.Format("%d", count);
MessageBox(countlength);///////////
MessageBox(countlength1);///////////
/////////////////////
fout1.write(countlength, countlength.GetLength());
fout1.write(countlength1, countlength1.GetLength());
char aaa = '|';
fout1.write(&aaa, 1);
// fout1.write(cipher_mess, cipher_mess.GetLength());
fout1.write(stringcipher, count);
fout1.close ();
delete []deskey;
MessageBox("加密成功");*/
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -