⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crypt.txt

📁 包含MD5,SHA-1,DES,RSA的程序代码
💻 TXT
📖 第 1 页 / 共 5 页
字号:
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<char*>(_alloca(iLen)); 
  Hex2Binary(LPCTSTR(strTemp), reinterpret_cast<unsigned char*>(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<unsigned char*>(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<unsigned char*>(acDigest), 16, acHex); 
   iLength = 16; 
   poEdit->SetWindowText(acHex); 
   poEdit = (CEdit*)(GetDlgItem(IDC_EDITSTR2)); 
   //判别是否存在非法字符 
   for(i=0; i<iLength; i++) 
    if(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<unsigned char*>(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(szDecyptMessage); 
   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; 
} 

 //使文件加密按钮有效 
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<unsigned char*>(acDigest), 16, szHexDigest); 
   iLength = 16; 
   break; 
  case RSA: 
   AfxMessageBox("采用RSA算法加密显示结果"); 
   break; 
 } 
 //将结果显示出来 
 poEdit = (CEdit*)(GetDlgItem(IDC_EDITHEX1)); 
 poEdit->SetWindowText(szHexDigest); 
 //判别是否存在非法字符 
 for(int i=0; i<iLength; i++) 
  if(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; 
 } 
} 
} 

//显示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); 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -