溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

VC++中如何實(shí)現(xiàn)ANSI字符串加密與Unicode字符串加密

發(fā)布時(shí)間:2021-08-20 09:45:07 來(lái)源:億速云 閱讀:139 作者:小新 欄目:編程語(yǔ)言

小編給大家分享一下VC++中如何實(shí)現(xiàn)ANSI字符串加密與Unicode字符串加密,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

基于字符串加密的MD5算法,VS2008 VC++,多字節(jié)編譯工程。主要代碼如下,實(shí)現(xiàn)了ANSI字符串加密與Unicode字符串加密。

運(yùn)行效果如下:

VC++中如何實(shí)現(xiàn)ANSI字符串加密與Unicode字符串加密

核心代碼:

void CEncryptByMd5Dlg::OnButtonOk()  
{ 
  // TODO: Add your control notification handler code here 
  UpdateData(true); 
  unsigned int len=0; 
  char *cTemp =NULL; 
  if(m_bType==0) 
  { 
    len=m_sText.GetLength(); 
    cTemp=(char*)(LPCTSTR)m_sText; 
  } 
  else 
  { 
    len=CStringW(m_sText).GetLength()*2; 
    cTemp=(char*)ANSI2UNICODE(m_sText); 
  } 
  char *cIdentity; 
  CMd5A md5; 
  cIdentity = md5.MDString(cTemp,len); 
  m_sEncrypt = CString(cIdentity); 
  if(m_bUpper==TRUE) 
  { 
    m_sEncrypt.MakeUpper(); 
  } 
  else 
  { 
    m_sEncrypt.MakeLower(); 
  } 
  UpdateData(false); 
} 
 
void CEncryptByMd5Dlg::OnBnClickedBtnCompare() 
{ 
  // TODO: Add your control notification handler code here 
  UpdateData(true); 
  if(m_sEncrypt==m_szMD5_2) 
  { 
    MessageBox(_T("密文比較結(jié)果相同!"),_T("比較相同"),MB_OK|MB_ICONINFORMATION); 
  } 
  else 
  { 
    MessageBox(_T("密文比較結(jié)果失??!"),_T("比較不同"),MB_OK|MB_ICONERROR); 
  } 
  UpdateData(FALSE); 
} 
 
void CEncryptByMd5Dlg::OnEnChangeEdit1() 
{ 
  // TODO: If this is a RICHEDIT control, the control will not 
  // send this notification unless you override the CDialog::OnInitDialog() 
  // function and call CRichEditCtrl().SetEventMask() 
  // with the ENM_CHANGE flag ORed into the mask. 
  OnButtonOk(); 
  // TODO: Add your control notification handler code here 
} 
 
char * CEncryptByMd5Dlg::Unicode2ANSI(CString strSource) 
{ 
  if (strSource.IsEmpty()) return NULL; 
  char *pBuffer = NULL; 
  int nBufferSize = 0; 
#ifdef _UNICODE  
  nBufferSize = WideCharToMultiByte(CP_ACP, 0, (LPCTSTR)strSource, -1, NULL, 0, NULL, NULL) + 1; 
  pBuffer = new char[nBufferSize]; 
  memset(pBuffer, 0, sizeof(char)*nBufferSize); 
 
  WideCharToMultiByte(CP_ACP, 0, (LPCTSTR)strSource, -1, pBuffer, nBufferSize, NULL, NULL); 
#else  
  nBufferSize = strSource.GetLength() + 1; 
  pBuffer = new char[nBufferSize]; 
  memset(pBuffer, 0, sizeof(char)*nBufferSize); 
 
  strcpy_s(pBuffer, nBufferSize, (LPCTSTR)strSource); 
#endif  
  return pBuffer; 
} 
 
wchar_t * CEncryptByMd5Dlg::ANSI2UNICODE(CString pData) 
{ 
  int nLength = MultiByteToWideChar(CP_ACP, 0, pData, -1, NULL, 0); 
  wchar_t *pwBuffer = new wchar_t[nLength + 1]; 
  memset(pwBuffer, 0, sizeof(wchar_t)*(nLength + 1)); 
  MultiByteToWideChar(CP_ACP, 0, pData, -1, pwBuffer, nLength); 
  return pwBuffer; 
} 
 
void CEncryptByMd5Dlg::OnBnClickedCheckUpper() 
{ 
  OnButtonOk(); 
  // TODO: Add your control notification handler code here 
} 
 
void CEncryptByMd5Dlg::OnBnClickedRadio1() 
{ 
  OnButtonOk(); 
  // TODO: Add your control notification handler code here 
} 
 
void CEncryptByMd5Dlg::OnBnClickedRadio2() 
{ 
  OnButtonOk(); 
  // TODO: Add your control notification handler code here 
}

以上是“VC++中如何實(shí)現(xiàn)ANSI字符串加密與Unicode字符串加密”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI