当前位置:   article > 正文

常用的加密方式,C++_密码加密的方式有哪些 c++

密码加密的方式有哪些 c++

base64,DES,MD5

base64.h

  1. #pragma once
  2. const char base[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  3. class CBase64
  4. {
  5. public:
  6. CBase64();
  7. ~CBase64();
  8. // 头文件 base64.h
  9. /* Base64 编码 */
  10. CString base64_encode(CString str);
  11. /* Base64 解码 */
  12. char *base64_decode(const char* data, int data_len);
  13. static char find_pos(char ch);
  14. };


base64.cpp

  1. #include "stdafx.h"
  2. #include "Base64.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. CBase64::CBase64()
  7. {
  8. }
  9. CBase64::~CBase64()
  10. {
  11. }
  12. /* Base64 编码 */
  13. CString CBase64::base64_encode(CString str)
  14. {
  15. //int data_len = strlen(data);
  16. USES_CONVERSION;
  17. const char * data = T2A(str);
  18. int data_len = strlen(data);
  19. int prepare = 0;
  20. int ret_len;
  21. int temp = 0;
  22. char *ret = NULL;
  23. char *f = NULL;
  24. int tmp = 0;
  25. char changed[4];
  26. int i = 0;
  27. ret_len = data_len / 3;
  28. temp = data_len % 3;
  29. if (temp > 0)
  30. {
  31. ret_len += 1;
  32. }
  33. ret_len = ret_len * 4 + 1;
  34. ret = (char *)malloc(ret_len);
  35. if (ret == NULL)
  36. {
  37. printf("No enough memory.\n");
  38. exit(0);
  39. }
  40. memset(ret, 0, ret_len);
  41. f = ret;
  42. while (tmp < data_len)
  43. {
  44. temp = 0;
  45. prepare = 0;
  46. memset(changed, '\0', 4);
  47. while (temp < 3)
  48. {
  49. //printf("tmp = %d\n", tmp);
  50. if (tmp >= data_len)
  51. {
  52. break;
  53. }
  54. prepare = ((prepare << 8) | (data[tmp] & 0xFF));
  55. tmp++;
  56. temp++;
  57. }
  58. prepare = (prepare << ((3 - temp) * 8));
  59. //printf("before for : temp = %d, prepare = %d\n", temp, prepare);
  60. for (i = 0; i < 4; i++)
  61. {
  62. if (temp < i)
  63. {
  64. changed[i] = 0x40;
  65. }
  66. else
  67. {
  68. changed[i] = (prepare >> ((3 - i) * 6)) & 0x3F;
  69. }
  70. *f = base[changed[i]];
  71. //printf("%.2X", changed[i]);
  72. f++;
  73. }
  74. }
  75. *f = '\0';
  76. //return ret;
  77. return A2W(ret);
  78. }
  79. /* 转换算子 */
  80. char CBase64::find_pos(char ch)
  81. {
  82. char *ptr = (char*)strrchr(base, ch);//the last position (the only) in base[]
  83. return (ptr - base);
  84. }
  85. /* Base64 解码 */
  86. char *CBase64::base64_decode(const char *data, int data_len)
  87. {
  88. int ret_len = (data_len / 4) * 3;
  89. int equal_count = 0;
  90. char *ret = NULL;
  91. char *f = NULL;
  92. int tmp = 0;
  93. int temp = 0;
  94. int prepare = 0;
  95. int i = 0;
  96. if (*(data + data_len - 1) == '=')
  97. {
  98. equal_count += 1;
  99. }
  100. if (*(data + data_len - 2) == '=')
  101. {
  102. equal_count += 1;
  103. }
  104. if (*(data + data_len - 3) == '=')
  105. {//seems impossible
  106. equal_count += 1;
  107. }
  108. switch (equal_count)
  109. {
  110. case 0:
  111. ret_len += 4;//3 + 1 [1 for NULL]
  112. break;
  113. case 1:
  114. ret_len += 4;//Ceil((6*3)/8)+1
  115. break;
  116. case 2:
  117. ret_len += 3;//Ceil((6*2)/8)+1
  118. break;
  119. case 3:
  120. ret_len += 2;//Ceil((6*1)/8)+1
  121. break;
  122. }
  123. ret = (char *)malloc(ret_len);
  124. if (ret == NULL)
  125. {
  126. printf("No enough memory.\n");
  127. exit(0);
  128. }
  129. memset(ret, 0, ret_len);
  130. f = ret;
  131. while (tmp < (data_len - equal_count))
  132. {
  133. temp = 0;
  134. prepare = 0;
  135. while (temp < 4)
  136. {
  137. if (tmp >= (data_len - equal_count))
  138. {
  139. break;
  140. }
  141. prepare = (prepare << 6) | (find_pos(data[tmp]));
  142. temp++;
  143. tmp++;
  144. }
  145. prepare = prepare << ((4 - temp) * 6);
  146. for (i = 0; i < 3; i++)
  147. {
  148. if (i == temp)
  149. {
  150. break;
  151. }
  152. *f = (char)((prepare >> ((2 - i) * 8)) & 0xFF);
  153. f++;
  154. }
  155. }
  156. *f = '\0';
  157. return ret;
  158. }

DES.h

  1. #pragma once
  2. #include <string>
  3. class CDes
  4. {
  5. private:
  6. public:
  7. //加密
  8. CString Encrypt(CString str,BOOL bIsPwd=FALSE);
  9. char hexCiphertextAnyLength[16384], bitsCiphertextAnyLength[32768];
  10. void ConvertCiphertext2OtherFormat(int iBitsLen, char *szCipherInBytes);
  11. //解密
  12. CString Decrypt(CString str,BOOL bIsPwd = FALSE);
  13. //类构造函数
  14. CDes();
  15. //类析构函数
  16. ~CDes();
  17. //功能:产生16个28位的key
  18. //参数:源8位的字符串(key),存放key的序号0-1
  19. //结果:函数将调用private CreateSubKey将结果存于char SubKeys[keyN][16][48]
  20. void InitializeKey(char* srcBytes, unsigned int keyN);
  21. //功能:加密8位字符串
  22. //参数:8位字符串,使用Key的序号0-1
  23. //结果:函数将加密后结果存放于private szCiphertext[16]
  24. // 用户通过属性Ciphertext得到
  25. void EncryptData(char* _srcBytes, unsigned int keyN);
  26. //功能:解密16位十六进制字符串
  27. //参数:16位十六进制字符串,使用Key的序号0-1
  28. //结果:函数将解密候结果存放于private szPlaintext[8]
  29. // 用户通过属性Plaintext得到
  30. void DecryptData(char* _srcBytes, unsigned int keyN);
  31. //功能:加密任意长度字符串
  32. //参数:任意长度字符串,长度,使用Key的序号0-1
  33. //结果:函数将加密后结果存放于private szFCiphertextAnyLength[8192]
  34. // 用户通过属性CiphertextAnyLength得到
  35. void EncryptAnyLength(char* _srcBytes, unsigned int _bytesLength, unsigned int keyN);
  36. //功能:解密任意长度十六进制字符串
  37. //参数:任意长度字符串,长度,使用Key的序号0-1
  38. //结果:函数将加密后结果存放于private szFPlaintextAnyLength[8192]
  39. // 用户通过属性PlaintextAnyLength得到
  40. void DecryptAnyLength(char* _srcBytes, unsigned int _bytesLength, unsigned int keyN);
  41. //功能:Bytes到Bits的转换,
  42. //参数:待变换字符串,处理后结果存放缓冲区指针,Bits缓冲区大小
  43. void Bytes2Bits(char *srcBytes, char* dstBits, unsigned int sizeBits);
  44. //功能:Bits到Bytes的转换,
  45. //参数:待变换字符串,处理后结果存放缓冲区指针,Bits缓冲区大小
  46. void Bits2Bytes(char *dstBytes, char* srcBits, unsigned int sizeBits);
  47. //功能:Int到Bits的转换,
  48. //参数:待变换字符串,处理后结果存放缓冲区指针
  49. void Int2Bits(unsigned int srcByte, char* dstBits);
  50. //功能:Bits到Hex的转换
  51. //参数:待变换字符串,处理后结果存放缓冲区指针,Bits缓冲区大小
  52. void Bits2Hex(char *dstHex, char* srcBits, unsigned int sizeBits);
  53. //功能:Bits到Hex的转换
  54. //参数:待变换字符串,处理后结果存放缓冲区指针,Bits缓冲区大小
  55. void Hex2Bits(char *srcHex, char* dstBits, unsigned int sizeBits);
  56. //szCiphertextInBinary的get函数
  57. char* GetCiphertextInBinary();
  58. //szCiphertextInHex的get函数
  59. char* GetCiphertextInHex();
  60. //Ciphertext的get函数
  61. char* GetCiphertextInBytes();
  62. //Plaintext的get函数
  63. char* GetPlaintext();
  64. //CiphertextAnyLength的get函数
  65. char* GetCiphertextAnyLength();
  66. //PlaintextAnyLength的get函数
  67. char* GetPlaintextAnyLength();
  68. private:
  69. char szSubKeys[2][16][48];//储存2个16组48位密钥,第2个用于3DES
  70. char szCiphertextRaw[64]; //储存二进制密文(64个Bits) int 0,1
  71. char szPlaintextRaw[64]; //储存二进制密文(64个Bits) int 0,1
  72. char szCiphertextInBytes[8];//储存8位密文
  73. char szPlaintextInBytes[8];//储存8位明文字符串
  74. char szCiphertextInBinary[65]; //储存二进制密文(64个Bits) char '0','1',最后一位存'\0'
  75. char szCiphertextInHex[17]; //储存十六进制密文,最后一位存'\0'
  76. char szPlaintext[9];//储存8位明文字符串,最后一位存'\0'
  77. char szFCiphertextAnyLength[8192];//任意长度密文
  78. char szFPlaintextAnyLength[8192];//任意长度明文字符串
  79. //功能:生成子密钥
  80. //参数:经过PC1变换的56位二进制字符串,生成的szSubKeys编号0-1
  81. //结果:将保存于char szSubKeys[16][48]
  82. void CreateSubKey(char* sz_56key, unsigned int keyN);
  83. //功能:DES中的F函数,
  84. //参数:左32位,右32位,key序号(0-15),使用的szSubKeys编号0-1
  85. //结果:均在变换左右32位
  86. void FunctionF(char* sz_Li, char* sz_Ri, unsigned int iKey, unsigned int keyN);
  87. //功能:IP变换
  88. //参数:待处理字符串,处理后结果存放指针
  89. //结果:函数改变第二个参数的内容
  90. void InitialPermuteData(char* _src, char* _dst);
  91. //功能:将右32位进行扩展位48位,
  92. //参数:原32位字符串,扩展后结果存放指针
  93. //结果:函数改变第二个参数的内容
  94. void ExpansionR(char* _src, char* _dst);
  95. //功能:异或函数,
  96. //参数:待异或的操作字符串1,字符串2,操作数长度,处理后结果存放指针
  97. //结果: 函数改变第四个参数的内容
  98. void XOR(char* szParam1, char* szParam2, unsigned int uiParamLength, char* szReturnValueBuffer);
  99. //功能:S-BOX , 数据压缩,
  100. //参数:48位二进制字符串,
  101. //结果:返回结果:32位字符串
  102. void CompressFuncS(char* _src48, char* _dst32);
  103. //功能:IP逆变换,
  104. //参数:待变换字符串,处理后结果存放指针
  105. //结果:函数改变第二个参数的内容
  106. void PermutationP(char* _src, char* _dst);
  107. };

DES.cpp

  1. #include "stdafx.h"
  2. #include "DES.h"
  3. // permuted choice table (PC1)
  4. const static char PC1_Table[56] = {
  5. 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
  6. 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
  7. 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
  8. 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
  9. };
  10. // permuted choice key (PC2)
  11. const static char PC2_Table[48] = {
  12. 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
  13. 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
  14. 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
  15. 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
  16. };
  17. // number left rotations of pc1
  18. const static char Shift_Table[16] = {
  19. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  20. };
  21. // initial permutation (IP)
  22. const static char IP_Table[64] = {
  23. 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
  24. 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
  25. 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
  26. 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
  27. };
  28. // expansion operation matrix (E)
  29. const static char E_Table[48] = {
  30. 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
  31. 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
  32. 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
  33. 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1
  34. };
  35. // The (in)famous S-boxes
  36. const static char S_Box[8][4][16] = {
  37. // S1
  38. 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
  39. 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
  40. 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
  41. 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
  42. // S2
  43. 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
  44. 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
  45. 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
  46. 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
  47. // S3
  48. 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
  49. 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
  50. 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
  51. 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
  52. // S4
  53. 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
  54. 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
  55. 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
  56. 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
  57. // S5
  58. 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
  59. 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
  60. 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
  61. 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
  62. // S6
  63. 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
  64. 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
  65. 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
  66. 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
  67. // S7
  68. 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
  69. 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
  70. 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
  71. 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
  72. // S8
  73. 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
  74. 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
  75. 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
  76. 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
  77. };
  78. // 32-bit permutation function P used on the output of the S-boxes
  79. const static char P_Table[32] = {
  80. 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
  81. 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
  82. };
  83. // final permutation IP^-1
  84. const static char IPR_Table[64] = {
  85. 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31,
  86. 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
  87. 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27,
  88. 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25
  89. };
  90. char KEY[8] = "1234678";//密匙
  91. char PWDKEY[8] = "2354123";
  92. CDes::CDes()
  93. {
  94. memset(szCiphertextRaw, 0, 64);
  95. memset(szPlaintextRaw, 0, 64);
  96. memset(szCiphertextInBytes, 0, 8);
  97. memset(szPlaintextInBytes, 0, 8);
  98. memset(szCiphertextInBinary, 0, 65);
  99. memset(szCiphertextInHex, 0, 17);
  100. memset(szPlaintext, 0, 9);
  101. memset(szFCiphertextAnyLength, 0, 8192);
  102. memset(szCiphertextInHex, 0, 8192);
  103. /dlj
  104. /end
  105. }
  106. CDes::~CDes()
  107. {
  108. }
  109. void CDes::InitializeKey(char* srcBytes, unsigned int keyN)
  110. {
  111. //convert 8 char-bytes key to 64 binary-bits
  112. char sz_64key[64] = { 0 };
  113. Bytes2Bits(srcBytes, sz_64key, 64);
  114. //PC 1
  115. char sz_56key[56] = { 0 };
  116. for (int k = 0; k < 56; k++)
  117. {
  118. sz_56key[k] = sz_64key[PC1_Table[k] - 1];
  119. }
  120. CreateSubKey(sz_56key, keyN);
  121. }
  122. void CDes::CreateSubKey(char* sz_56key, unsigned int keyN)
  123. {
  124. char szTmpL[28] = { 0 };
  125. char szTmpR[28] = { 0 };
  126. char szCi[28] = { 0 };
  127. char szDi[28] = { 0 };
  128. memcpy(szTmpL, sz_56key, 28);
  129. memcpy(szTmpR, sz_56key + 28, 28);
  130. for (int i = 0; i < 16; i++)
  131. {
  132. //shift to left
  133. //Left 28 bits
  134. memcpy(szCi, szTmpL + Shift_Table[i], 28 - Shift_Table[i]);
  135. memcpy(szCi + 28 - Shift_Table[i], szTmpL, Shift_Table[i]);
  136. //Right 28 bits
  137. memcpy(szDi, szTmpR + Shift_Table[i], 28 - Shift_Table[i]);
  138. memcpy(szDi + 28 - Shift_Table[i], szTmpR, Shift_Table[i]);
  139. //permuted choice 48 bits key
  140. char szTmp56[56] = { 0 };
  141. memcpy(szTmp56, szCi, 28);
  142. memcpy(szTmp56 + 28, szDi, 28);
  143. for (int j = 0; j < 48; j++)
  144. {
  145. szSubKeys[keyN][i][j] = szTmp56[PC2_Table[j] - 1];
  146. }
  147. //Evaluate new szTmpL and szTmpR
  148. memcpy(szTmpL, szCi, 28);
  149. memcpy(szTmpR, szDi, 28);
  150. }
  151. }
  152. void CDes::EncryptData(char* _srcBytes, unsigned int keyN)
  153. {
  154. char szSrcBits[64] = { 0 };
  155. char sz_IP[64] = { 0 };
  156. char sz_Li[32] = { 0 };
  157. char sz_Ri[32] = { 0 };
  158. char sz_Final64[64] = { 0 };
  159. Bytes2Bits(_srcBytes, szSrcBits, 64);
  160. //IP
  161. InitialPermuteData(szSrcBits, sz_IP);
  162. memcpy(sz_Li, sz_IP, 32);
  163. memcpy(sz_Ri, sz_IP + 32, 32);
  164. for (int i = 0; i < 16; i++)
  165. {
  166. FunctionF(sz_Li, sz_Ri, i, keyN);
  167. }
  168. //so D=LR
  169. memcpy(sz_Final64, sz_Ri, 32);
  170. memcpy(sz_Final64 + 32, sz_Li, 32);
  171. //~IP
  172. for (int j = 0; j < 64; j++)
  173. {
  174. szCiphertextRaw[j] = sz_Final64[IPR_Table[j] - 1];
  175. }
  176. Bits2Bytes(szCiphertextInBytes, szCiphertextRaw, 64);
  177. }
  178. void CDes::DecryptData(char* _srcBytes, unsigned int keyN)
  179. {
  180. char szSrcBits[64] = { 0 };
  181. char sz_IP[64] = { 0 };
  182. char sz_Li[32] = { 0 };
  183. char sz_Ri[32] = { 0 };
  184. char sz_Final64[64] = { 0 };
  185. Bytes2Bits(_srcBytes, szSrcBits, 64);
  186. //IP --- return is sz_IP
  187. InitialPermuteData(szSrcBits, sz_IP);
  188. //divide the 64 bits data to two parts
  189. memcpy(sz_Ri, sz_IP, 32); //exchange L to R
  190. memcpy(sz_Li, sz_IP + 32, 32); //exchange R to L
  191. //16 rounds F and xor and exchange
  192. for (int i = 0; i < 16; i++)
  193. {
  194. FunctionF(sz_Ri, sz_Li, 15 - i, keyN);
  195. }
  196. memcpy(sz_Final64, sz_Li, 32);
  197. memcpy(sz_Final64 + 32, sz_Ri, 32);
  198. // ~IP
  199. for (int j = 0; j < 64; j++)
  200. {
  201. szPlaintextRaw[j] = sz_Final64[IPR_Table[j] - 1];
  202. }
  203. Bits2Bytes(szPlaintextInBytes, szPlaintextRaw, 64);
  204. }
  205. void CDes::FunctionF(char* sz_Li, char* sz_Ri, unsigned int iKey, unsigned int keyN)
  206. {
  207. char sz_48R[48] = { 0 };
  208. char sz_xor48[48] = { 0 };
  209. char sz_P32[32] = { 0 };
  210. char sz_Rii[32] = { 0 };
  211. char sz_Key[48] = { 0 };
  212. char s_Compress32[32] = { 0 };
  213. memcpy(sz_Key, szSubKeys[keyN][iKey], 48);
  214. ExpansionR(sz_Ri, sz_48R);
  215. XOR(sz_48R, sz_Key, 48, sz_xor48);
  216. CompressFuncS(sz_xor48, s_Compress32);
  217. PermutationP(s_Compress32, sz_P32);
  218. XOR(sz_P32, sz_Li, 32, sz_Rii);
  219. memcpy(sz_Li, sz_Ri, 32);
  220. memcpy(sz_Ri, sz_Rii, 32);
  221. }
  222. void CDes::InitialPermuteData(char* _src, char* _dst)
  223. {
  224. //IP
  225. for (int i = 0; i < 64; i++)
  226. {
  227. _dst[i] = _src[IP_Table[i] - 1];
  228. }
  229. }
  230. void CDes::ExpansionR(char* _src, char* _dst)
  231. {
  232. for (int i = 0; i < 48; i++)
  233. {
  234. _dst[i] = _src[E_Table[i] - 1];
  235. }
  236. }
  237. void CDes::XOR(char* szParam1, char* szParam2, unsigned int uiParamLength, char* szReturnValueBuffer)
  238. {
  239. for (unsigned int i = 0; i < uiParamLength; i++)
  240. {
  241. szReturnValueBuffer[i] = szParam1[i] ^ szParam2[i];
  242. }
  243. }
  244. void CDes::CompressFuncS(char* _src48, char* _dst32)
  245. {
  246. char bTemp[8][6] = { 0 };
  247. char dstBits[4] = { 0 };
  248. for (int i = 0; i < 8; i++)
  249. {
  250. memcpy(bTemp[i], _src48 + i * 6, 6);
  251. int iX = (bTemp[i][0]) * 2 + (bTemp[i][5]);
  252. int iY = 0;
  253. for (int j = 1; j < 5; j++)
  254. {
  255. iY += bTemp[i][j] << (4 - j);
  256. }
  257. Int2Bits(S_Box[i][iX][iY], dstBits);
  258. memcpy(_dst32 + i * 4, dstBits, 4);
  259. }
  260. }
  261. void CDes::PermutationP(char* _src, char* _dst)
  262. {
  263. for (int i = 0; i < 32; i++)
  264. {
  265. _dst[i] = _src[P_Table[i] - 1];
  266. }
  267. }
  268. void CDes::Bytes2Bits(char *srcBytes, char* dstBits, unsigned int sizeBits)
  269. {
  270. for (unsigned int i = 0; i < sizeBits; i++)
  271. dstBits[i] = ((srcBytes[i >> 3] << (i & 7)) & 128) >> 7;
  272. }
  273. void CDes::Bits2Bytes(char *dstBytes, char* srcBits, unsigned int sizeBits)
  274. {
  275. memset(dstBytes, 0, sizeBits >> 3);
  276. for (unsigned int i = 0; i < sizeBits; i++)
  277. dstBytes[i >> 3] |= (srcBits[i] << (7 - (i & 7)));
  278. }
  279. void CDes::Int2Bits(unsigned int _src, char* dstBits)
  280. {
  281. for (unsigned int i = 0; i < 4; i++)
  282. dstBits[i] = ((_src << i) & 8) >> 3;
  283. }
  284. void CDes::Bits2Hex(char *dstHex, char* srcBits, unsigned int sizeBits)
  285. {
  286. memset(dstHex, 0, sizeBits >> 2);
  287. for (unsigned int i = 0; i < sizeBits; i++) //convert to int 0-15
  288. dstHex[i >> 2] += (srcBits[i] << (3 - (i & 3)));
  289. for (unsigned int j = 0; j < (sizeBits >> 2); j++)
  290. dstHex[j] += dstHex[j] > 9 ? 55 : 48; //convert to char '0'-'F'
  291. }
  292. void CDes::Hex2Bits(char *srcHex, char* dstBits, unsigned int sizeBits)
  293. {
  294. memset(dstBits, 0, sizeBits);
  295. for (unsigned int i = 0; i < (sizeBits >> 2); i++)
  296. srcHex[i] -= srcHex[i] > 64 ? 55 : 48; //convert to char int 0-15
  297. for (unsigned int j = 0; j < sizeBits; j++)
  298. dstBits[j] = ((srcHex[j >> 2] << (j & 3)) & 15) >> 3;
  299. }
  300. char* CDes::GetCiphertextInBinary()
  301. {
  302. for (unsigned int i = 0; i < 64; i++)
  303. {
  304. szCiphertextInBinary[i] = szCiphertextRaw[i] + 48; // from int(0) to char('0') and int1 to char('1')
  305. }
  306. szCiphertextInBinary[64] = '\0';
  307. return szCiphertextInBinary;
  308. }
  309. char* CDes::GetCiphertextInHex()
  310. {
  311. Bits2Hex(szCiphertextInHex, szCiphertextRaw, 64);
  312. szCiphertextInHex[16] = '\0';
  313. return szCiphertextInHex;
  314. }
  315. char* CDes::GetCiphertextInBytes()
  316. {
  317. return szCiphertextInBytes;
  318. }
  319. char* CDes::GetPlaintext()
  320. {
  321. memcpy(szPlaintext, szPlaintextInBytes, 8);
  322. szPlaintext[8] = '\0';
  323. return szPlaintext;
  324. }
  325. char* CDes::GetCiphertextAnyLength()
  326. {
  327. return szFCiphertextAnyLength;
  328. }
  329. char* CDes::GetPlaintextAnyLength()
  330. {
  331. return szFPlaintextAnyLength;
  332. }
  333. void CDes::EncryptAnyLength(char* _srcBytes, unsigned int _bytesLength, unsigned int keyN)
  334. {
  335. if (_bytesLength == 8)
  336. {
  337. EncryptData(_srcBytes, keyN);
  338. memcpy(szFCiphertextAnyLength, szCiphertextInBytes, 8);
  339. szFCiphertextAnyLength[8] = '\0';
  340. }
  341. else if (_bytesLength < 8)
  342. {
  343. char _temp8bytes[8] = { 0 };
  344. memcpy(_temp8bytes, _srcBytes, _bytesLength);
  345. EncryptData(_temp8bytes, keyN);
  346. memcpy(szFCiphertextAnyLength, szCiphertextInBytes, 8);
  347. szFCiphertextAnyLength[8] = '\0';
  348. }
  349. else if (_bytesLength > 8)
  350. {
  351. int iParts = _bytesLength >> 3;
  352. int iResidue = _bytesLength % 8;
  353. char szLast8Bits[8] = { 0 };
  354. for (int i = 0; i < iParts; i++)
  355. {
  356. memcpy(szLast8Bits, _srcBytes + (i << 3), 8);
  357. EncryptData(szLast8Bits, keyN);
  358. memcpy(szFCiphertextAnyLength + (i << 3), szCiphertextInBytes, 8);
  359. }
  360. memset(szLast8Bits, 0, 8);
  361. memcpy(szLast8Bits, _srcBytes + (iParts << 3), iResidue);
  362. EncryptData(szLast8Bits, keyN);
  363. memcpy(szFCiphertextAnyLength + (iParts << 3), szCiphertextInBytes, 8);
  364. szFCiphertextAnyLength[((iParts + 1) << 3)] = '\0';
  365. }
  366. }
  367. void CDes::DecryptAnyLength(char* _srcBytes, unsigned int _bytesLength, unsigned int keyN)
  368. {
  369. if (_bytesLength == 8)
  370. {
  371. DecryptData(_srcBytes, keyN);
  372. memcpy(szFPlaintextAnyLength, szPlaintextInBytes, 8);
  373. szFPlaintextAnyLength[8] = '\0';
  374. }
  375. else if (_bytesLength < 8)
  376. {
  377. char _temp8bytes[8] = { 0 };
  378. memcpy(_temp8bytes, _srcBytes, 8);
  379. DecryptData(_temp8bytes, keyN);
  380. memcpy(szFPlaintextAnyLength, szPlaintextInBytes, _bytesLength);
  381. szFPlaintextAnyLength[_bytesLength] = '\0';
  382. }
  383. else if (_bytesLength > 8)
  384. {
  385. int iParts = _bytesLength >> 3;
  386. int iResidue = _bytesLength % 8;
  387. char szLast8Bits[8] = { 0 };
  388. for (int i = 0; i < iParts; i++)
  389. {
  390. memcpy(szLast8Bits, _srcBytes + (i << 3), 8);
  391. DecryptData(szLast8Bits, keyN);
  392. memcpy(szFPlaintextAnyLength + (i << 3), szPlaintextInBytes, 8);
  393. }
  394. if (iResidue != 0)
  395. {
  396. memset(szLast8Bits, 0, 8);
  397. memcpy(szLast8Bits, _srcBytes + (iParts << 3), 8);
  398. DecryptData(szLast8Bits, keyN);
  399. memcpy(szFPlaintextAnyLength + (iParts << 3), szPlaintextInBytes, iResidue);
  400. }
  401. szFPlaintextAnyLength[_bytesLength] = '\0';
  402. }
  403. }
  404. /dlj
  405. CString CDes::Encrypt(CString strPlaintext,BOOL bIsPwd)
  406. {
  407. if (bIsPwd)
  408. {
  409. InitializeKey(PWDKEY, 0);
  410. }
  411. else
  412. {
  413. InitializeKey(KEY, 0);
  414. }
  415. USES_CONVERSION;
  416. char *szPlaintextData = T2A(strPlaintext);
  417. //加密任意长度的字符串
  418. EncryptAnyLength(szPlaintextData, strlen(szPlaintextData), 0);
  419. ConvertCiphertext2OtherFormat(strlen(szPlaintextData) % 8 == 0 ? strlen(szPlaintextData) << 3 : ((strlen(szPlaintextData) >> 3) + 1) << 6, GetCiphertextAnyLength());
  420. return A2W(hexCiphertextAnyLength);
  421. }
  422. void CDes::ConvertCiphertext2OtherFormat(int iBitsLen, char *szCipherInBytes)
  423. {
  424. memset(hexCiphertextAnyLength, 0, 16384);
  425. memset(bitsCiphertextAnyLength, 0, 32768);
  426. Bytes2Bits(szCipherInBytes, bitsCiphertextAnyLength, iBitsLen);
  427. Bits2Hex(hexCiphertextAnyLength, bitsCiphertextAnyLength, iBitsLen);
  428. for (int i = 0; i < iBitsLen; i++)
  429. {
  430. bitsCiphertextAnyLength[i] += 48;
  431. }
  432. }
  433. CString CDes::Decrypt(CString strCiphertext,BOOL bIsPwd)
  434. {
  435. USES_CONVERSION;
  436. if (bIsPwd)
  437. {
  438. InitializeKey(PWDKEY, 0);
  439. }
  440. else
  441. {
  442. InitializeKey(KEY, 0);
  443. }
  444. //InitializeKey(KEY, 0);
  445. char *szCiphertext = T2A(strCiphertext);
  446. int iLen = 0;
  447. char szCiphertextData[8192];
  448. memset(szCiphertextData, 0, 8192);
  449. iLen = ((strlen(szCiphertext) >> 2) + (strlen(szCiphertext) % 4 == 0 ? 0 : 1)) << 4;
  450. memset(hexCiphertextAnyLength, 0, 16384);
  451. memcpy(hexCiphertextAnyLength, szCiphertext, strlen(szCiphertext));
  452. Hex2Bits(hexCiphertextAnyLength, bitsCiphertextAnyLength, iLen);
  453. Bits2Bytes(szCiphertextData, bitsCiphertextAnyLength, iLen);
  454. DecryptAnyLength(szCiphertextData, iLen>>3, 0);
  455. char *temp=GetPlaintextAnyLength();
  456. return A2W(temp);
  457. }

md5.h

  1. #pragma once
  2. typedef struct
  3. {
  4. unsigned int count[2];
  5. unsigned int state[4];
  6. unsigned char buffer[64];
  7. }MD5_CTX;
  8. #define F(x,y,z) ((x & y) | (~x & z))
  9. #define G(x,y,z) ((x & z) | (y & ~z))
  10. #define H(x,y,z) (x^y^z)
  11. #define I(x,y,z) (y ^ (x | ~z))
  12. #define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))
  13. #define FF(a,b,c,d,x,s,ac) \
  14. {\
  15. a += F(b, c, d) + x + ac; \
  16. a = ROTATE_LEFT(a, s); \
  17. a += b; \
  18. }
  19. #define GG(a,b,c,d,x,s,ac) \
  20. { \
  21. a += G(b, c, d) + x + ac; \
  22. a = ROTATE_LEFT(a, s); \
  23. a += b; \
  24. }
  25. #define HH(a,b,c,d,x,s,ac) \
  26. { \
  27. a += H(b, c, d) + x + ac; \
  28. a = ROTATE_LEFT(a, s); \
  29. a += b; \
  30. }
  31. #define II(a,b,c,d,x,s,ac) \
  32. { \
  33. a += I(b, c, d) + x + ac; \
  34. a = ROTATE_LEFT(a, s); \
  35. a += b; \
  36. }
  37. class CMD5
  38. {
  39. public:
  40. CMD5();
  41. ~CMD5();
  42. void MD5Init(MD5_CTX *context);
  43. void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen);
  44. void MD5Final(MD5_CTX *context, unsigned char digest[16]);
  45. void MD5Transform(unsigned int state[4], unsigned char block[64]);
  46. void MD5Encode(unsigned char *output, unsigned int *input, unsigned int len);
  47. void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len);
  48. CString Encrypt16(CString str);
  49. CString Encrypt32(CString str);
  50. };

md5.cpp

  1. #include "stdafx.h"
  2. #include "MD5.h"
  3. unsigned char PADDING[] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  7. CMD5::CMD5()
  8. {
  9. }
  10. CMD5::~CMD5()
  11. {
  12. }
  13. void CMD5::MD5Init(MD5_CTX *context)
  14. {
  15. context->count[0] = 0;
  16. context->count[1] = 0;
  17. context->state[0] = 0x67452301;
  18. context->state[1] = 0xEFCDAB89;
  19. context->state[2] = 0x98BADCFE;
  20. context->state[3] = 0x10325476;
  21. }
  22. void CMD5::MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen)
  23. {
  24. unsigned int i = 0, index = 0, partlen = 0;
  25. index = (context->count[0] >> 3) & 0x3F;
  26. partlen = 64 - index;
  27. context->count[0] += inputlen << 3;
  28. if (context->count[0] < (inputlen << 3))
  29. context->count[1]++;
  30. context->count[1] += inputlen >> 29;
  31. if (inputlen >= partlen)
  32. {
  33. memcpy(&context->buffer[index], input, partlen);
  34. MD5Transform(context->state, context->buffer);
  35. for (i = partlen; i + 64 <= inputlen; i += 64)
  36. MD5Transform(context->state, &input[i]);
  37. index = 0;
  38. }
  39. else
  40. {
  41. i = 0;
  42. }
  43. memcpy(&context->buffer[index], &input[i], inputlen - i);
  44. }
  45. void CMD5::MD5Final(MD5_CTX *context, unsigned char digest[16])
  46. {
  47. unsigned int index = 0, padlen = 0;
  48. unsigned char bits[8];
  49. index = (context->count[0] >> 3) & 0x3F;
  50. padlen = (index < 56) ? (56 - index) : (120 - index);
  51. MD5Encode(bits, context->count, 8);
  52. MD5Update(context, PADDING, padlen);
  53. MD5Update(context, bits, 8);
  54. MD5Encode(digest, context->state, 16);
  55. }
  56. void CMD5::MD5Encode(unsigned char *output, unsigned int *input, unsigned int len)
  57. {
  58. unsigned int i = 0, j = 0;
  59. while (j < len)
  60. {
  61. output[j] = input[i] & 0xFF;
  62. output[j + 1] = (input[i] >> 8) & 0xFF;
  63. output[j + 2] = (input[i] >> 16) & 0xFF;
  64. output[j + 3] = (input[i] >> 24) & 0xFF;
  65. i++;
  66. j += 4;
  67. }
  68. }
  69. void CMD5::MD5Decode(unsigned int *output, unsigned char *input, unsigned int len)
  70. {
  71. unsigned int i = 0, j = 0;
  72. while (j < len)
  73. {
  74. output[i] = (input[j]) |
  75. (input[j + 1] << 8) |
  76. (input[j + 2] << 16) |
  77. (input[j + 3] << 24);
  78. i++;
  79. j += 4;
  80. }
  81. }
  82. void CMD5::MD5Transform(unsigned int state[4], unsigned char block[64])
  83. {
  84. unsigned int a = state[0];
  85. unsigned int b = state[1];
  86. unsigned int c = state[2];
  87. unsigned int d = state[3];
  88. unsigned int x[64];
  89. MD5Decode(x, block, 64);
  90. FF(a, b, c, d, x[0], 7, 0xd76aa478);
  91. FF(d, a, b, c, x[1], 12, 0xe8c7b756);
  92. FF(c, d, a, b, x[2], 17, 0x242070db);
  93. FF(b, c, d, a, x[3], 22, 0xc1bdceee);
  94. FF(a, b, c, d, x[4], 7, 0xf57c0faf);
  95. FF(d, a, b, c, x[5], 12, 0x4787c62a);
  96. FF(c, d, a, b, x[6], 17, 0xa8304613);
  97. FF(b, c, d, a, x[7], 22, 0xfd469501);
  98. FF(a, b, c, d, x[8], 7, 0x698098d8);
  99. FF(d, a, b, c, x[9], 12, 0x8b44f7af);
  100. FF(c, d, a, b, x[10], 17, 0xffff5bb1);
  101. FF(b, c, d, a, x[11], 22, 0x895cd7be);
  102. FF(a, b, c, d, x[12], 7, 0x6b901122);
  103. FF(d, a, b, c, x[13], 12, 0xfd987193);
  104. FF(c, d, a, b, x[14], 17, 0xa679438e);
  105. FF(b, c, d, a, x[15], 22, 0x49b40821);
  106. GG(a, b, c, d, x[1], 5, 0xf61e2562);
  107. GG(d, a, b, c, x[6], 9, 0xc040b340);
  108. GG(c, d, a, b, x[11], 14, 0x265e5a51);
  109. GG(b, c, d, a, x[0], 20, 0xe9b6c7aa);
  110. GG(a, b, c, d, x[5], 5, 0xd62f105d);
  111. GG(d, a, b, c, x[10], 9, 0x2441453);
  112. GG(c, d, a, b, x[15], 14, 0xd8a1e681);
  113. GG(b, c, d, a, x[4], 20, 0xe7d3fbc8);
  114. GG(a, b, c, d, x[9], 5, 0x21e1cde6);
  115. GG(d, a, b, c, x[14], 9, 0xc33707d6);
  116. GG(c, d, a, b, x[3], 14, 0xf4d50d87);
  117. GG(b, c, d, a, x[8], 20, 0x455a14ed);
  118. GG(a, b, c, d, x[13], 5, 0xa9e3e905);
  119. GG(d, a, b, c, x[2], 9, 0xfcefa3f8);
  120. GG(c, d, a, b, x[7], 14, 0x676f02d9);
  121. GG(b, c, d, a, x[12], 20, 0x8d2a4c8a);
  122. HH(a, b, c, d, x[5], 4, 0xfffa3942);
  123. HH(d, a, b, c, x[8], 11, 0x8771f681);
  124. HH(c, d, a, b, x[11], 16, 0x6d9d6122);
  125. HH(b, c, d, a, x[14], 23, 0xfde5380c);
  126. HH(a, b, c, d, x[1], 4, 0xa4beea44);
  127. HH(d, a, b, c, x[4], 11, 0x4bdecfa9);
  128. HH(c, d, a, b, x[7], 16, 0xf6bb4b60);
  129. HH(b, c, d, a, x[10], 23, 0xbebfbc70);
  130. HH(a, b, c, d, x[13], 4, 0x289b7ec6);
  131. HH(d, a, b, c, x[0], 11, 0xeaa127fa);
  132. HH(c, d, a, b, x[3], 16, 0xd4ef3085);
  133. HH(b, c, d, a, x[6], 23, 0x4881d05);
  134. HH(a, b, c, d, x[9], 4, 0xd9d4d039);
  135. HH(d, a, b, c, x[12], 11, 0xe6db99e5);
  136. HH(c, d, a, b, x[15], 16, 0x1fa27cf8);
  137. HH(b, c, d, a, x[2], 23, 0xc4ac5665);
  138. II(a, b, c, d, x[0], 6, 0xf4292244);
  139. II(d, a, b, c, x[7], 10, 0x432aff97);
  140. II(c, d, a, b, x[14], 15, 0xab9423a7);
  141. II(b, c, d, a, x[5], 21, 0xfc93a039);
  142. II(a, b, c, d, x[12], 6, 0x655b59c3);
  143. II(d, a, b, c, x[3], 10, 0x8f0ccc92);
  144. II(c, d, a, b, x[10], 15, 0xffeff47d);
  145. II(b, c, d, a, x[1], 21, 0x85845dd1);
  146. II(a, b, c, d, x[8], 6, 0x6fa87e4f);
  147. II(d, a, b, c, x[15], 10, 0xfe2ce6e0);
  148. II(c, d, a, b, x[6], 15, 0xa3014314);
  149. II(b, c, d, a, x[13], 21, 0x4e0811a1);
  150. II(a, b, c, d, x[4], 6, 0xf7537e82);
  151. II(d, a, b, c, x[11], 10, 0xbd3af235);
  152. II(c, d, a, b, x[2], 15, 0x2ad7d2bb);
  153. II(b, c, d, a, x[9], 21, 0xeb86d391);
  154. state[0] += a;
  155. state[1] += b;
  156. state[2] += c;
  157. state[3] += d;
  158. }
  159. CString CMD5::Encrypt16(CString str)
  160. {
  161. MD5_CTX md5;
  162. MD5Init(&md5);
  163. int i;
  164. USES_CONVERSION;
  165. char *chTemp = T2A(str);
  166. unsigned char *encrypt = (unsigned char*)chTemp;// = T2A(str);//21232f297a57a5a743894a0e4a801fc3
  167. unsigned char decrypt[16];
  168. MD5Update(&md5, encrypt, strlen((char *)encrypt));
  169. MD5Final(&md5, decrypt);
  170. CString szRe;
  171. for (i = 4; i < 12; i++)
  172. {
  173. CString strtemp;
  174. strtemp.Format(L"%02x", decrypt[i]);
  175. szRe += strtemp;
  176. printf("%02x", decrypt[i]); //02x前需要加上 %
  177. }
  178. return szRe;
  179. }
  180. CString CMD5::Encrypt32(CString str)
  181. {
  182. MD5_CTX md5;
  183. MD5Init(&md5);
  184. int i;
  185. USES_CONVERSION;
  186. char *chTemp = T2A(str);
  187. unsigned char *encrypt = (unsigned char*)chTemp;// = T2A(str);//21232f297a57a5a743894a0e4a801fc3
  188. unsigned char decrypt[16];
  189. MD5Update(&md5, encrypt, strlen((char *)encrypt));
  190. MD5Final(&md5, decrypt);
  191. CString szRe;
  192. for (i = 0; i < 16; i++)
  193. {
  194. CString strtemp;
  195. strtemp.Format(L"%02x", decrypt[i]);
  196. szRe += strtemp;
  197. printf("%02x", decrypt[i]); //02x前需要加上 %
  198. }
  199. return szRe;
  200. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/990852
推荐阅读
相关标签
  

闽ICP备14008679号