VC实现数据的加密和解密

VC实现数据的加密和解密(MD5加密\DES\RSA(2)[refer=8,canydice] 我想问一下怎么编写filebuffer.h呢?用MD5加密的算法里?[refer=1,abfsd]主要的程序如下:\r\n1)、MD...[/refer] 我想问一下怎么编写filebuffer.h呢?用MD5加密的算法里? 回复1楼abfsd发表: 主要的程序如下:\r\n1)、MD5\r\n\r\n// MD5.h: interface for the CMD5 class.\r\n//\r\n////////////////////////... 7楼 发表://对字符串或者是16进制数据进行[refer=7,abfsd]//对字符串或者是16进制数据进行加密或者解密\r\nvoid CEncryAndDecryptDlg::OnBtnstringdigest() \r\n{\r\n // T...[/refer] //对字符串或者是16进制数据进行加密或者解密
void CEncryAndDecryptDlg::OnBtnstringdigest()
{
// TODO: Add your control notification handler code here
//判别算法为加密算法还是解密算法
CButton* poAction = (CButton*)(GetDlgItem(IDC_RADENCRYPT));
CEdit* poEdit;
//获取单选框的值,用于区分为字符串还是16进制数据
CButton* poButton = (CButton*)(GetDlgItem(IDC_RADALPHA));
//确定选择的方法
/*switch(m_iMethod)
{
case DES:
AfxMessageBox("采用DES算法");
break;
case MD5:
AfxMessageBox("采用MD5算法");
break;
case RSA:
AfxMessageBox("采用RSA算法");
break;
}*/
CString strTemp,strPrimeP,strPrimeQ;//用于获取EDIT中输入的信息
char acDigest[65] = {0};
int iLen;
//判别为加密算法还是解密算法
if(poAction->GetCheck() == 1)//为加密算法
{
m_iAction = ENCRYPT;
//判别为字符串还是为16进制形式
if(poButton->GetCheck() == 1)//为字符串形式
{
poEdit = (CEdit*)(GetDlgItem(IDC_EDITALPHA));
poEdit->GetWindowText(strTemp);
if(m_iMethod == DES && strTemp.GetLength() != 8)
{
AfxMessageBox("输入的加密的字符串长度不是8位,请重试!");
return;
}
m_oMD5.Reset();
m_oMD5.AddData(LPCTSTR(strTemp),strTemp.GetLength());
m_oMD5.FinalDigest(acDigest);
}
else//为16进制形式
{
poEdit = (CEdit*)(GetDlgItem(IDC_EDITHEX));
poEdit->GetWindowText(strTemp);
iLen = strTemp.GetLength()/2;
char* pcData = static_cast(_alloca(iLen));
Hex2Binary(LPCTSTR(strTemp), reinterpret_cast(pcData), iLen);
m_oMD5.Reset();
m_oMD5.AddData(pcData, iLen);
m_oMD5.FinalDigest(acDigest);
}
//下面就是实现将输出的结果显示在EDIT里面就可以了
char acHex[129] = {0};
poEdit = (CEdit*)(GetDlgItem(IDC_EDITHEX2));
CString strKey,strResult;
CStatic* poStatic;
int iLength,i,iPrimeP = 0,iPrimeQ = 0,iSubkey = 0;
switch(m_iMethod)
{
case DES:
CEdit* poEditKey;
poEditKey = (CEdit*)GetDlgItem(IDC_EDITDES);
poEditKey->GetWindowText(strKey);
//判断密钥的长度是否为8位
if(strKey.GetLength() != 8)
{
AfxMessageBox("输入的密钥长度不是8位,请重试!");
return;
}
//判别里面时候存在中文字符
for(i = 0;i< 8;i++)
{
//重字符串中获取第i个字符
char ch =

strKey.GetAt(i);
//判别是否存在非法字符
if(ch&0x80)
{
AfxMessageBox("含有中文字符");
return;
}
}
//返回的加密结果
strResult = m_oDES.Encrypt(strTemp,strKey);
poEdit = (CEdit*)GetDlgItem(IDC_EDITSTR2);
poEdit->SetWindowText(strResult);
//将上面获取的字符串数据转化为16进制数据
Binary2Hex(reinterpret_cast(strResult.GetBuffer(0)),16,acHex);
poEdit = (CEdit*)GetDlgItem(IDC_EDITHEX2);
//显示结果
poEdit->SetWindowText(acHex);
//在IDC_LBLSTR上面显示刚刚加密的内容
poStatic = (CStatic*)GetDlgItem(IDC_LBLSTR);
poStatic->SetWindowText("DES加密的明文为:"+strTemp);
//同时将上面的明文去除
poEdit = (CEdit*)GetDlgItem(IDC_EDITALPHA);
poEdit->SetWindowText("");
break;
case MD5:
Binary2Hex(reinterpret_cast(acDigest), 16, acHex);
iLength = 16;
poEdit->SetWindowText(acHex);
poEdit = (CEdit*)(GetDlgItem(IDC_EDITSTR2));
//判别是否存在非法字符
for(i=0; iif(acDigest[i] == 0)
acDigest[i] = 0x0A;
poEdit->SetWindowText(acDigest);
break;
case RSA:
poEdit = (CEdit*)GetDlgItem(IDC_EDITP);
poEdit->GetWindowText(strPrimeP);
poEdit = (CEdit*)GetDlgItem(IDC_EDITQ);
poEdit->GetWindowText(strPrimeQ);
//判断输入的数据P、Q是否为素数
if((!ToPrimeNumber(strPrimeP)) || (!ToPrimeNumber(strPrimeQ)))
return;

AfxMessageBox("采用RSA算法加密显示结果");
iPrimeP = atoi(strPrimeP.GetBuffer(strPrimeP.GetLength()));
iPrimeQ = atoi(strPrimeQ.GetBuffer(strPrimeQ.GetLength()));
m_iRSAn = iPrimeP*iPrimeQ;
//strTemp为加密的内容
//调用CRSA类来进行加密
iSubkey = m_oRSA.GetSecretKey(iPrimeP,iPrimeQ);
//保存密钥,用于解密
m_iRSAe = iSubkey;
//显示密钥
strKey.Format(_T("%d"),iSubkey);
poStatic = (CStatic*)GetDlgItem(IDC_LBLSTR);
poStatic->SetWindowText("RSA加密的密钥为:"+strKey);
strResult = m_oRSA.Encrypt(strTemp,iPrimeP,iPrimeQ,iSubkey);
//清空IDC_EDITALPHA里面的输入的明文
poEdit = (CEdit*)GetDlgItem(IDC_EDITALPHA);
poEdit->SetWindowText("");
poEdit = (CEdit*)GetDlgItem(IDC_EDITSTR2);
poEdit->SetWindowText(strResult);
//将上面获取的字符串数据转化为16进制数据
Binary2Hex(reinterpret_cast(strResult.GetBuffer(0)),16,acHex);
poEdit = (CEdit*)GetDlgItem(IDC_EDITHEX2);
//显示结果
poEdit->SetWindowText(acHex);
break;
}
}
else//解密算法
{
m_iAction = DECRYPT;
CString szDecyptResult,szDecyptMessage;
switch(m_iMethod)
{
case DES:
//显示解密的结果
szDecyptResult = m_oDES.Decrypt();
poEdit = (CEdit*)GetDlgItem(IDC_EDITALPHA);
//显示解密的结果
poEdit->SetWindowText(szDecyptResult);
break;
case MD5://解密按钮已经被屏蔽掉
break;
case RSA:
AfxMessageBox("采用RSA算法解密显示结果");
//获取解密的密文
poEdit = (CEdit*)GetDlgItem(IDC_EDITSTR2);
poEdit->GetWindowText(szDecyptMessa

ge);
szDecyptResult = m_oRSA.Decrypt(szDecyptMessage,m_iRSAe,m_iRSAn);
poEdit = (CEdit*)GetDlgItem(IDC_EDITALPHA);
poEdit->SetWindowText(szDecyptResult);
break;
}
}
}

//对输入的字符串进行转化
BOOL CEncryAndDecryptDlg::ToPrimeNumber(CString szTemp)
{
//判别szTemp中是否有非数字的字符
int iLength = szTemp.GetLength();
for(int i = 0;i < iLength;i++)
{
char ch = szTemp.GetAt(i);
if(!isdigit(ch))//不是数字
{
AfxMessageBox("输入的数据中含有非数字字符!");
return FALSE;
}
}
//将CString字符串类转化为整形
int INum = atoi(szTemp);
if(!IsPrime(INum))//非素数处理
{
AfxMessageBox("输入的数据不是素数,请重试!");
return FALSE;
}
return TRUE;
}

//全局函数,用于判别输入的数据是否为素数
BOOL CEncryAndDecryptDlg::IsPrime(int x)
{
int k;
k = int(sqrt ( x ));
for ( int i = 2; i <= k; i ++ )
{
if ( x % i == 0 )
break;
}
if ( i >= k + 1 )
return TRUE;
else
return FALSE;
} 6楼 发表://使文件加密按钮有效 void CEn[refer=6,abfsd]//使文件加密按钮有效\r\nvoid CEncryAndDecryptDlg::OnUpdateBtnFileEncry(CCmdUI* pCmdUI)\r\n{\r\n BOOL m_bTe...[/refer] //使文件加密按钮有效
void CEncryAndDecryptDlg::OnUpdateBtnFileEncry(CCmdUI* pCmdUI)
{
BOOL m_bTemp = TRUE;
CEdit* pEdit = (CEdit*)(GetDlgItem(IDC_EDITFILE));
//当没有输入IDC_EDITFILE的字符长度或者算法选择MD5同时为解密算法,按钮无效
if(pEdit->GetWindowTextLength() <= 0)
m_bTemp = FALSE;
if((m_iMethod == MD5) && (m_iAction == DECRYPT))
m_bTemp = FALSE;
pCmdUI->Enable(m_bTemp);
}

//使文件加密按钮有效
void CEncryAndDecryptDlg::OnUpdateBtnStrEncry(CCmdUI* pCmdUI)
{
BOOL m_bTemp = TRUE;
CEdit* pEdit1,*pEdit2;
pEdit1 = (CEdit*)(GetDlgItem(IDC_EDITSTR2));
pEdit2 = (CEdit*)(GetDlgItem(IDC_EDITALPHA));
if(m_iAction == ENCRYPT)//加密时的处理
{
//当算法选择MD5同时为解密算法,按钮无效
if((m_iMethod == MD5) && (m_iAction == DECRYPT))
m_bTemp = FALSE;
pCmdUI->Enable(m_bTemp);
}
else//解密时的处理
{
//当IDC_EDITSTR2没有信息时候
if(pEdit1->GetWindowTextLength() <= 0)
m_bTemp = FALSE;
//用于设置解密只能一次
if(pEdit2->GetWindowTextLength() && m_iMethod == DES)
m_bTemp = FALSE;
if((m_iMethod == MD5) && (m_iAction == DECRYPT))
m_bTemp = FALSE;
if((m_iMethod == RSA) && (pEdit2->GetWindowTextLength() > 0))
m_bTemp = FALSE;
pCmdUI->Enable(m_bTemp);
}
}

//使保存按钮有效
void CEncryAndDecryptDlg::OnUpdateBtnSaveAs(CCmdUI* pCmdUI)
{
CEdit* pEdit = (CEdit*)(GetDlgItem(IDC_EDITSTR1));
//当有字符串输入到ID号为IDC_EDITSTR1的EDITBOX里面,该按钮就有效
pCmdUI->Enable(pEdit->GetWindowTextLength() > 0);
}

//使ID号为IDC_EDITDES的EDITBOX变得有效或者无效
void CEncryAndDecryptDlg::OnUpdateEditDES(CCmdUI* pCmdUI)
{
/* BOOL m_bTemp = TRUE;
if

((m_iMethod == MD5) || (m_iMethod == RSA))
m_bTemp = FALSE;*/
pCmdUI->Enable(m_iMethod == DES);
}

//使ID号为IDC_EDITP的EDITBOX变得有效或者无效
void CEncryAndDecryptDlg::OnUpdateEditP(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_iMethod == RSA);
}

//使ID号为IDC_EDITQ的EDITBOX变得有效或者无效
void CEncryAndDecryptDlg::OnUpdateEditQ(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_iMethod == RSA);
}

//判别使用什么加密算法
void CEncryAndDecryptDlg::OnSelchangeCombomethods()
{
CComboBox* poComboBox;
poComboBox = (CComboBox*)(GetDlgItem(IDC_COMBO_METHODS));
int iSel = poComboBox->GetCurSel();
ASSERT((iSel >= DES)&&(iSel <= RSA));
m_iMethod = iSel;//获取选中的值
}

//点击单选框字符时的处理
void CEncryAndDecryptDlg::OnRadalpha()
{
// TODO: Add your control notification handler code here
//使字符串EDITBOX有效
m_oEditAlpha.EnableWindow(TRUE);
//对该EDITBOX重新设置背景
m_oEditAlpha.SetBkColor(m_oPlainBg);
//使16进制EDITBOX无效
m_oEditHex.EnableWindow(FALSE);
//对该EDITBOX重新设置背景
m_oEditHex.SetBkColor(m_oPlainBg1);
}

//点击单选框16进制时的处理
void CEncryAndDecryptDlg::OnRadhex()
{
// TODO: Add your control notification handler code here
//使字符串EDITBOX无效
m_oEditAlpha.EnableWindow(FALSE);
//对该EDITBOX重新设置背景
m_oEditAlpha.SetBkColor(m_oPlainBg1);
//使16进制EDITBOX有效
m_oEditHex.EnableWindow(TRUE);
//对该EDITBOX重新设置背景
m_oEditHex.SetBkColor(m_oPlainBg);
}

//点击单选框解密时的处理
void CEncryAndDecryptDlg::OnRaddecrypt()
{
// TODO: Add your control notification handler code here
//记录动作为解密
m_iAction = DECRYPT;
//改变按钮为解密
CButton* poButton1 = (CButton*)(GetDlgItem(IDC_BTNFILEDIGEST));
poButton1->SetWindowText("&文件解密");
CButton *poButton2 = (CButton*)(GetDlgItem(IDC_BTNSTRINGDIGEST));
poButton2->SetWindowText("&字符串解密");
//更换位图
m_oTransparentBitmap1.ChangeBitmap(HBITMAP(m_oBMP2));
m_oTransparentBitmap2.ChangeBitmap(HBITMAP(m_oBMP2));
}

//点击单选框加密时的处理
void CEncryAndDecryptDlg::OnRadencrypt()
{
// TODO: Add your control notification handler code here
//记录动作为加密
m_iAction = ENCRYPT;
//改变按钮为加密
CButton* poButton1 = (CButton*)(GetDlgItem(IDC_BTNFILEDIGEST));
poButton1->SetWindowText("&文件加密");
CButton *poButton2 = (CButton*)(GetDlgItem(IDC_BTNSTRINGDIGEST));
poButton2->SetWindowText("&字符串加密");
//更换位图
m_oTransparentBitmap1.ChangeBitmap(HBITMAP(m_oBMP1));
m_oTransparentBitmap2.ChangeBitmap(HBITMAP(m_oBMP1));
}

//对文件进行加密处理
void CEncryAndDecryptDlg::OnBtnfiledigest()
{
// TODO: Add your control notification handler code here、
//判别算法为加密算法还是解密算法
CButton* poAction = (CButton*)(GetDlgItem(IDC_RADENCRYPT))

;
//从EDIT中获取文件名
CString strFileName;
CEdit* poEdit = (CEdit*)(GetDlgItem(IDC_EDITFILE));
poEdit->GetWindowText(strFileName);
//确定选择的方法
/*switch(m_iMethod)
{
case DES:
AfxMessageBox("采用DES算法");
break;
case MD5:
AfxMessageBox("采用MD5算法");
break;
case RSA:
AfxMessageBox("采用RSA算法");
break;
}*/
//对文件进行加密解密
char acDigest[65] = {0};
char szHexDigest[129] = {0};
int iLength;
//判别为加密算法还是解密算法
if(poAction->GetCheck() == 1)//为加密算法
{
m_iAction = ENCRYPT;
switch(m_iMethod)
{
case DES:
AfxMessageBox("采用DES算法加密显示结果");
break;
case MD5:
AfxMessageBox("采用MD5算法加密显示结果");
//对摘要进行加密
m_oMD5.DigestFile(LPCTSTR(strFileName), acDigest);
Binary2Hex(reinterpret_cast(acDigest), 16, szHexDigest);
iLength = 16;
break;
case RSA:
AfxMessageBox("采用RSA算法加密显示结果");
break;
}
//将结果显示出来
poEdit = (CEdit*)(GetDlgItem(IDC_EDITHEX1));
poEdit->SetWindowText(szHexDigest);
//判别是否存在非法字符
for(int i=0; iif(acDigest[i] == 0)
acDigest[i] = 0x0A;
poEdit = (CEdit*)(GetDlgItem(IDC_EDITSTR1));
poEdit->SetWindowText(acDigest);
}
else//解密算法
{
m_iAction = DECRYPT;
switch(m_iMethod)
{
case DES:
AfxMessageBox("DES算法解密显示结果");
break;
case MD5://解密按钮已经被屏蔽掉
break;
case RSA:
AfxMessageBox("采用RSA算法解密显示结果");
break;
}
}
} 5楼 发表://显示String模式所对应的面板上[refer=5,abfsd]//显示String模式所对应的面板上的所有控件\r\nvoid CEncryAndDecryptDlg::ShowStringGroup(BOOL bShow)\r\n{\r\n...[/refer] //显示String模式所对应的面板上的所有控件
void CEncryAndDecryptDlg::ShowStringGroup(BOOL bShow)
{
CWnd* poWnd;
//显示各个控件
poWnd = GetDlgItem(IDC_LBLSTR);
poWnd->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_EDITALPHA);
poWnd->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_EDITHEX);
poWnd->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_RADALPHA);
poWnd->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_RADHEX);
poWnd->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_STATICBMPARROW2);
poWnd->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_BTNSTRINGDIGEST);
poWnd->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_LBLSTR2);
poWnd->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_EDITSTR2);
poWnd->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_LBLHEX2);
poWnd->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_EDITHEX2);
poWnd->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
}
void CEncryAndDecryptDlg::MoveFileGroup()
{
//获取y坐标
CRect oRect;
CWnd

* poWnd;
poWnd = GetDlgItem(IDC_BORDER);
poWnd->GetWindowRect(&oRect);
int iYRef = oRect.bottom;
poWnd = GetDlgItem(IDC_LBLFILE);
poWnd->GetWindowRect(&oRect);
int iDelta = oRect.top - iYRef;
//坐标转化
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//设置各个控件的位置
//
poWnd = GetDlgItem(IDC_EDITFILE);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_BTNFILE);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_STATICBMPARROW1);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_BTNFILEDIGEST);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_LBLSTR1);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_EDITSTR1);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_BTNSAVEAS);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_LBLHEX1);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_EDITHEX1);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
}
void CEncryAndDecryptDlg::MoveStringGroup()
{
//获取y坐标
CRect oRect;
CWnd* poWnd;
poWnd = GetDlgItem(IDC_BORDER);
poWnd->GetWindowRect(&oRect);
int iYRef = oRect.bottom;
poWnd = GetDlgItem(IDC_LBLSTR);
poWnd->GetWindowRect(&oRect);
int iDelta = oRect.top - iYRef;
//坐标转化
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//设置各个控件的位置
//
poWnd = GetDlgItem(IDC_EDITALPHA);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_EDITHEX);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_RADALPHA);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRec

t);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_RADHEX);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_STATICBMPARROW2);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_BTNSTRINGDIGEST);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_LBLSTR2);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_EDITSTR2);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_LBLHEX2);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
//
poWnd = GetDlgItem(IDC_EDITHEX2);
poWnd->GetWindowRect(&oRect);
ScreenToClient(oRect);
poWnd->SetWindowPos(NULL, oRect.left, oRect.top-iDelta, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
}

//点击浏览按钮
void CEncryAndDecryptDlg::OnBtnfile()
{
// TODO: Add your control notification handler code here
CFileDialog m_FileOpen(TRUE);
//设置打开窗体的标题
m_FileOpen.m_ofn.lpstrTitle = _T("打开文件");
m_FileOpen.m_ofn.lpstrFilter = _T("文件 (*.*)\0*.*\0\0");
//点击浏览按钮
if(IDOK == m_FileOpen.DoModal())
{
//获取打开的文件路径
CString m_FileName = m_FileOpen.GetPathName();
CEdit* pEdit = (CEdit*)(GetDlgItem(IDC_EDITFILE));
//将选择的文件路径名显示到IDC_EDITFILE表识的EDIT中
pEdit->SetWindowText(m_FileName);
}
}

//点击按钮保存
void CEncryAndDecryptDlg::OnBtnsaveas()
{
// TODO: Add your control notification handler code here
CFileDialog oFileOpen(FALSE);
//设置保存文件的标题
oFileOpen.m_ofn.lpstrTitle = _T("保存加密文件");
oFileOpen.m_ofn.lpstrFilter = _T("文件格式 (*.txt)\0*.txt\0(*.*)\0*.*\0\0");
if(oFileOpen.DoModal() == IDOK)
{
//获取文件路径
CString oStrFileName = oFileOpen.GetPathName();
oStrFileName += ".txt";
CFile oFile;
//文件异常类,用于捕获文件异常
CFileException oFileException;
TCHAR szBuff[MAX_PATH+1];
_tcscpy(szBuff, LPCTSTR(oStrFileName));
//获取路径或文件名中的文件扩展名
CString pcExt = ::PathFindExtension(oStrFileName);
//创建文件来保存,以二进制形式来保存
BOOL bOpen = oFile.Open(oStrFileName, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary, &oFileException);
if(bOpen == TRUE)
{
//打开文件处理
char ac

Digest[65] = {0};
CEdit* poEdit = (CEdit*)(GetDlgItem(IDC_EDITHEX1));
CString oStrHex;
poEdit->GetWindowText(oStrHex);
int iLength;
switch(m_iMethod)
{
case DES:
AfxMessageBox("采用DES算法");
break;
case MD5:
iLength = 16;
break;
case RSA:
AfxMessageBox("采用RSA算法");
break;
default:
ASSERT(0);
}
Hex2Binary(LPCTSTR(oStrHex), reinterpret_cast(acDigest), iLength);
oFile.Write(acDigest, iLength);
oFile.Close();
AfxMessageBox("保存文件成功!");
}
else
{
AfxMessageBox("保存文件失败!");
}
}
} 4楼 发表://下面是16进制和字符以及二进制[refer=4,abfsd]//下面是16进制和字符以及二进制相互转化的全局函数\r\n//将字符转化为16进制\r\nvoid Char2Hex(unsigned char ...[/refer] //下面是16进制和字符以及二进制相互转化的全局函数
//将字符转化为16进制
void Char2Hex(unsigned char ch, char* szHex)
{
static unsigned char saucHex[] = "0123456789ABCDEF";
szHex[0] = saucHex[ch >> 4];
szHex[1] = saucHex[ch&0xF];
szHex[2] = 0;



______________________
and or 异或

AND OR 异或
1 0 0 1 1 0 0 1 1 0 0 1
0 1 0 1 0 1 0 1 0 1 0 1
0 0 0 1 1 1 0 1 1 1 0 0




相关文档
最新文档