📄 creatersakeydlg.cpp
字号:
// CreateRSAKeyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CreateRSAKey.h"
#include "CreateRSAKeyDlg.h"
#include "rsakey.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCreateRSAKeyDlg dialog
CCreateRSAKeyDlg::CCreateRSAKeyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCreateRSAKeyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCreateRSAKeyDlg)
m_savepubkey = _T("");
m_saveprvkey = _T("");
//}}AFX_DATA_INIT
m_savepubkey = _T("");
m_saveprvkey = _T("");
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCreateRSAKeyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCreateRSAKeyDlg)
DDX_Text(pDX, IDC_EDIT1, m_savepubkey);
DDX_Text(pDX, IDC_EDIT2, m_saveprvkey);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCreateRSAKeyDlg, CDialog)
//{{AFX_MSG_MAP(CCreateRSAKeyDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SavePub, OnSavePub)
ON_BN_CLICKED(IDC_SavePrv, OnSavePrv)
ON_BN_CLICKED(IDC_CreateKey, OnCreateKey)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCreateRSAKeyDlg message handlers
BOOL CCreateRSAKeyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
}
// 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 CCreateRSAKeyDlg::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 CCreateRSAKeyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCreateRSAKeyDlg::OnSavePub()
{
// 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_pub = fileDlg.GetPathName();
// m_savepubkey = m_pub;
m_savepubkey = fileDlg.GetPathName();
}
UpdateData(false);
}
void CCreateRSAKeyDlg::OnSavePrv()
{
// 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_saveprvkey = fileDlg.GetPathName();
}
UpdateData(false);
}
void CCreateRSAKeyDlg::OnCreateKey()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_saveprvkey == "")
// m_saveprvkey = "prvkey.txt";
{
MessageBox("请输入保存私钥的路径");
return;
}
if(m_savepubkey == "")
//m_savepubkey = "pubkey.txt";
{
MessageBox("请输入保存公钥的路径");
return;
}
int KEYLENGTH = 1024;
initrand64_lt();
initrandBBS_lt();
RSAkey my_key (KEYLENGTH);
RSApub my_public_key (my_key);
fstream fout1 (m_saveprvkey,ios::out | ios::binary );
CString s("PRIK|");
fout1.write(s, s.GetLength());
// fout1 << my_key;
// fout1.close ();
CString s1(my_key.keyvalue().p.lint2str(16));
CString s2(my_key.keyvalue().q.lint2str(16));
CString s3(my_key.keyvalue().pubexp.lint2str(16));
CString s4(my_key.keyvalue().prvexp.lint2str(16));
CString count1length("");
int count1 = s1.GetLength();
count1length.Format("%d", count1);
CString count2length("");
int count2 = s2.GetLength();
count2length.Format("%d", count2);
CString count3length("");
int count3 = s3.GetLength();
count3length.Format("%d", count3);
CString count4length("");
int count4 = s4.GetLength();
count4length.Format("%d", count4);
fout1.write(count1length, count1length.GetLength());
fout1.write(count2length, count2length.GetLength());
fout1.write(count3length, count3length.GetLength());
fout1.write(count4length, count4length.GetLength());
fout1.write(s1, count1);
fout1.write(s2, count2);
fout1.write(s3, count3);
fout1.write(s4, count4);
fout1.close ();
fstream fout2 (m_savepubkey,ios::out | ios::binary );
CString t("PUBK|");
CString t1(my_public_key.keyvalue().pubexp.lint2str(16));
CString t2(my_public_key.keyvalue().mod.lint2str(16));
CString count5length("");
int count5 = t1.GetLength();
count5length.Format("%d", count5);
CString count6length("");
int count6 = t2.GetLength();
count6length.Format("%d", count6);
fout2.write(t, t.GetLength());
fout2.write(count5length, count5length.GetLength());
fout2.write(count6length, count6length.GetLength());
fout2.write(t1, t1.GetLength());
fout2.write(t2, t2.GetLength());
fout2.close ();
MessageBox("successful");
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -