分享基于字符串加密的MD5算法实例代码


本文摘自PHP中文网,作者零下一度,侵删。

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

运行效果如下:

核心代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

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

}

以上就是分享基于字符串加密的MD5算法实例代码的详细内容!

相关阅读 >>

asp.net mvc 对用户输入的字符串做trim处理的方法实例

字符串分割的使用实例代码

c#实现操作字符串的方法总结

asp如何解析json字符串并转化为asp对象

c#基于正则表达式如何删除字符串中数字或非数字的方法详解

c#获取字符串长度,一个汉字长度为2

base64加密?它只是一种编码算法,切勿用来加密

identityserver4 signingcredential(rsa 证书加密)实例详解

属性值反序列化失败怎么解决?

c#中字符串的一般性和特殊性具体介绍(图文)

更多相关阅读请进入《字符串》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...