温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VC++中如何实现ANSI字符串加密与Unicode字符串加密

发布时间:2021-08-20 09:45:07 来源:亿速云 阅读:137 作者:小新 栏目:编程语言

小编给大家分享一下VC++中如何实现ANSI字符串加密与Unicode字符串加密,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

基于字符串加密的MD5算法,VS2008 VC++,多字节编译工程。主要代码如下,实现了ANSI字符串加密与Unicode字符串加密。

运行效果如下:

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 
}

以上是“VC++中如何实现ANSI字符串加密与Unicode字符串加密”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI