login.cpp
来自「《Visual C++视频技术方案宝典》配套光盘」· C++ 代码 · 共 165 行
CPP
165 行
// Login.cpp : implementation file
//
#include "stdafx.h"
#include "RSA.h"
#include "Login.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLogin dialog
CLogin::CLogin(CWnd* pParent /*=NULL*/)
: CDialog(CLogin::IDD, pParent)
{
//{{AFX_DATA_INIT(CLogin)
m_Name = _T("");
m_PassWord = _T("");
//}}AFX_DATA_INIT
}
void CLogin::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLogin)
DDX_Text(pDX, IDC_EDIT1, m_Name);
DDX_Text(pDX, IDC_EDIT2, m_PassWord);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLogin, CDialog)
//{{AFX_MSG_MAP(CLogin)
ON_BN_CLICKED(IDC_BUTTON1, OnOK)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLogin message handlers
void CLogin::OnInitADOConn()
{
try
{
//创建连接对象实例
m_pConnection.CreateInstance("ADODB.Connection");
//设置连接字符串
CString strConnect="DRIVER={Microsoft Access Driver (*.mdb)};\
uid=;pwd=;DBQ=shujuku.mdb;";
//使用Open方法连接数据库
m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
}
void CLogin::ExitConnect()
{
//关闭记录集和连接
if(m_pRecordset!=NULL)
m_pRecordset->Close();
m_pConnection->Close();
}
BOOL CLogin::HaveName(CString name, CString pow)
{
//连接数据库
OnInitADOConn();
CString sql;
sql.Format("select * from user where 用户名 = '%s' and 密码 = '%s'",name,pow);
//创建记录集指针对象实例
m_pRecordset.CreateInstance(__uuidof(Recordset));
//打开记录集
m_pRecordset->Open((_bstr_t)sql,m_pConnection.GetInterfacePtr(),adOpenDynamic,
adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
return TRUE;
else
return FALSE;
//断开数据库连接
ExitConnect();
}
void CLogin::OnOK()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_Name.IsEmpty() || m_PassWord.IsEmpty())
{
MessageBox("用户名或密码不能为空!");
return;
}
//选取素数p和q
int p = 43;
int q = 59;
//计算n
int n = p*q;
int cn = (p-1)*(q-1);
//选取b
int b = 5;
//选取a,使a*b%cn=1
int a = 1949;
//将明文以两个字符为一组进行分组,以00表示a,01表示b,03表示c,......
int len = m_PassWord.GetLength();
if (len %2 != 0)
{
m_PassWord += "0";
len += 1;
}
//存储明文的数字化格式
int iData[100];
int index = 0;
CString str;
for (int i = 0 ; i < len; i++,index++)
{
int one = m_PassWord[i]-97;
int two = m_PassWord[i+1]-97;
if (two < 10)
str.Format("%i0%i",one,two);
else
str.Format("%i%i",one,two);
iData[index] = atoi(str);
i++;
}
//对明文数字进行加密
//c= E(m) = m^b mod n m为明文数字
CString Encript = "";
for (i = 0 ; i < index ; i++)
{
iData[i] = ((UINT64) pow(iData[i],b)) % n;
if (i != index-1)
str.Format("%i",iData[i]);
else
str.Format("%i",iData[i]);
Encript += str;
}
BOOL result = HaveName(m_Name,Encript);
if(result)
CDialog::OnOK();
else
{
MessageBox("用户名或密码错误!");
return;
}
}
void CLogin::OnButton2()
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?