?? rsa_wangmindlg.cpp
字號:
// RSA_WANGMINDlg.cpp : implementation file
//
#include "stdafx.h"
#include "RSA_WANGMIN.h"
#include "RSA_WANGMINDlg.h"
#include "BigNumber.h"
#include <vector>
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
using namespace std;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CBigNumber P,Q,N,D,E;
int ready,jiami_flag=-1,yaoshi=0;
vector <int> kuai;//每一個數組意思存放每次加密后的密文的字節數
int edit_kuai=0;
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()
/////////////////////////////////////////////////////////////////////////////
// CRSA_WANGMINDlg dialog
CRSA_WANGMINDlg::CRSA_WANGMINDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRSA_WANGMINDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRSA_WANGMINDlg)
m_N = _T("");
m_P = _T("");
m_Q = _T("");
m_jinggao = _T("");
m_OUT = _T("");
m_D = _T("");
m_E = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRSA_WANGMINDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRSA_WANGMINDlg)
DDX_Control(pDX, IDC_PROGRESS, m_cProgress);
DDX_Control(pDX, IDC_JIEMI, m_JIEMI);
DDX_Control(pDX, IDC_EXIT, m_EXIT);
DDX_Control(pDX, IDC_JIAMI, m_JIAMI);
DDX_Control(pDX, IDC_BUTTON_YAOSHI, m_YAOSHI);
DDX_Control(pDX, IDC_BUTTON1_RAND, m_RAND);
DDX_Text(pDX, IDC_EDIT_N, m_N);
DDX_Text(pDX, IDC_EDIT2, m_P);
DDX_Text(pDX, IDC_EDIT3, m_Q);
DDX_Text(pDX, IDC_EDIT4, m_jinggao);
DDX_Text(pDX, IDC_EDIT_TIME, m_OUT);
DDX_Text(pDX, IDC_EDIT_D, m_D);
DDX_Text(pDX, IDC_EDIT_E, m_E);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRSA_WANGMINDlg, CDialog)
//{{AFX_MSG_MAP(CRSA_WANGMINDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1_RAND, OnButton1Rand)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_JIAMI, OnJiami)
ON_BN_CLICKED(IDC_JIEMI, OnJiemi)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_BUTTON_YAOSHI, OnButtonYaoshi)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRSA_WANGMINDlg message handlers
BOOL CRSA_WANGMINDlg::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 CRSA_WANGMINDlg::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 CRSA_WANGMINDlg::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 CRSA_WANGMINDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CRSA_WANGMINDlg::OnButton1Rand()
{
// TODO: Add your control notification handler code here
m_cProgress.SetPos(0);
m_cProgress.SetRange32(0,1000);
m_cProgress.SetStep(200);
m_cProgress.StepIt();
m_jinggao="";
CTime t0=CTime::GetCurrentTime();
SetCursor(LoadCursor(NULL, IDC_WAIT));
ready=1;
UpdateData(TRUE);
int len=8;
P.Mov(0);
Q.Mov(0);
N.Mov(0);
E.Mov(0);
m_P=_T("");
m_Q=_T("");
m_N=_T("");
m_E=_T("");
m_D=_T("");
P.GetPrime(len);
P.Put(m_P);
m_cProgress.StepIt();
Q.GetPrime(len);
Q.Put(m_Q);
m_cProgress.StepIt();
N.Mov(P.Mul(Q));
m_cProgress.StepIt();
N.Put(m_N);
CTime t1=CTime::GetCurrentTime();
CTimeSpan t=t1-t0;
m_OUT.Format("%d",t.GetTotalSeconds());
m_OUT+=" 秒";
SetCursor(LoadCursor(NULL, IDC_APPSTARTING));
m_cProgress.SetPos(5000);
UpdateData(FALSE);
}
void CRSA_WANGMINDlg::OnExit()
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();
}
void CRSA_WANGMINDlg::OnJiami()
{
// TODO: Add your control notification handler code here
vector <int> kuai_jiami;
CFile m_file;
CString m_strName;
m_jinggao="";
//////////////////////////
m_cProgress.SetPos(0);
m_cProgress.SetRange32(0,5000);
m_cProgress.SetStep(2);
/////////////////////////
unsigned int i,kuai_hao=0;
CBigNumber W,MIWEN,wangmin;
CString miwen_name,aa;
int k=0,num;
CString bb;
CTime t0;
if(ready==0)
{
m_jinggao=_T("請先隨機產生兩個素數!");
UpdateData(FALSE);
return;
}
if(yaoshi==0)
{
m_jinggao=_T("請先產生公鑰和私鑰!");
UpdateData(FALSE);
return;
}
CFileDialog openDlg(true,"Text Files(*.txt)| ",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Text Files(*.txt) | *.txt|");
if( openDlg.DoModal() )
{
m_strName = openDlg.GetPathName(); //獲得路徑名
if( !m_file.Open(m_strName,CFile::modeReadWrite | CFile::shareDenyWrite))
{
AfxMessageBox(_T("無法打開要加密的文件!"));
m_strName.Empty();
return;
}
UINT dwPart=(UINT) 8;
unsigned char pBuf[8];
UINT dwRead = 0; //dwRead是實際獲取的塊
CFile wrFile; //存放加密的文件
CString str;
CFileDialog storeDlg(false,"Text Files(*.txt) | *.txt|",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Text Files(*.txt) | *.txt|");
if( storeDlg.DoModal() )
{
miwen_name = storeDlg.GetPathName();
if( !wrFile.Open(miwen_name,CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite))
{
AfxMessageBox(_T("無法打開文件!"));
miwen_name.Empty();
return;
}
t0=CTime::GetCurrentTime();
SetCursor(LoadCursor(NULL, IDC_WAIT));
do{
m_cProgress.StepIt();
for(i=0;i<8;i++)
pBuf[i]=0;
dwRead = m_file.Read( pBuf, dwPart );//dwRead是實際獲取字節數,pBuf是緩存,dwPart是預計的字節數
W.Get(pBuf,dwRead); //W 是取出字符串后轉換成的大數
if(W.Cmp(N)>=0) // 確保被加密數據必須小于N
{
m_OUT=_T("加密信息大于N!");
UpdateData(FALSE);
return;
}
MIWEN.Mov(W.Mon(D,N)); //加密后放在MIWEN
//算出MIWEN的實際的比特長
k=MIWEN.m_nLength*32-32;//MIWEN的前m_nLength-1 組的比特長
num=MIWEN.m_ulValue[MIWEN.m_nLength-1];
while(num){num=num>>1;k++;} //算出MIWEN的最高的數組的實際的比特長
if (k%8==0)
kuai_jiami.push_back((k/8));///////////////////???????????????
else
kuai_jiami.push_back((k/8+1));
str="";
MIWEN.Putchar(str);
wrFile.Write((LPCTSTR)str,str.GetLength());//把加密后字符串寫回wrFile里,每次放實際取出來的字節數
}while(!(dwRead<dwPart));
m_cProgress.SetPos(5000);
m_OUT="";
CTime t1=CTime::GetCurrentTime();
CTimeSpan t=t1-t0;
m_OUT.Format("%d",t.GetTotalSeconds());
m_OUT+=" 秒";
UpdateData(FALSE);
wrFile.Close();//加密后的文件
m_file.Close();//源文件
}
}
SetCursor(LoadCursor(NULL, IDC_APPSTARTING));
AfxMessageBox("文件加密已經完成");
jiami_flag=0;
kuai=kuai_jiami;/////////////////////////////////????????????????
}
void CRSA_WANGMINDlg::OnJiemi()
{
// TODO: Add your control notification handler code here
m_cProgress.SetPos(0);
m_cProgress.SetRange32(0,5000);
m_cProgress.SetStep(3);
CFile m_file_jiemi;//待解密文件
CString m_jiemi;//待解密文件名
m_jinggao="";
CTime t0;
vector<int>::iterator theIterator;//存放每一次取出密文的長度
unsigned int kuai_hao=0;
CBigNumber W,JIEMI,wangmin;
if(jiami_flag!=0)
{
m_jinggao=_T("請先進行加密文件,然后再進行解密!");
UpdateData(FALSE);
return;
}
CFileDialog openDlg(true,"Text Files(*.txt)|(*.txt)|",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Text Files(*.txt)|*.txt|");
if( openDlg.DoModal() )
{
m_jiemi = openDlg.GetPathName();
if( !m_file_jiemi.Open(m_jiemi,CFile::modeReadWrite | CFile::shareDenyWrite))
{
AfxMessageBox(_T("無法打開文件!"));
m_jiemi.Empty();
return;
}
unsigned char pBuf[200];
UINT dwRead = 0; //dwRead是實際獲取的字節
CFile wrFile; //存放己解密的文件
CString strName; //己解密文件的文件名
CString str;
m_cProgress.StepIt();
CFileDialog storeDlg(false,"Text Files(*.txt)|*.txt|",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Text Files(*.txt)|*.txt|");
if( storeDlg.DoModal() )
{
strName = storeDlg.GetPathName();
if( !wrFile.Open(strName,CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite))
{
AfxMessageBox(_T("無法打開文件!"));
strName.Empty();
return;
}
theIterator = kuai.begin(); //////////////》/??????????????
t0=CTime::GetCurrentTime();
SetCursor(LoadCursor(NULL, IDC_WAIT));
m_cProgress.StepIt();
while( theIterator != kuai.end())//////////////////////////????????
{
m_cProgress.StepIt();
for(int i=0;i<200;i++)//////////////////?????????????
pBuf[i]=0;
dwRead = m_file_jiemi.Read( pBuf, (UINT)*theIterator);//dwRead是實際獲取字節,pBuf是緩存
theIterator++;/////////////////////////////????????
W.Get(pBuf,dwRead); //W 是取出字符串后轉換成的大數
str="";
JIEMI.Mov(W.Mon(E,N));
JIEMI.Putchar(str);
wrFile.Write((LPCTSTR)str,str.GetLength());
m_cProgress.StepIt();
};
m_cProgress.SetPos(5000);
m_OUT="";
CTime t1=CTime::GetCurrentTime();
CTimeSpan t=t1-t0;
m_OUT.Format("%d",t.GetTotalSeconds());
m_OUT+=" 秒";
UpdateData(FALSE);
wrFile.Close();//關閉解密后的文件
m_file_jiemi.Close();//關閉待解密文件
}
}
AfxMessageBox("解密文件完成");
SetCursor(LoadCursor(NULL, IDC_APPSTARTING));
}
void CRSA_WANGMINDlg::OnButtonYaoshi()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_cProgress.SetPos(0);
m_cProgress.SetRange32(0,5000);
m_cProgress.SetStep(1);
m_cProgress.StepIt();
m_jinggao="";
CTime t0=CTime::GetCurrentTime();
if (ready==0)
{
m_jinggao=_T("請先隨機產生兩個素數!");
UpdateData(FALSE);
return;
}
SetCursor(LoadCursor(NULL, IDC_WAIT));
yaoshi=1;
P.m_ulValue[0]--;
Q.m_ulValue[0]--;
P.Mov(P.Mul(Q));
D.Mov(0x10001);
m_D="65537";
E.Mov(D.Euc(P));
E.Put(m_E);
Q.m_ulValue[0]=0;
m_OUT="";
CTime t1=CTime::GetCurrentTime();
CTimeSpan t=t1-t0;
m_OUT.Format("%d",t.GetTotalSeconds());
m_OUT+=" 秒";
SetCursor(LoadCursor(NULL, IDC_APPSTARTING));
m_cProgress.SetPos(5000);
UpdateData(FALSE);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -