用AIP获得MD5哈希密码
此文介绍利用AIP获得16位哈希码,在此基础上获得8位(长度可修改)哈希密码的方法。
代码
1 //-------------------------------------------------------------------
2 // 函数名: makeMD5
3 // 功能: 算出MD5哈希密码
4 // In: pParam 输入参数
5 // Out: outBuffer MD5输出
6 // 返回值:
7 // 注释:
8 //-------------------------------------------------------------------
9 bool makeMD5(const char* const pParam, char* outBuffer)
10 {
11 HCRYPTPROV hCryptProv = NULL;
12 HCRYPTHASH hHash = NULL;
13
14 if(!::CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
15 {
16 if(!::CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
17 {
18 return false;
19 }
20 }
21
22 if (!::CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))
23 {
24 return false;
25 }
26
27 if (hHash)
28 {
29 if (!::CryptHashData(hHash, (BYTE*)pParam, (DWORD)strlen(pParam), 0))
30 {
31 return false;
32 }
33
34 BYTE bData[1024];
35 memset(bData, 0, sizeof(bData));
36 CHAR rgbDigits[] = "0123456789abcdef";
37 DWORD dwDataLen = 16;
38 if (!::CryptGetHashParam(hHash, HP_HASHVAL, bData, &dwDataLen, 0))
39 {
40 return false;
41 }
42
43 int md5_index = 0;
44 for (DWORD i = 0; i < dwDataLen; ++i)
45 {
46 outBuffer[md5_index++] = rgbDigits[bData[i] >> 4];
47 outBuffer[md5_index++] = rgbDigits[bData[i] & 0xf];
48 }
49
50 ::CryptDestroyHash(hHash);
51
52 if (hCryptProv)
53 {
54 ::CryptReleaseContext(hCryptProv,0);
55 }
56
57 return true;
58 }
59
60 if (hCryptProv)
61 {
62 ::CryptReleaseContext(hCryptProv,0);
63 }
64
65 return false;
66 }
67
68
69 //-------------------------------------------------------------------
70 // 函数名: subHashMD5
71 // 功能: 处理MD5哈希密码
72 // In: pTxt 输入参数
73 // Out: outHash 生成的哈希密码
74 // 返回值:
75 // 注释:
76 //-------------------------------------------------------------------
77 void subHashMD5(const string &pTxt, string *outHash)
78 {
79 const int HASH_LEN = 16;
80 const int BUF_SIZE = 50;
81
82 char cMD5Hash[BUF_SIZE];
83
84 memset(cMD5Hash, 0, sizeof(cMD5Hash));
85
86 // 计算MD5哈希密码
87 if (makeMD5(pTxt.c_str(), cMD5Hash))
88 {
89 // ①32位的16进制哈希值4位一段分为8段
90 // ②8段16进制数转换为10进制数
91 // ③8个16进制数连接为一个8位的密码
92
93 char tempBuf[BUF_SIZE];
94 string strMD5(cMD5Hash);
95
96 strHexToInt(strMD5.substr(0, 4), BUF_SIZE, tempBuf);
97 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 1
98
99 strHexToInt(strMD5.substr(4, 4), BUF_SIZE, tempBuf);
100 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 2
101
102 strHexToInt(strMD5.substr(8, 4), BUF_SIZE, tempBuf);
103 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 3
104
105 strHexToInt(strMD5.substr(12, 4), BUF_SIZE, tempBuf);
106 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 4
107
108 strHexToInt(strMD5.substr(16, 4), BUF_SIZE, tempBuf);
109 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 5
110
111 strHexToInt(strMD5.substr(20, 4), BUF_SIZE, tempBuf);
112 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 6
113
114 strHexToInt(strMD5.substr(24, 4), BUF_SIZE, tempBuf);
115 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 7
116
117 strHexToInt(strMD5.substr(28, 4), BUF_SIZE, tempBuf);
118 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 8
119 }
120 }
121
122 //-------------------------------------------------------------------
123 // 函数名: strHexToInt
124 // 功能: 16进制数转换为10进制数
125 // In: pHex 16进制字符串
126 // pSize out长度
127 // Out: outStr 10进制数字符串
128 // 返回值:
129 // 注释:
130 //-------------------------------------------------------------------
131 void strHexToInt(const string &pHex, const int pSize, char *outStr)
132 {
133 int nValue;
134 sscanf_s(pHex.c_str(), "%x", &nValue);
135
136 memset(outStr, 0, pSize);
137 _itoa_s(nValue, outStr, pSize, 10);
138 }
2 // 函数名: makeMD5
3 // 功能: 算出MD5哈希密码
4 // In: pParam 输入参数
5 // Out: outBuffer MD5输出
6 // 返回值:
7 // 注释:
8 //-------------------------------------------------------------------
9 bool makeMD5(const char* const pParam, char* outBuffer)
10 {
11 HCRYPTPROV hCryptProv = NULL;
12 HCRYPTHASH hHash = NULL;
13
14 if(!::CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
15 {
16 if(!::CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
17 {
18 return false;
19 }
20 }
21
22 if (!::CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))
23 {
24 return false;
25 }
26
27 if (hHash)
28 {
29 if (!::CryptHashData(hHash, (BYTE*)pParam, (DWORD)strlen(pParam), 0))
30 {
31 return false;
32 }
33
34 BYTE bData[1024];
35 memset(bData, 0, sizeof(bData));
36 CHAR rgbDigits[] = "0123456789abcdef";
37 DWORD dwDataLen = 16;
38 if (!::CryptGetHashParam(hHash, HP_HASHVAL, bData, &dwDataLen, 0))
39 {
40 return false;
41 }
42
43 int md5_index = 0;
44 for (DWORD i = 0; i < dwDataLen; ++i)
45 {
46 outBuffer[md5_index++] = rgbDigits[bData[i] >> 4];
47 outBuffer[md5_index++] = rgbDigits[bData[i] & 0xf];
48 }
49
50 ::CryptDestroyHash(hHash);
51
52 if (hCryptProv)
53 {
54 ::CryptReleaseContext(hCryptProv,0);
55 }
56
57 return true;
58 }
59
60 if (hCryptProv)
61 {
62 ::CryptReleaseContext(hCryptProv,0);
63 }
64
65 return false;
66 }
67
68
69 //-------------------------------------------------------------------
70 // 函数名: subHashMD5
71 // 功能: 处理MD5哈希密码
72 // In: pTxt 输入参数
73 // Out: outHash 生成的哈希密码
74 // 返回值:
75 // 注释:
76 //-------------------------------------------------------------------
77 void subHashMD5(const string &pTxt, string *outHash)
78 {
79 const int HASH_LEN = 16;
80 const int BUF_SIZE = 50;
81
82 char cMD5Hash[BUF_SIZE];
83
84 memset(cMD5Hash, 0, sizeof(cMD5Hash));
85
86 // 计算MD5哈希密码
87 if (makeMD5(pTxt.c_str(), cMD5Hash))
88 {
89 // ①32位的16进制哈希值4位一段分为8段
90 // ②8段16进制数转换为10进制数
91 // ③8个16进制数连接为一个8位的密码
92
93 char tempBuf[BUF_SIZE];
94 string strMD5(cMD5Hash);
95
96 strHexToInt(strMD5.substr(0, 4), BUF_SIZE, tempBuf);
97 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 1
98
99 strHexToInt(strMD5.substr(4, 4), BUF_SIZE, tempBuf);
100 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 2
101
102 strHexToInt(strMD5.substr(8, 4), BUF_SIZE, tempBuf);
103 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 3
104
105 strHexToInt(strMD5.substr(12, 4), BUF_SIZE, tempBuf);
106 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 4
107
108 strHexToInt(strMD5.substr(16, 4), BUF_SIZE, tempBuf);
109 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 5
110
111 strHexToInt(strMD5.substr(20, 4), BUF_SIZE, tempBuf);
112 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 6
113
114 strHexToInt(strMD5.substr(24, 4), BUF_SIZE, tempBuf);
115 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 7
116
117 strHexToInt(strMD5.substr(28, 4), BUF_SIZE, tempBuf);
118 outHash->append( & tempBuf[strlen(tempBuf) - 1]); // 8
119 }
120 }
121
122 //-------------------------------------------------------------------
123 // 函数名: strHexToInt
124 // 功能: 16进制数转换为10进制数
125 // In: pHex 16进制字符串
126 // pSize out长度
127 // Out: outStr 10进制数字符串
128 // 返回值:
129 // 注释:
130 //-------------------------------------------------------------------
131 void strHexToInt(const string &pHex, const int pSize, char *outStr)
132 {
133 int nValue;
134 sscanf_s(pHex.c_str(), "%x", &nValue);
135
136 memset(outStr, 0, pSize);
137 _itoa_s(nValue, outStr, pSize, 10);
138 }
欢迎指正.

浙公网安备 33010602011771号