分享基於字串加密的MD5演算法執行個體代碼

來源:互聯網
上載者:User
基於字串加密的MD5演算法,VS2008 VC++,多位元組編譯工程。主要代碼如下,實現了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("密文比較結果相同!"),_T("比較相同"),MB_OK|MB_ICONINFORMATION);   }   else   {     MessageBox(_T("密文比較結果失敗!"),_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 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.