{"id":429,"date":"2020-12-13T00:05:46","date_gmt":"2020-12-12T16:05:46","guid":{"rendered":"http:\/\/moyisuiying.com\/?p=429"},"modified":"2020-12-13T00:05:46","modified_gmt":"2020-12-12T16:05:46","slug":"c%e5%ae%9e%e7%8e%b0md5%e6%91%98%e8%a6%81%e7%ae%97%e6%b3%95%e5%8a%a0%e7%9b%90salt%e5%80%bc","status":"publish","type":"post","link":"http:\/\/moyisuiying.com\/index.php\/cppstudy\/information-secure\/429.html","title":{"rendered":"C++\u5b9e\u73b0MD5\u6458\u8981\u7b97\u6cd5\u52a0\u76d0salt\u503c"},"content":{"rendered":"\n<h1>C++\u5b9e\u73b0MD5\u6458\u8981\u7b97\u6cd5\u52a0\u76d0salt\u503c<\/h1>\n\n\n\n<h2>1.\u4fe1\u606f\u6458\u8981\u51fd\u6570<\/h2>\n\n\n\n<h3>1.1Hash\u51fd\u6570<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u3000\u3000\u54c8\u5e0c\u51fd\u6570\u5c31\u662f\u80fd\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u6570\u636e\u6620\u5c04\u4e3a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u636e\u7684\u51fd\u6570\u3002\u54c8\u5e0c\u51fd\u6570\u8fd4\u56de\u7684\u503c\u88ab\u53eb\u505a\u54c8\u5e0c\u503c\u3001\u54c8\u5e0c\u7801\u3001\u6563\u5217\uff0c\u6216\u8005\u76f4\u63a5\u53eb\u505a\u54c8\u5e0c\u3002<\/p><\/blockquote>\n\n\n\n<h3>1.2\u6d88\u606f\u6458\u8981<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u3000 \u5c06\u957f\u5ea6\u4e0d\u56fa\u5b9a\u7684\u6d88\u606f\uff08message)\u4f5c\u4e3a\u8f93\u5165\u53c2\u6570\uff0c\u8fd0\u884c\u7279\u5b9a\u7684Hash\u51fd\u6570\uff0c\u751f\u6210\u56fa\u5b9a\u957f\u5ea6\u7684\u8f93\u51fa\uff0c\u8fd9\u4e2a\u8f93\u51fa\u5c31\u662fHash\uff0c\u4e5f\u79f0\u4e3a\u8fd9\u4e2a\u6d88\u606f\u7684\u6d88\u606f\u6458\u8981\uff08Message Digest\uff09<\/p><\/blockquote>\n\n\n\n<h3>1.3\u4fe1\u606f\u6458\u8981\u7b97\u6cd5\u662fhash\u7b97\u6cd5\u7684\u4e00\u79cd\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a<\/h3>\n\n\n\n<p>\u2460\u65e0\u8bba\u8f93\u5165\u7684\u6d88\u606f\u6709\u591a\u957f\uff0c\u8ba1\u7b97\u51fa\u6765\u7684\u6d88\u606f\u6458\u8981\u7684\u957f\u5ea6\u603b\u662f\u56fa\u5b9a\u7684\uff0c\u8ba1\u7b97\u51fa\u7684\u7ed3\u679c\u8d8a\u957f\uff0c\u4e00\u822c\u8ba4\u4e3a\u8be5\u6458\u8981\u7b97\u6cd5\u8d8a\u5b89\u5168\uff0cMD5 128\u4f4d SHA-1 160\u4f4d<\/p>\n\n\n\n<p>\u2461\u8f93\u5165\u7684\u6d88\u606f\u4e0d\u540c\uff0c\u4ea7\u751f\u7684\u6d88\u606f\u6458\u8981\u5fc5\u4e0d\u540c\uff0c\u8f93\u5165\u7684\u6d88\u606f\u76f8\u540c\uff0c\u4ea7\u751f\u7684\u6d88\u606f\u6458\u8981\u4e00\u5b9a\u662f\u76f8\u540c\u7684<\/p>\n\n\n\n<p>\u2462\u5355\u5411\u4e0d\u53ef\u9006\u3002<\/p>\n\n\n\n<p>\u2463\u6d88\u606f\u6458\u8981\u770b\u8d77\u6765\u662f\u201c\u968f\u673a\u7684\u3002\u8fd9\u4e9b\u6bd4\u7279\u770b\u4e0a\u53bb\u662f\u80e1\u4e71\u7684\u6742\u51d1\u5728\u4e00\u8d77\u7684\u3002\u53ef\u4ee5\u7528\u5927\u91cf\u7684\u8f93\u5165\u6765\u68c0\u9a8c\u5176\u8f93\u51fa\u662f\u5426\u76f8\u540c\uff0c\u4e00\u822c\uff0c\u4e0d\u540c\u7684\u8f93\u5165\u4f1a\u6709\u4e0d\u540c\u7684\u8f93\u51fa\uff0c\u800c\u4e14\u8f93\u51fa\u7684\u6458\u8981\u6d88\u606f\u53ef\u4ee5\u901a\u8fc7\u968f\u673a\u6027\u68c0\u9a8c\u3002\u4f46\u662f\uff0c\u4e00\u4e2a\u6458\u8981\u5e76\u4e0d\u662f\u771f\u6b63\u968f\u673a\u7684\uff0c\u56e0\u4e3a\u7528\u76f8\u540c\u7684\u7b97\u6cd5\u5bf9\u76f8\u540c\u7684\u6d88\u606f\u6c42\u4e24\u6b21\u6458\u8981\uff0c\u5176\u7ed3\u679c\u5fc5\u7136\u76f8\u540c\uff1b\u800c\u82e5\u662f\u771f\u6b63\u968f\u673a\u7684\uff0c\u5219\u65e0\u8bba\u5982\u4f55\u90fd\u662f\u65e0\u6cd5\u91cd\u73b0\u7684\u3002\u56e0\u6b64\u6d88\u606f\u6458\u8981\u662f\u201c\u4f2a\u968f\u673a\u7684\u201d\u3002<\/p>\n\n\n\n<p>\u2464\u597d\u7684\u6458\u8981\u7b97\u6cd5\uff0c\u6ca1\u6709\u4eba\u80fd\u4ece\u4e2d\u627e\u5230\u201c\u78b0\u649e\u201d\uff0c\u867d\u7136\u201c\u78b0\u649e\u201d\u662f\u80af\u5b9a\u5b58\u5728\u7684\u3002\u5373\u5bf9\u4e8e\u7ed9\u5b9a\u7684\u4e00\u4e2a\u6458\u8981\uff0c\u4e0d\u53ef\u80fd\u627e\u5230\u4e00\u6761\u4fe1\u606f\u4f7f\u5176\u6458\u8981\u6b63\u597d\u662f\u7ed9\u5b9a\u7684\u3002\u6216\u8005\u8bf4\uff0c\u65e0\u6cd5\u627e\u5230\u4e24\u6761\u6d88\u606f\uff0c\u662f\u5b83\u4eec\u7684\u6458\u8981\u76f8\u540c\u3002<\/p>\n\n\n\n<h3>1.4\u6d88\u606f\u6458\u8981\u7684\u5e94\u7528\u2014\u2014\u6570\u5b57\u7b7e\u540d<\/h3>\n\n\n\n<h4>1.4.1\u6570\u636e\u7684\u5b8c\u6574\u6027<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u6570\u636e\u7684\u5b8c\u6574\u6027\u662f\u6307\u4fe1\u5bbf\u63a5\u6536\u5230\u7684\u6d88\u606f\u4e00\u5b9a\u662f\u4fe1\u6e90\u53d1\u9001\u7684\u4fe1\u606f\uff0c\u800c\u4e2d\u95f4\u7edd\u65e0\u4efb\u4f55\u66f4\u6539\u3002<\/p><\/blockquote>\n\n\n\n<h4>1.4.2\u4fe1\u606f\u7684\u4e0d\u53ef\u5426\u8ba4\u6027<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u4fe1\u606f\u7684\u4e0d\u53ef\u5426\u8ba4\u6027\u662f\u6307\u4fe1\u6e90\u4e0d\u80fd\u5426\u8ba4\u66fe\u7ecf\u53d1\u9001\u8fc7\u7684\u4fe1\u606f<\/p><\/blockquote>\n\n\n\n<p>\u200b \u4e00\u822c\u5730\uff0c\u628a\u5bf9\u4e00\u4e2a\u4fe1\u606f\u7684\u6458\u8981\u79f0\u4e3a\u8be5\u6d88\u606f\u7684\u6307\u7eb9\u6216\u6570\u5b57\u7b7e\u540d\u3002\u6570\u5b57\u7b7e\u540d\u662f\u4fdd\u8bc1\u4fe1\u606f\u7684\u5b8c\u6574\u6027\u548c\u4e0d\u53ef\u5426\u8ba4\u6027\u7684\u65b9\u6cd5\u3002\u5176\u5b9e\uff0c\u901a\u8fc7\u6570\u5b57\u7b7e\u540d\u8fd8\u80fd\u5b9e\u73b0\u5bf9\u4fe1\u6e90\u7684\u8eab\u4efd\u8bc6\u522b\uff08\u8ba4\u8bc1\uff09\uff0c\u5373\u786e\u5b9a\u201c\u4fe1\u6e90\u201d\u662f\u5426\u662f\u4fe1\u5bbf\u610f\u5b9a\u7684\u901a\u4fe1\u4f19\u4f34\u3002<\/p>\n\n\n\n<p>\u200b \u6570\u5b57\u7b7e\u540d\u5e94\u8be5\u5177\u6709<strong>\u552f\u4e00\u6027<\/strong>\uff0c\u5373\u4e0d\u540c\u7684\u6d88\u606f\u7684\u7b7e\u540d\u662f\u4e0d\u4e00\u6837\u7684\uff1b\u540c\u65f6\u8fd8\u5e94\u5177\u6709<strong>\u4e0d\u53ef\u4f2a\u9020\u6027<\/strong>\uff0c\u5373\u4e0d\u53ef\u80fd\u627e\u5230\u53e6\u4e00\u4e2a\u6d88\u606f\uff0c\u4f7f\u5176\u7b7e\u540d\u4e0e\u5df2\u6709\u7684\u6d88\u606f\u7684\u7b7e\u540d\u4e00\u6837\uff1b\u8fd8\u5e94\u5177\u6709<strong>\u4e0d\u53ef\u9006\u6027<\/strong>\uff0c\u5373\u65e0\u6cd5\u6839\u636e\u7b7e\u540d\u8fd8\u539f\u88ab\u7b7e\u540d\u7684\u6d88\u606f\u7684\u4efb\u4f55\u4fe1\u606f\u3002\u8fd9\u4e9b\u7279\u5f81\u6070\u6070\u90fd\u662f\u6d88\u606f\u6458\u8981\u7b97\u6cd5\u7684\u7279\u5f81\uff0c\u6240\u4ee5\u6d88\u606f\u6458\u8981\u7b97\u6cd5\u9002\u5408\u4f5c\u4e3a\u6570\u5b57\u7b7e\u540d\u7b97\u6cd5\u3002<\/p>\n\n\n\n<p>\u200b \u6570\u5b57\u7b7e\u540d\u65b9\u6848\u662f\u4e00\u79cd\u4ee5\u7535\u5b50\u5f62\u5f0f\u5b58\u50a8\u6d88\u606f\u7b7e\u540d\u7684\u65b9\u6cd5\u3002\u4e00\u4e2a\u5b8c\u6574\u7684\u6570\u5b57\u7b7e\u540d\u65b9\u6848\u5e94\u8be5\u7531\u4e24\u90e8\u5206\u7ec4\u6210\uff1a<strong>\u7b7e\u540d\u7b97\u6cd5\u548c\u9a8c\u8bc1\u7b97\u6cd5<\/strong>\u3002\u4e00\u822c\u5730\u8bf4\uff0c\u4efb\u4f55\u4e00\u4e2a\u516c\u94a5\u5bc6\u7801\u4f53\u5236\u90fd\u53ef\u4ee5\u5355\u72ec\u5730\u4f5c\u4e3a\u4e00\u79cd\u6570\u5b57\u7b7e\u540d\u65b9\u6848\u4f7f\u7528\u3002\u5982RSA\u4f5c\u4e3a\u6570\u5b57\u7b7e\u540d\u65b9\u6848\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/img-blog.csdnimg.cn\/20201212230704805.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"[\u5916\u94fe\u56fe\u7247\u8f6c\u5b58\u5931\u8d25,\u6e90\u7ad9\u53ef\u80fd\u6709\u9632\u76d7\u94fe\u673a\u5236,\u5efa\u8bae\u5c06\u56fe\u7247\u4fdd\u5b58\u4e0b\u6765\u76f4\u63a5\u4e0a\u4f20(img-qE8TaKD0-1607785105922)(images\\20161029105448942.png)]\"\/><\/figure >\n<noscript><img src=\"https:\/\/img-blog.csdnimg.cn\/20201212230704805.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" alt=\"[\u5916\u94fe\u56fe\u7247\u8f6c\u5b58\u5931\u8d25,\u6e90\u7ad9\u53ef\u80fd\u6709\u9632\u76d7\u94fe\u673a\u5236,\u5efa\u8bae\u5c06\u56fe\u7247\u4fdd\u5b58\u4e0b\u6765\u76f4\u63a5\u4e0a\u4f20(img-qE8TaKD0-1607785105922)(images\\20161029105448942.png)]\"\/><\/figure><\/noscript>\n\n\n\n<h3>1.5RSA\u4f5c\u4e3a\u6570\u5b57\u7b7e\u540d<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RSA\u662f\u975e\u5bf9\u79f0\u52a0\u5bc6 \n\u6982\u5ff5\uff1a\n\u516c\u94a5\uff1a\u7ed9\u9700\u8981\u52a0\u5bc6\u65b9\n\u79c1\u94a5\uff1a\u89e3\u5bc6\u8005\u81ea\u5df1\u7559\n\u5bc6\u94a5\u751f\u6210\u8fc7\u7a0b\uff1a\n1\uff1a\u968f\u673a\u9009\u62e9\u4e24\u4e2a\u8d28\u6570p\u3001q\uff0c\u8ba1\u7b97\u51fa n=p x q\n2\uff1a\u8ba1\u7b97\u51fa\u4e0d\u5927\u4e8eN\u4e0eN\u4e92\u8d28\u7684\u6570\u7684\u6570\u91cf  f(n)=(p-1) x (q-1).\n3\uff1a\u53d6e\u4e0d\u5927\u4e8ef(n)\u4e14\u4e0ef(n)\u4e92\u8d28\u7684\u6570.\n4\uff1a\u8ba1\u7b97\u51fae x d mod f(n) = 1 \u65f6 d\u7684\u503c.\n5\uff1a\u5219(e,n)\u4e3a\u516c\u94a5(d,n)\u4e3a\u79c1\u94a5\n\u52a0\u5bc6\u8fc7\u7a0b\uff1a\n\u539f\u6587^e mod n = \u5bc6\u6587\n\u89e3\u5bc6\u8fc7\u7a0b\uff1a\n\u5bc6\u6587^d mod n = \u539f\u6587<\/code><\/pre>\n\n\n\n<p>\u200b \u8fd9\u79cd\u7b7e\u540d\u5b9e\u9645\u4e0a\u5c31\u662f\u7528\u4fe1\u6e90\u5730\u79c1\u94a5\u52a0\u5bc6\u6d88\u606f\uff0c\u52a0\u5bc6\u540e\u5730\u6d88\u606f\u5373\u6210\u4e86\u7b7e\u4f53\uff1b\u800c\u7528\u5bf9\u5e94\u5730\u516c\u94a5\u8fdb\u884c\u9a8c\u8bc1\uff0c\u82e5\u516c\u94a5\u89e3\u5bc6\u540e\u7684\u6d88\u606f\u4e0e\u539f\u6765\u7684\u6d88\u606f\u76f8\u540c\uff0c\u5219\u6d88\u606f\u662f\u5b8c\u6574\u7684\uff0c\u5426\u5219\u6d88\u606f\u4e0d\u5b8c\u6574\u3002\u5b83\u6b63\u597d\u548c\u516c\u94a5\u5bc6\u7801\u7528\u4e8e\u6d88\u606f\u4fdd\u5bc6\u662f\u76f8\u53cd\u7684\u8fc7\u7a0b\u3002\u56e0\u4e3a\u53ea\u6709\u4fe1\u6e90\u624d\u62e5\u6709\u81ea\u5df1\u5730\u79c1\u94a5\uff0c\u522b\u4eba\u65e0\u6cd5\u91cd\u65b0\u52a0\u5bc6\u6e90\u6d88\u606f\uff0c\u6240\u4ee5\u5373\u4f7f\u6709\u4eba\u622a\u83b7\u4e14\u66f4\u6539\u4e86\u6e90\u6d88\u606f\uff0c\u4e5f\u65e0\u6cd5\u91cd\u65b0\u751f\u6210\u7b7e\u4f53\uff0c\u56e0\u4e3a\u53ea\u6709\u7528\u4fe1\u6e90\u7684\u79c1\u94a5\u624d\u80fd\u5f62\u6210\u6b63\u786e\u5730\u7b7e\u4f53\u3002\u540c\u6837\u4fe1\u5bbf\u53ea\u8981\u9a8c\u8bc1\u7528\u4fe1\u6e90\u7684\u516c\u94a5\u89e3\u5bc6\u7684\u6d88\u606f\u662f\u5426\u4e0e\u660e\u6587\u6d88\u606f\u76f8\u540c\uff0c\u5c31\u53ef\u4ee5\u77e5\u9053\u6d88\u606f\u662f\u5426\u88ab\u66f4\u6539\u8fc7\uff0c\u800c\u4e14\u53ef\u4ee5\u8ba4\u8bc1\u6d88\u606f\u662f\u5426\u662f\u786e\u5b9e\u6765\u81ea\u610f\u5b9a\u7684\u4fe1\u6e90\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u4fe1\u6e90\u4e0d\u80fd\u5426\u8ba4\u66fe\u5c06\u53d1\u9001\u7684\u6d88\u606f\u3002\u6240\u4ee5\u8fd9\u6837\u53ef\u4ee5\u5b8c\u6210\u6570\u5b57\u7b7e\u540d\u7684\u529f\u80fd<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/img-blog.csdnimg.cn\/20201212230717988.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"[\u5916\u94fe\u56fe\u7247\u8f6c\u5b58\u5931\u8d25,\u6e90\u7ad9\u53ef\u80fd\u6709\u9632\u76d7\u94fe\u673a\u5236,\u5efa\u8bae\u5c06\u56fe\u7247\u4fdd\u5b58\u4e0b\u6765\u76f4\u63a5\u4e0a\u4f20(img-rMzuPDDA-1607785105944)(images\\662236-20180823173238402-2085730366.png)]\"\/><\/figure >\n<noscript><img src=\"https:\/\/img-blog.csdnimg.cn\/20201212230717988.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" alt=\"[\u5916\u94fe\u56fe\u7247\u8f6c\u5b58\u5931\u8d25,\u6e90\u7ad9\u53ef\u80fd\u6709\u9632\u76d7\u94fe\u673a\u5236,\u5efa\u8bae\u5c06\u56fe\u7247\u4fdd\u5b58\u4e0b\u6765\u76f4\u63a5\u4e0a\u4f20(img-rMzuPDDA-1607785105944)(images\\662236-20180823173238402-2085730366.png)]\"\/><\/figure><\/noscript>\n\n\n\n<p>\u200b \u4f46\u8fd9\u79cd\u65b9\u6848\u8fc7\u4e8e\u5355\u7eaf\uff0c\u5b83\u4ec5\u53ef\u4ee5\u4fdd\u8bc1\u6d88\u606f\u7684\u5b8c\u6574\u6027\uff0c\u800c\u65e0\u6cd5\u786e\u4fdd\u6d88\u606f\u7684\u4fdd\u5bc6\u6027\u3002\u800c\u4e14\u8fd9\u79cd\u65b9\u6848\u8981\u5bf9\u6240\u6709\u7684\u6d88\u606f\u8fdb\u884c\u52a0\u5bc6\u64cd\u4f5c\uff0c\u8fd9\u5728\u6d88\u606f\u7684\u957f\u5ea6\u6bd4\u8f83\u5927\u65f6\uff0c\u6548\u7387\u4f7f\u975e\u5e38\u4f4e\u7684\uff0c\u4e3b\u8981\u539f\u56e0\u5728\u4e8e\u516c\u94a5\u4f53\u5236\u7684\u52a0\u89e3\u5bc6\u8fc7\u7a0b\u7684\u4f4e\u6548\u6027\u3002\u6240\u4ee5\u8fd9\u79cd\u65b9\u6848\u4e00\u822c\u4e0d\u53ef\u53d6\u3002<\/p>\n\n\n\n<p>\u200b \u51e0\u4e4e\u6240\u6709\u7684\u6570\u5b57\u7b7e\u540d\u65b9\u6848\u90fd\u8981\u548c\u5feb\u901f\u9ad8\u6548\u7684<strong>\u6458\u8981\u7b97\u6cd5\uff08Hash\u51fd\u6570\uff09<\/strong>\u4e00\u8d77\u4f7f\u7528\uff0c\u5f53\u516c\u94a5\u7b97\u6cd5\u4e0e\u6458\u8981\u7b97\u6cd5\u7ed3\u5408\u8d77\u6765\u4f7f\u7528\u65f6\uff0c\u4fbf\u6784\u6210\u4e86\u4e00\u79cd\u6709\u6548\u5730\u6570\u5b57\u7b7e\u540d\u65b9\u6848\u3002<br>\u200b \u8fd9\u4e2a\u8fc7\u7a0b\u662f\uff1a\u9996\u5148\u7528\u6458\u8981\u7b97\u6cd5\u5bf9\u6d88\u606f\u8fdb\u884c\u6458\u8981\uff0c\u7136\u540e\u5728\u628a\u6458\u8981\u503c\u7528\u4fe1\u6e90\u7684\u79c1\u94a5\u52a0\u5bc6\uff1b\u63a5\u6536\u65b9\u5148\u628a\u63a5\u6536\u7684\u660e\u6587\u7528\u540c\u6837\u7684\u6458\u8981\u7b97\u6cd5\u6458\u8981\uff0c\u5f62\u6210\u201c\u51c6\u7b7e\u4f53\u201d\uff0c\u7136\u540e\u518d\u628a\u51c6\u7b7e\u4f53\u4e0e\u7528\u4fe1\u6e90\u7684\u516c\u94a5\u89e3\u5bc6\u51fa\u7684\u201c\u7b7e\u4f53\u201d\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u76f8\u540c\u5c31\u8ba4\u4e3a\u6d88\u606f\u662f\u5b8c\u6574\u7684\uff0c\u5426\u5219\u6d88\u606f\u4e0d\u5b8c\u6574\u3002<\/p>\n\n\n\n<p>\u200b \u8fd9\u79cd\u65b9\u6cd5\u4f7f\u516c\u94a5\u52a0\u5bc6\u53ea\u5bf9\u6d88\u606f\u6458\u8981\u8fdb\u884c\u64cd\u4f5c\uff0c\u56e0\u4e3a\u4e00\u79cd\u6458\u8981\u7b97\u6cd5\u7684\u6458\u8981\u6d88\u606f\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u4e14\u90fd\u6bd4\u8f83\u201c\u77ed\u201d\uff08\u76f8\u5bf9\u4e8e\u6d88\u606f\u800c\u8a00\uff09\uff0c\u6b63\u597d\u7b26\u5408\u516c\u94a5\u52a0\u5bc6\u7684\u8981\u6c42\u3002\u8fd9\u6837\u6548\u7387\u5f97\u5230\u4e86\u63d0\u9ad8\uff0c\u800c\u5176\u5b89\u5168\u6027\u4e5f\u5e76\u672a\u56e0\u4e3a\u4f7f\u7528\u6458\u8981\u7b97\u6cd5\u800c\u51cf\u5f31\u3002<\/p>\n\n\n\n<h3>1.6 MD5\u4fe1\u606f\u6458\u8981\u7b97\u6cd5\uff08\u4e0d\u662f\u52a0\u5bc6\u7b97\u6cd5\uff09<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>MD5\u4fe1\u606f\u6458\u8981\u7b97\u6cd5<\/strong>\uff08\u82f1\u8bed\uff1aMD5 Message-Digest Algorithm\uff09\uff0c\u4e00\u79cd\u88ab\u5e7f\u6cdb\u4f7f\u7528\u7684<a href=\"https:\/\/baike.baidu.com\/item\/\u5bc6\u7801\u6563\u5217\u51fd\u6570\/14937715\" target=\"_blank\"  rel=\"nofollow\" >\u5bc6\u7801\u6563\u5217\u51fd\u6570<\/a>\uff0c\u53ef\u4ee5\u4ea7\u751f\u51fa\u4e00\u4e2a128\u4f4d\uff0816<a href=\"https:\/\/baike.baidu.com\/item\/\u5b57\u8282\/1096318\" target=\"_blank\"  rel=\"nofollow\" >\u5b57\u8282<\/a>\uff09\u7684\u6563\u5217\u503c\uff08hash value\uff09\uff0c\u7528\u4e8e\u786e\u4fdd\u4fe1\u606f\u4f20\u8f93\u5b8c\u6574\u4e00\u81f4\u3002MD5\u7531\u7f8e\u56fd\u5bc6\u7801\u5b66\u5bb6<a href=\"https:\/\/baike.baidu.com\/item\/\u7f57\u7eb3\u5fb7\u00b7\u674e\u7ef4\u65af\u7279\/700199\" target=\"_blank\"  rel=\"nofollow\" >\u7f57\u7eb3\u5fb7\u00b7\u674e\u7ef4\u65af\u7279<\/a>\uff08Ronald Linn Rivest\uff09\u8bbe\u8ba1\uff0c\u4e8e1992\u5e74\u516c\u5f00\uff0c\u7528\u4ee5\u53d6\u4ee3<a href=\"https:\/\/baike.baidu.com\/item\/MD4\/8090275\" target=\"_blank\"  rel=\"nofollow\" >MD4<\/a>\u7b97\u6cd5\u3002\u8fd9\u5957\u7b97\u6cd5\u7684\u7a0b\u5e8f\u5728 RFC 1321 \u6807\u51c6\u4e2d\u88ab\u52a0\u4ee5\u89c4\u8303\u30021996\u5e74\u540e\u8be5\u7b97\u6cd5\u88ab\u8bc1\u5b9e\u5b58\u5728\u5f31\u70b9\uff0c\u53ef\u4ee5\u88ab\u52a0\u4ee5\u7834\u89e3\uff0c\u5bf9\u4e8e\u9700\u8981\u9ad8\u5ea6\u5b89\u5168\u6027\u7684\u6570\u636e\uff0c\u4e13\u5bb6\u4e00\u822c\u5efa\u8bae\u6539\u7528\u5176\u4ed6\u7b97\u6cd5\uff0c\u5982<a href=\"https:\/\/baike.baidu.com\/item\/SHA-2\/22718180\" target=\"_blank\"  rel=\"nofollow\" >SHA-2<\/a>\u30022004\u5e74\uff0c\u8bc1\u5b9eMD5\u7b97\u6cd5\u65e0\u6cd5\u9632\u6b62\u78b0\u649e\uff08collision\uff09\uff0c\u56e0\u6b64\u4e0d\u9002\u7528\u4e8e\u5b89\u5168\u6027\u8ba4\u8bc1\uff0c\u5982<a href=\"https:\/\/baike.baidu.com\/item\/SSL\/320778\" target=\"_blank\"  rel=\"nofollow\" >SSL<\/a>\u516c\u5f00\u5bc6\u94a5\u8ba4\u8bc1\u6216\u662f<a href=\"https:\/\/baike.baidu.com\/item\/\u6570\u5b57\u7b7e\u540d\/212550\" target=\"_blank\"  rel=\"nofollow\" >\u6570\u5b57\u7b7e\u540d<\/a>\u7b49\u7528\u9014\u3002<\/p><\/blockquote>\n\n\n\n<h4>1.6.1\u539f\u7406<\/h4>\n\n\n\n<p>\u00b7 MD5\u7b97\u6cd5\u7684\u539f\u7406\u53ef\u7b80\u8981\u7684\u53d9\u8ff0\u4e3a\uff1aMD5\u7801\u4ee5512\u4f4d\u5206\u7ec4\u6765\u5904\u7406\u8f93\u5165\u7684\u4fe1\u606f\uff0c\u4e14\u6bcf\u4e00\u5206\u7ec4\u53c8\u88ab\u5212\u5206\u4e3a16\u4e2a32\u4f4d\u5b50\u5206\u7ec4\uff0c\u7ecf\u8fc7\u4e86\u4e00\u7cfb\u5217\u7684\u5904\u7406\u540e\uff0c\u7b97\u6cd5\u7684\u8f93\u51fa\u7531\u56db\u4e2a32\u4f4d\u5206\u7ec4\u7ec4\u6210\uff0c\u5c06\u8fd9\u56db\u4e2a32\u4f4d\u5206\u7ec4\u7ea7\u8054\u540e\u5c06\u751f\u6210\u4e00\u4e2a128\u4f4d<a href=\"https:\/\/baike.baidu.com\/item\/\u6563\u5217\u503c\/10398613\" target=\"_blank\"  rel=\"nofollow\" >\u6563\u5217\u503c<\/a>\u3002<\/p>\n\n\n\n<p>\u603b\u4f53\u6d41\u7a0b\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6bcf\u6b21\u7684\u8fd0\u7b97\u90fd\u7531\u524d\u4e00\u8f6e\u7684128\u4f4d\u7ed3\u679c\u503c\u548c\u5f53\u524d\u7684512bit\u503c\u8fdb\u884c\u8fd0\u7b97 \u3002<br><img   class=\"lazyload\" data-src=\"https:\/\/img-blog.csdnimg.cn\/20201212230857485.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/p >\n<noscript><img src=\"https:\/\/img-blog.csdnimg.cn\/20201212230857485.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/p><\/noscript>\n\n\n\n<h4>1.6.2\u7b97\u6cd5\u6b65\u9aa4<\/h4>\n\n\n\n<h5>1.6.2.1\u6309\u4f4d\u8865\u5145\u6570\u636e<\/h5>\n\n\n\n<p>\u200b \u5728MD5\u7b97\u6cd5\u4e2d\uff0c\u9996\u5148\u9700\u8981\u5bf9\u4fe1\u606f\u8fdb\u884c\u586b\u5145\uff0c\u8fd9\u4e2a\u6570\u636e\u6309\u4f4d(bit)\u8865\u5145\uff0c\u8981\u6c42\u6700\u7ec8\u7684\u4f4d\u6570\u5bf9512\u6c42\u6a21\u7684\u7ed3\u679c\u4e3a448\u3002\u4e5f\u5c31\u662f\u8bf4\u6570\u636e\u8865\u4f4d\u540e\uff0c\u5176\u4f4d\u6570\u957f\u5ea6\u53ea\u5dee64\u4f4d(bit)\u5c31\u662f512\u7684\u6574\u6570\u500d\u3002\u5373\u4fbf\u662f\u8fd9\u4e2a\u6570\u636e\u7684\u4f4d\u6570\u5bf9512\u6c42\u6a21\u7684\u7ed3\u679c\u6b63\u597d\u662f448\u4e5f\u5fc5\u987b\u8fdb\u884c\u8865\u4f4d\u3002\u8865\u4f4d\u7684\u5b9e\u73b0\u8fc7\u7a0b\uff1a\u9996\u5148\u5728\u6570\u636e\u540e\u8865\u4e00\u4e2a1 bit\uff1b \u63a5\u7740\u5728\u540e\u9762\u8865\u4e0a\u4e00\u58060 bit, \u76f4\u5230\u6574\u4e2a\u6570\u636e\u7684\u4f4d\u6570\u5bf9512\u6c42\u6a21\u7684\u7ed3\u679c\u6b63\u597d\u4e3a448\u3002\u603b\u4e4b\uff0c\u81f3\u5c11\u88651\u4f4d\uff0c\u800c\u6700\u591a\u53ef\u80fd\u8865512\u4f4d [8] \u3002<\/p>\n\n\n\n<h5>1.6.2.2\u6269\u5c55\u957f\u5ea6<\/h5>\n\n\n\n<p>\u200b \u5728\u5b8c\u6210\u8865\u4f4d\u5de5\u4f5c\u540e\uff0c\u53c8\u5c06\u4e00\u4e2a\u8868\u793a\u6570\u636e\u539f\u59cb\u957f\u5ea6\u768464 bit\u6570(\u8fd9\u662f\u5bf9\u539f\u59cb\u6570\u636e\u6ca1\u6709\u8865\u4f4d\u524d\u957f\u5ea6\u7684\u63cf\u8ff0\uff0c\u7528\u4e8c\u8fdb\u5236\u6765\u8868\u793a)\u8865\u5728\u6700\u540e\u3002\u5f53\u5b8c\u6210\u8865\u4f4d\u53ca\u8865\u5145\u6570\u636e\u7684\u63cf\u8ff0\u540e\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u6570\u636e\u957f\u5ea6\u6b63\u597d\u662f512\u7684\u6574\u6570\u500d\u3002\u4e5f\u5c31\u662f\u8bf4\u957f\u5ea6\u6b63\u597d\u662f16\u4e2a(32bit) \u5b57\u7684\u6574\u6570\u500d\u3002<\/p>\n\n\n\n<h5>1.6.2.3\u521d\u59cb\u5316MD\u7f13\u5b58\u5668<\/h5>\n\n\n\n<p>\u200b MD5\u8fd0\u7b97\u8981\u7528\u5230\u4e00\u4e2a128\u4f4d\u7684MD5\u7f13\u5b58\u5668\uff0c\u7528\u6765\u4fdd\u5b58\u4e2d\u95f4\u53d8\u91cf\u548c\u6700\u7ec8\u7ed3\u679c\u3002\u8be5\u7f13\u5b58\u5668\u53c8\u53ef\u770b\u6210\u662f4\u4e2a32\u4f4d\u7684\u5bc4\u5b58\u5668A\u3001B\u3001C\u3001D\uff0c\u521d\u59cb\u5316\u4e3a \uff1a<br>A \uff1a 01 23 45 67<br>B\uff1a 89 ab cd ef<br>C\uff1a fe dc ba 98<br>D\uff1a 76 54 32 10<\/p>\n\n\n\n<h5>1.6.2.4\u5904\u7406\u6570\u636e\u6bb5<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\u9996\u5148\u5b9a\u4e494\u4e2a\u975e\u7ebf\u6027\u51fd\u6570F\u3001G\u3001H\u3001I\uff0c\u5bf9\u8f93\u5165\u7684\u62a5\u6587\u8fd0\u7b97\u4ee5512\u4f4d\u6570\u636e\u6bb5\u4e3a\u5355\u4f4d\u8fdb\u884c\u5904\u7406\u3002\u5bf9\u6bcf\u4e2a\u6570\u636e\u6bb5\u90fd\u8981\u8fdb\u884c4\u8f6e\u7684\u903b\u8f91\u5904\u7406\uff0c\u57284\u8f6e\u4e2d\u5206\u522b\u4f7f\u75284\u4e2a\u4e0d\u540c\u7684\u51fd\u6570F\u3001G\u3001H\u3001I\u3002\u6bcf\u4e00\u8f6e\u4ee5ABCD\u548c\u5f53\u524d\u7684512\u4f4d\u7684\u5757\u4e3a\u8f93\u5165\uff0c\u5904\u7406\u540e\u9001\u5165ABCD(128\u4f4d)  \u3002<\/code><\/pre>\n\n\n\n<h5>1.62.5\u8f93\u51fa<\/h5>\n\n\n\n<p>\u4fe1\u606f\u6458\u8981\u6700\u7ec8\u5904\u7406\u6210\u4ee5A, B, C, D \u7684\u5f62\u5f0f\u8f93\u51fa\u3002\u4e5f\u5c31\u662f\u5f00\u59cb\u4e8eA\u7684\u4f4e\u4f4d\u5728\u524d\u7684\u987a\u5e8f\u5b57\u8282\uff0c\u7ed3\u675f\u4e8eD\u7684\u9ad8\u4f4d\u5728\u524d\u7684\u987a\u5e8f\u5b57\u8282 \u3002<\/p>\n\n\n\n<h4>1.6.3\u5b89\u5168\u6027\u5206\u6790<\/h4>\n\n\n\n<p>MD5\u76f8\u5bf9<a href=\"https:\/\/baike.baidu.com\/item\/MD4\/8090275\" target=\"_blank\"  rel=\"nofollow\" >MD4<\/a>\u6240\u4f5c\u7684\u6539\u8fdb\uff1a<\/p>\n\n\n\n<h5>1.6.3.1\u589e\u52a0\u4e86\u7b2c\u56db\u8f6e\u3002<\/h5>\n\n\n\n<h5>1.6.3.2\u6bcf\u4e00\u6b65\u5747\u6709\u552f\u4e00\u7684\u52a0\u6cd5\u5e38\u6570\u3002<\/h5>\n\n\n\n<h5>1.6.3.3\u51cf\u5f31\u7b2c\u4e8c\u8f6e\u4e2d\u51fd\u6570\u7684\u5bf9\u79f0\u6027\u3002<\/h5>\n\n\n\n<h5>1.6.3.4\u7b2c\u4e00\u6b65\u52a0\u4e0a\u4e86\u4e0a\u4e00\u6b65\u7684\u7ed3\u679c\uff0c\u8fd9\u5c06\u5f15\u8d77\u66f4\u5feb\u7684\u96ea\u5d29\u6548\u5e94\uff08\u5c31\u662f\u5bf9\u660e\u6587\u6216\u8005\u5bc6\u94a5\u6539\u53d8 1bit \u90fd\u4f1a\u5f15\u8d77\u5bc6\u6587\u7684\u5de8\u5927\u4e0d\u540c\uff09\u3002<\/h5>\n\n\n\n<h5>1.6.3.5\u6539\u53d8\u4e86\u7b2c\u4e8c\u8f6e\u548c\u7b2c\u4e09\u8f6e\u4e2d\u8bbf\u95ee\u6d88\u606f\u5b50\u5206\u7ec4\u7684\u6b21\u5e8f\uff0c\u4f7f\u5176\u66f4\u4e0d\u76f8\u4f3c\u3002<\/h5>\n\n\n\n<h5>1.6.3.6\u8fd1\u4f3c\u4f18\u5316\u4e86\u6bcf\u4e00\u8f6e\u4e2d\u7684\u5faa\u73af\u5de6\u79fb\u4f4d\u79fb\u91cf\u4ee5\u5b9e\u73b0\u66f4\u5feb\u7684\u96ea\u5d29\u6548\u5e94\uff0c\u5404\u8f6e\u7684\u4f4d\u79fb\u91cf\u4e92\u4e0d\u76f8\u540c\u3002<\/h5>\n\n\n\n<h3>1.7MD5\u7b97\u6cd5\u7684\u7f3a\u70b9<\/h3>\n\n\n\n<p>\u200b MD5 \u7b97\u6cd5\u81ea\u8bde\u751f\u4e4b\u65e5\u8d77\uff0c\u5c31\u6709\u5f88\u591a\u4eba\u8bd5\u56fe\u8bc1\u660e\u548c\u53d1\u73b0\u5b83\u7684\u4e0d\u5b89\u5168\u4e4b\u5904\uff0c\u5373\u5b58\u5728\u78b0\u649e\uff08\u5728\u5bf9\u4e24\u4e2a\u4e0d\u540c\u7684\u5185\u5bb9\u4f7f\u7528 MD5\u7b97\u6cd5\u8fd0\u7b97\u7684\u65f6\u5019\uff0c\u6709\u53ef\u80fd\u5f97\u5230\u4e00\u5bf9\u76f8\u540c\u7684\u7ed3\u679c\u503c\uff09\u30022009\u5e74\uff0c\u4e2d\u56fd\u79d1\u5b66\u9662\u7684\u8c22\u6d9b\u548c\u51af\u767b\u56fd\u4ec5\u7528\u4e86 \u7684\u78b0\u649e\u7b97\u6cd5\u590d\u6742\u5ea6\uff0c\u7834\u89e3\u4e86MD5\u7684\u78b0\u649e\u62b5\u6297\uff0c\u8be5\u653b\u51fb\u5728\u666e\u901a\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u53ea\u9700\u8981\u6570\u79d2\u949f\u3002<\/p>\n\n\n\n<h3>1.8MD5\u7b97\u6cd5\u52a0\u76d0<\/h3>\n\n\n\n<p>\u6839\u636eMD5\u7684\u5b58\u5728\u7684\u7f3a\u70b9\uff0c\u4e3a\u4e86\u52a0\u5f3aMD5\u52a0\u5bc6\u7b97\u6cd5\u7684\u5b89\u5168\u6027\uff08\u672c\u8eab\u662f\u4e0d\u53ef\u9006\u7684\uff09\uff0c\u4ece\u800c\u52a0\u5165\u4e86\u65b0\u7684\u7b97\u6cd5\u90e8\u5206\u5373\u52a0\u76d0\u503c\uff0c\u52a0\u76d0\u503c\u662f\u968f\u673a\u751f\u6210\u7684\u4e00\u7ec4\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5305\u62ec\u968f\u673a\u7684\u5927\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u5b57\u7b26\uff0c\u4f4d\u6570\u53ef\u4ee5\u6839\u636e\u8981\u6c42\u800c\u4e0d\u4e00\u6837\uff0c\u4f7f\u7528\u4e0d\u540c\u7684\u52a0\u76d0\u503c\u4ea7\u751f\u7684\u6700\u7ec8\u5bc6\u6587\u662f\u4e0d\u4e00\u6837\u7684\u3002\u7531\u4e8e\u4f7f\u7528\u52a0\u76d0\u503c\u4ee5\u540e\u7684\u5bc6\u7801\u76f8\u5f53\u7684\u5b89\u5168\uff0c\u5373\u4fbf\u662f\u4f60\u83b7\u5f97\u4e86\u5176\u4e2d\u7684salt\u548c\u6700\u7ec8\u5bc6\u6587\uff0c\u7834\u89e3\u4e5f\u662f\u4e00\u4e2a\u8017\u8d39\u76f8\u5f53\u591a\u65f6\u95f4\u7684\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u8bf4\u662f\u7834\u89e3\u5355\u7eafMD5\u7684\u597d\u51e0\u500d\u3002\u5e76\u4e14\u540c\u4e00\u4e2a\u660e\u6587\u5728\u7ecf\u8fc7\u52a0\u76d0\uff08\u8fd9\u4e2a\u76d0\u662f\u968f\u673a\u7684\uff0c\u4e0d\u540c\u7684\uff09\u540e\u8fdb\u884cMD5\u7b97\u6cd5\u4ea7\u751f\u7684\u5bc6\u6587\u4e5f\u662f\u4e0d\u540c\u7684\u3002\u8fd9\u5c31\u4f7f\u5f97MD5\u7b97\u6cd5\u7684\u6297\u78b0\u649e\u6027\u5f97\u5230\u5927\u5e45\u5ea6\u589e\u5f3a\u3002<\/p>\n\n\n\n<h2>2.MD5+\u76d0\u7684\u5b9e\u73b0\u6b65\u9aa4<\/h2>\n\n\n\n<p>1).\u9996\u5148\u83b7\u53d6\u9700\u8981\u8fdb\u884cmd5\u666e\u6458\u8981\u7b97\u6cd5\u7684\u660e\u6587text\u3002<br>2).\u968f\u673a\u751f\u6210\u52a0\u76d0\u503c\uff08\u53ef\u4ee5\u7531\u5b57\u6bcd\uff0c\u6570\u5b57\uff0c\u5b57\u7b26\u7ec4\u6210\uff0c\u4f4d\u6570\u53ef\u4ee5\u81ea\u5df1\u51b3\u5b9a\uff0c\u8fd9\u91cc\u4f7f\u752816\u4f4d\u7684\u5b57\u7b26\u4e32\uff09salt\u5b57\u7b26\u4e32.<br>3)\u5c06\u751f\u6210\u7684salt\u5b57\u7b26\u4e32\u8ffd\u52a0\u5230\u660e\u6587text\u4e2d\uff0c\u5f97\u5230\u7ec4\u5408\u7684\u5b57\u7b26\u4e32 mergeText = text +salt\u3002<br>4\uff09\u4f7f\u7528md5\u666e\u901a\u6458\u8981\u7b97\u6cd5\u83b7\u53d6mergeText \u7684hash\u503c\u5f97\u5230\u4e00\u4e2a32\u4f4d\u7684\u5b57\u7b26\u4e32decodeText\u3002<br>5).\u5c06\u76d0salt\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\u9010\u4e2a\u63d2\u5165\u5230\u5b57\u7b26\u4e32decodeText\u662f i *3+1\uff08i = 0,1,2,\u202616\uff09\u7684\u4f4d\u7f6e\u4e0a,\u5f97\u5230\u4e00\u4e2a\u65b0\u768448\u4f4d\u7684\u5b57\u7b26\u4e32 decode\u3002<br>6\uff09\u6700\u7ec8\u7684decode\u5b57\u7b26\u4e32\u5c31\u662f\u6240\u8981\u7684\u5bc6\u6587\uff0c\u4f4d\u957f\u4e3a48\u4e2a\u5b57\u7b26\u3002<\/p>\n\n\n\n<h2>3.\u4ee3\u7801\u5b9e\u73b0<\/h2>\n\n\n\n<p>\u8fd9\u91cc\u4e3b\u8981\u501f\u52a9\u5df2\u7ecf\u4f7f\u7528C++\u5b9e\u73b0\u7684MD5\u7b97\u6cd5\u7c7b\u6765\u5b9e\u73b0MD5+\u76d0\u7684\u539f\u7406\u3002<\/p>\n\n\n\n<h3>3.1\u4ea7\u751f\u76d0\u503c\u5b57\u7b26\u4e32\u7684\u7b97\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n\u4ea7\u751f\u76d0\u503c\u5b57\u7b26\u4e32salt\n*\/\nstring EncryptUtil::createSalt()\n{\n    \/\/\u968f\u673a\u751f\u6210\u4e24\u4e2a\u968f\u673a\u6570\n    int num1 = rand() % 99999999;\n    int num2 = rand() % 99999999;\n    string salt = \"\";\n    \/\/\u5c06\u8fd9\u4e24\u4e2a\u968f\u673a\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u540e\u8ffd\u52a0\u5230salt\u4e2d\n    salt.append(to_string(num1));\n    salt.append(to_string(num2));\n    \/\/salt\u4e0d\u591f\u5341\u516d\u4f4d\u5c31\u5728\u540e\u9762\u52a00\n    int len = salt.size();\n    if (len &lt; 16) {\n        for (int i = 0; i &lt; 16 - len; i++) {\n            salt.append(\"0\");\n        }\n    }\n    return salt;\n}<\/code><\/pre>\n\n\n\n<h3>3.2\u83b7\u53d6md5\u666e\u901a\u6458\u8981\u51fd\u6570+\u76d0\u503csalt\u64cd\u4f5c\u540e\u7684\u6700\u7ec8\u5bc6\u6587<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n* \u52a0\u76d0MD5\n* @param text : \u9700\u8981\u8fdb\u884c\u52a0\u76d0\u540e\u6458\u8981\u7684text\u660e\u6587\n* @return string\uff1a\u8fd4\u56de\u8fdb\u884c\u6458\u8981\u7b97\u6cd5\u540e\u7684\u5b57\u7b26\u4e32\n*\/\nstring EncryptUtil::encryptBySalt(const string&amp; text) {\n    \/\/\u4ea7\u751f\u76d0\u503c\u5b57\u7b26\u4e32salt\n    string salt = createSalt();\n    \/\/\u5c06\u4ea7\u751f\u7684salt\u8ffd\u52a0\u5230\u9700\u8981\u8fdb\u884c\u6458\u8981\u7684\u660e\u6587text\u4e2d\uff0c\u5f97\u5230\u7531\u660e\u6587text\u548csalt\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\n    string merge_str =  text + salt;\n    \/\/\u4f7f\u7528md5\u7b97\u6cd5\u8fdb\u884c\u6458\u8981\u8ba1\u7b97\u5f97\u5230\u76f8\u5e94\u768432\u4f4d\u7684\u5bc6\u6587\n    string encodeText = md5Hex(merge_str);\n    char cs &#91;48];\n    \/\/\u5c0632\u4f4d\u7684\u5bc6\u6587\u548c\u4ea7\u751f\u768416\u4f4d\u7684salt\u8fdb\u884c\u91cd\u7ec4\u5f62\u6210\u65b0\u768448\u4f4d\u7684\u5b57\u7b26\u4e32\u3002\n    \/\/\u8fd9\u91cc\u8868\u793a\uff0c\u5728\u65b0\u7684\u5b57\u7b26\u6570\u7ec4cs\u4e2d\uff0csalt\u4e2d\u7684\u5b57\u7b26\u6dfb\u52a0\u5230cs\u5b57\u7b26\u6570\u7ec4\u4e2d\u4f4d\u7f6e\u662fi*3+1\u7684\u4f4d\u7f6e\uff0c\u5176\u5b83\u4f4d\u7f6e\u7531\u6458\u8981\u5bc6\u6587encodeText\u4e2d\u7684\u5b57\u7b26\u586b\u5145\n    for (int i = 0; i &lt; 48; i += 3) {\n        cs&#91;i] = encodeText&#91;i \/ 3 * 2];\n        char c = salt&#91;i \/ 3];\n        cs&#91;i + 1] = c;\n        cs&#91;i + 2] = encodeText&#91;i \/ 3 * 2 + 1];\n    }\n    \/\/\u5c06\u7ec4\u5408\u6210\u7684cs\u5b57\u7b26\u6570\u7ec4\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u5f97\u523048\u4f4d\u7684\u52a0\u76d0\u540e\u5f62\u6210\u7684md5\u6458\u8981\u5bc6\u6587\n    string code = \"\";\n    for (int i = 0; i &lt; 48; i++)\n    {\n      code.append(1,cs&#91;i]);\n    }\n    return code;\n}<\/code><\/pre>\n\n\n\n<h3>3.4\u6821\u9a8c\u52a0\u76d0\u540e\u662f\u5426\u548c\u539f\u6587\u4e00\u81f4<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u6821\u9a8c\u52a0\u76d0\u540e\u662f\u5426\u548c\u539f\u6587\u4e00\u81f4\n * @param text : \u8fdb\u884c\u6458\u8981\u7b97\u6cd5\u7684\u660e\u6587\n * @param encryptText  \uff1a \u8fdb\u884c\u52a0\u76d0\u6458\u8981\u7b97\u6cd5\u540e\u7684\u5bc6\u6587\n * @return \u5982\u679c\u52a0\u76d0\u540e\u548c\u539f\u660e\u6587text\u4e00\u81f4\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\n *\/\n bool EncryptUtil::verify( string text,  string encryptText) {\n    char md5Text&#91;32];\n    char salt&#91;16];\n    \/\/\u9996\u5148\u5c06\u52a0\u76d0\u540emd5\u7b97\u6cd5\u6458\u8981\u5f97\u5230\u7684encryptText\u5bc6\u6587\u8fdb\u884c\u62c6\u5206\u6210\u4e00\u4e2a32\u4f4d\u7684\u666e\u901amd5\u6458\u8981\u7b97\u6cd5\u5bc6\u6587md5Text\u548c\u4e00\u4e2a16\u4f4d\u7684\u5b57\u7b26\u4e32salt\u3002\n    \/\/\u8fd9\u91cc\u5c31\u662f\u8fdb\u884csalt\u548cmd5\u6458\u8981\u5bc6\u6587\u5f62\u621048\u4f4d\u65b0\u7684\u5b57\u7b26\u4e32encryptText\u65f6\u7684\u9006\u5411\u64cd\u4f5c\u3002encryptText\u4e2dsalt&#91;i] =  encryptText&#91; 3* i + 1], i = 0,1,2,...15\n    for (int i = 0; i &lt; 48; i += 3) {\n        md5Text&#91;i \/ 3 * 2] = encryptText&#91;i];\n        md5Text&#91;i \/ 3 * 2 + 1] = encryptText&#91;i + 2];\n        salt&#91;i \/ 3] = encryptText&#91;i + 1];\n    }\n    \/\/\u5c06\u62c6\u5206\u768416\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\u8ffdsalt\u52a0\u5230\u660e\u6587text\u4e2d\n    for (int  i = 0; i &lt; 16; i++)\n    {\n        text.append(1,salt&#91;i]);\n    }\n    \/\/\u4f7f\u7528md5\u6458\u8981\u7b97\u6cd5\u8fdb\u884c\u751f\u6210\u5bc6\u6587\u5f97\u5230enCs2\n    string enCs2 = md5Hex(text);\n    \/\/\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u76f8\u7b49\uff0c\u5982\u679c\u76f8\u7b49\u5219\u8fd4\u56detrue\uff0c\u53ea\u8981\u6709\u4e00\u4e2a\u5b57\u7b26\u4e0d\u76f8\u7b49\u5c31\u8fd4\u56defalse\n    for (int i = 0; i &lt; enCs2.size(); i++)\n    {\n        if (enCs2&#91;i] != md5Text&#91;i])\n        {\n            return false;\n        }\n    }\n    return true;\n}<\/code><\/pre>\n\n\n\n<h2>4.\u5177\u4f53\u4ee3\u7801<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u5176\u4e2dMD5\u7b97\u6cd5\u7684\u5b9e\u73b0\u5f15\u7528\u4e86\u535a\u5ba2<a href=\"https:\/\/blog.csdn.net\/flydream0\/article\/details\/7049322\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/blog.csdn.net\/flydream0\/article\/details\/7049322<\/a>\u4e2d\u5b9e\u73b0\u7684md5\u7b97\u6cd5\u4ee3\u7801<\/p><\/blockquote>\n\n\n\n<h3>4.1EncryptUtil.h\u6587\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#pragma once\n#include&lt;iostream>\n#include&lt;string>\n#include\"Md5.h\"\n#include&lt;time.h>\nusing namespace std;\n\nclass EncryptUtil\n{\npublic:\n    EncryptUtil();\n\/**\n*@param text:\u9700\u8981\u8fdb\u884c\u666e\u901amd5\u6458\u8981\u7b97\u6cd5\u8ba1\u7b97\u7684\u660e\u6587\n*@return string\uff1a\u8fd4\u56de\u666e\u901amd5\u6458\u8981\u7b97\u8ba1\u7b97\u540e\u7684\u5b57\u7b26\u4e32\n*\/\n    string md5Decode(const string&amp; text);\n\/**\n\u4ea7\u751f\u76d0\u503c\u5b57\u7b26\u4e32salt\n*\/\n    string createSalt();\n\n\/**\n* \u52a0\u76d0MD5\n* @param text : \u9700\u8981\u8fdb\u884c\u52a0\u76d0\u540e\u6458\u8981\u7684text\u660e\u6587\n* @return string\uff1a\u8fd4\u56de\u8fdb\u884c\u6458\u8981\u7b97\u6cd5\u540e\u7684\u5b57\u7b26\u4e32\n*\/\n    string encryptBySalt(const string&amp; text);\n\/**\n * \u83b7\u53d6\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684MD5\u6458\u8981\n * @param text: \u9700\u8981\u8fdb\u884c\u6458\u8981\u7b97\u6cd5\u7684\u660e\u6587\n * @return string: \u8fd4\u56de\u8fdb\u884c\u666e\u901amd5\u6458\u8981\u540e\u7684\u5b57\u7b26\u4e32\n *\/\n    string md5Hex(const string&amp; src);\n\/**\n * \u6821\u9a8c\u52a0\u76d0\u540e\u662f\u5426\u548c\u539f\u6587\u4e00\u81f4\n * @param text : \u8fdb\u884c\u6458\u8981\u7b97\u6cd5\u7684\u660e\u6587\n * @param encryptText  \uff1a \u8fdb\u884c\u52a0\u76d0\u6458\u8981\u7b97\u6cd5\u540e\u7684\u5bc6\u6587\n * @return \u5982\u679c\u52a0\u76d0\u540e\u548c\u539f\u660e\u6587text\u4e00\u81f4\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\n *\/\n    bool verify( string text,  string md5Text);\n\/**\n  \u9010\u4e2a\u6bd4\u8f83\u4e24\u4e2a\u5bc6\u6587\u76f8\u540c\u4f4d\u7f6e\u4e0a\u5b57\u7b26\u662f\u5426\u76f8\u540c\u6765\u8ba1\u7b97\u4e24\u4e2a\u8fdb\u884cmd5\u52a0\u5bc6\u540e\u7684\u76f8\u4f3c\u5ea6\u3002\n @param str1: md5\u6458\u8981\u7b97\u6cd5\u540e\u7684\u5b57\u7b26\u4e32\n @param str2:md5\u6458\u8981\u7b97\u6cd5\u540e\u7684\u5b57\u7b26\u4e32\n @return double: \u8fd4\u56de\u76f8\u4f3c\u5ea6\n *\/\n    double similarityDegree(const string&amp; str1, const string&amp; str2);\n    ~EncryptUtil();\nprivate:\n    MD5* md5 = nullptr;\n\n};<\/code><\/pre>\n\n\n\n<h3>4.2EncryptUtil.cpp<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"EncryptUtil.h\"\n\n\n\nEncryptUtil::EncryptUtil()\n{\n    \/\/\u521d\u59cb\u5316MD5\u8fd9\u4e2a\u7c7b\n    this->md5 = new MD5();\n    \/\/\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50\uff0c\u5728\u4ea7\u751f\u76d0\u65f6\u53ef\u4ee5\u4ea7\u751f\u968f\u673a\u6570\u3002\n    srand(time(NULL));\n}\n\n\/**\n*@param text:\u9700\u8981\u8fdb\u884c\u666e\u901amd5\u6458\u8981\u7b97\u6cd5\u8ba1\u7b97\u7684\u660e\u6587\n*@return string\uff1a\u8fd4\u56de\u666e\u901amd5\u6458\u8981\u7b97\u8ba1\u7b97\u540e\u7684\u5b57\u7b26\u4e32\n*\/\nstring EncryptUtil::md5Decode(const string &amp; text)\n{\n    \/\/\u8c03\u7528md5Hex(text)\u51fd\u6570\u5373\u53ef\n     return this->md5Hex(text);\n}\n\/**\n\u4ea7\u751f\u76d0\u503c\u5b57\u7b26\u4e32salt\n*\/\nstring EncryptUtil::createSalt()\n{\n    \/\/\u968f\u673a\u751f\u6210\u4e24\u4e2a\u968f\u673a\u6570\n    int num1 = rand() % 99999999;\n    int num2 = rand() % 99999999;\n    string salt = \"\";\n    \/\/\u5c06\u8fd9\u4e24\u4e2a\u968f\u673a\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u540e\u8ffd\u52a0\u5230salt\u4e2d\n    salt.append(to_string(num1));\n    salt.append(to_string(num2));\n    \/\/salt\u4e0d\u591f\u5341\u516d\u4f4d\u5c31\u5728\u540e\u9762\u52a00\n    int len = salt.size();\n    if (len &lt; 16) {\n        for (int i = 0; i &lt; 16 - len; i++) {\n            salt.append(\"0\");\n        }\n    }\n    \/\/cout &lt;&lt; \"\u4ea7\u751f\u7684\u76d0\u503c\u5b57\u7b26\u4e32\u4e3a\uff1a\" &lt;&lt;salt&lt;&lt; endl;\n    return salt;\n}\n\n\/**\n* \u52a0\u76d0MD5\n* @param text : \u9700\u8981\u8fdb\u884c\u52a0\u76d0\u540e\u6458\u8981\u7684text\u660e\u6587\n* @return string\uff1a\u8fd4\u56de\u8fdb\u884c\u6458\u8981\u7b97\u6cd5\u540e\u7684\u5b57\u7b26\u4e32\n*\/\nstring EncryptUtil::encryptBySalt(const string&amp; text) {\n    \/\/\u4ea7\u751f\u76d0\u503c\u5b57\u7b26\u4e32salt\n    string salt = createSalt();\n    \/\/\u5c06\u4ea7\u751f\u7684salt\u8ffd\u52a0\u5230\u9700\u8981\u8fdb\u884c\u6458\u8981\u7684\u660e\u6587text\u4e2d\uff0c\u5f97\u5230\u7531\u660e\u6587text\u548csalt\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\n    string merge_str =  text + salt;\n    \/\/\u4f7f\u7528md5\u7b97\u6cd5\u8fdb\u884c\u6458\u8981\u8ba1\u7b97\u5f97\u5230\u76f8\u5e94\u768432\u4f4d\u7684\u5bc6\u6587\n    string encodeText = md5Hex(merge_str);\n    char cs &#91;48];\n    \/\/\u5c0632\u4f4d\u7684\u5bc6\u6587\u548c\u4ea7\u751f\u768416\u4f4d\u7684salt\u8fdb\u884c\u91cd\u7ec4\u5f62\u6210\u65b0\u768448\u4f4d\u7684\u5b57\u7b26\u4e32\u3002\n    \/\/\u8fd9\u91cc\u8868\u793a\uff0c\u5728\u65b0\u7684\u5b57\u7b26\u6570\u7ec4cs\u4e2d\uff0csalt\u4e2d\u7684\u5b57\u7b26\u6dfb\u52a0\u5230cs\u5b57\u7b26\u6570\u7ec4\u4e2d\u4f4d\u7f6e\u662fi*3+1\u7684\u4f4d\u7f6e\uff0c\u5176\u5b83\u4f4d\u7f6e\u7531\u6458\u8981\u5bc6\u6587encodeText\u4e2d\u7684\u5b57\u7b26\u586b\u5145\n    for (int i = 0; i &lt; 48; i += 3) {\n        cs&#91;i] = encodeText&#91;i \/ 3 * 2];\n        char c = salt&#91;i \/ 3];\n        cs&#91;i + 1] = c;\n        cs&#91;i + 2] = encodeText&#91;i \/ 3 * 2 + 1];\n    }\n    \/\/\u5c06\u7ec4\u5408\u6210\u7684cs\u5b57\u7b26\u6570\u7ec4\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u5f97\u523048\u4f4d\u7684\u52a0\u76d0\u540e\u5f62\u6210\u7684md5\u6458\u8981\u5bc6\u6587\n    string code = \"\";\n    for (int i = 0; i &lt; 48; i++)\n    {\n      code.append(1,cs&#91;i]);\n    }\n    return code;\n}\n\/**\n * \u83b7\u53d6\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684MD5\u6458\u8981\n * @param text: \u9700\u8981\u8fdb\u884c\u6458\u8981\u7b97\u6cd5\u7684\u660e\u6587\n * @return string: \u8fd4\u56de\u8fdb\u884c\u666e\u901amd5\u6458\u8981\u540e\u7684\u5b57\u7b26\u4e32\n *\/\nstring EncryptUtil::md5Hex(const string&amp; text) {\n    \/\/\u9996\u5148\u66f4\u65b0md5\u4e2d\u9700\u8981\u8fdb\u884c\u6458\u8981\u7684\u5b57\u7b26\u4e32\n    this->md5->update(text);\n    \/\/\u4f7f\u7528md5\u7684\u65b9\u6cd5\u751f\u6210\u5bf9\u5e94\u7684\u6458\u8981\u540e\u7684\u5b57\u7b26\u4e32\n    return this->md5->toString();\n\n}\n\/**\n * \u6821\u9a8c\u52a0\u76d0\u540e\u662f\u5426\u548c\u539f\u6587\u4e00\u81f4\n * @param text : \u8fdb\u884c\u6458\u8981\u7b97\u6cd5\u7684\u660e\u6587\n * @param encryptText  \uff1a \u8fdb\u884c\u52a0\u76d0\u6458\u8981\u7b97\u6cd5\u540e\u7684\u5bc6\u6587\n * @return \u5982\u679c\u52a0\u76d0\u540e\u548c\u539f\u660e\u6587text\u4e00\u81f4\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\n *\/\n bool EncryptUtil::verify( string text,  string encryptText) {\n    char md5Text&#91;32];\n    char salt&#91;16];\n    \/\/\u9996\u5148\u5c06\u52a0\u76d0\u540emd5\u7b97\u6cd5\u6458\u8981\u5f97\u5230\u7684encryptText\u5bc6\u6587\u8fdb\u884c\u62c6\u5206\u6210\u4e00\u4e2a32\u4f4d\u7684\u666e\u901amd5\u6458\u8981\u7b97\u6cd5\u5bc6\u6587md5Text\u548c\u4e00\u4e2a16\u4f4d\u7684\u5b57\u7b26\u4e32salt\u3002\n    \/\/\u8fd9\u91cc\u5c31\u662f\u8fdb\u884csalt\u548cmd5\u6458\u8981\u5bc6\u6587\u5f62\u621048\u4f4d\u65b0\u7684\u5b57\u7b26\u4e32encryptText\u65f6\u7684\u9006\u5411\u64cd\u4f5c\u3002encryptText\u4e2dsalt&#91;i] =  encryptText&#91; 3* i + 1], i = 0,1,2,...16\n    for (int i = 0; i &lt; 48; i += 3) {\n        md5Text&#91;i \/ 3 * 2] = encryptText&#91;i];\n        md5Text&#91;i \/ 3 * 2 + 1] = encryptText&#91;i + 2];\n        salt&#91;i \/ 3] = encryptText&#91;i + 1];\n    }\n    \/\/\u5c06\u62c6\u5206\u768416\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\u8ffdsalt\u52a0\u5230\u660e\u6587text\u4e2d\n    for (int  i = 0; i &lt; 16; i++)\n    {\n        text.append(1,salt&#91;i]);\n    }\n    \/\/\u4f7f\u7528md5\u6458\u8981\u7b97\u6cd5\u8fdb\u884c\u751f\u6210\u5bc6\u6587\u5f97\u5230enCs2\n    string enCs2 = md5Hex(text);\n    \/\/\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u76f8\u7b49\uff0c\u5982\u679c\u76f8\u7b49\u5219\u8fd4\u56detrue\uff0c\u53ea\u8981\u6709\u4e00\u4e2a\u5b57\u7b26\u4e0d\u76f8\u7b49\u5c31\u8fd4\u56defalse\n    for (int i = 0; i &lt; enCs2.size(); i++)\n    {\n        if (enCs2&#91;i] != md5Text&#91;i])\n        {\n            return false;\n        }\n    }\n    return true;\n}\n \/**\n  \u9010\u4e2a\u6bd4\u8f83\u4e24\u4e2a\u5bc6\u6587\u76f8\u540c\u4f4d\u7f6e\u4e0a\u5b57\u7b26\u662f\u5426\u76f8\u540c\u6765\u8ba1\u7b97\u4e24\u4e2a\u660e\u6587\u8fdb\u884cmd5\u52a0\u5bc6\u540e\u7684\u76f8\u4f3c\u5ea6\u3002\n @param str1: md5\u6458\u8981\u7b97\u6cd5\u540e\u7684\u5b57\u7b26\u4e32\u5bc6\u6587\n @param str2:md5\u6458\u8981\u7b97\u6cd5\u540e\u7684\u5b57\u7b26\u4e32\u5bc6\u6587\n @return double: \u8fd4\u56de\u76f8\u4f3c\u5ea6\n *\/\ndouble EncryptUtil::similarityDegree(const string&amp; str1, const string&amp; str2)\n{\n    if (str1.size() &lt;= 0 &amp;&amp; (str1.size() != str2.size()))\n    {\n        return 0;\n    }\n    int count = 0;\n    \/\/\u7edf\u8ba1\u5bf9\u5e94\u4f4d\u7f6e\u4e0a\u5b57\u7b26\u76f8\u540c\u7684\u4e2a\u6570\n    for (int i = 0; i &lt; str1.size(); i++)\n    {\n        if (str1&#91;i] == str2&#91;i])\n        {\n            count++;\n        }\n    }\n    \/\/\u8ba1\u7b97\u76f8\u4f3c\u5ea6\n    double result = count * 1.0 \/ str1.size();\n    return result;\n}\n\n\n\nEncryptUtil::~EncryptUtil()\n{\n}<\/code><\/pre>\n\n\n\n<h3>4.3 MD5.h<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#pragma once\n#ifndef MD5_H\n#define MD5_H\n#include &lt;string>\n#include &lt;fstream>\nusing namespace std;\n\/* Type define *\/\ntypedef unsigned char byte;\ntypedef unsigned int uint32;\n\/* MD5 declaration. *\/\nclass MD5 {\npublic:\n    MD5();\n    MD5(const void *input, size_t length);\n    MD5(const string &amp;str);\n    MD5(ifstream &amp;in);\n    void update(const void *input, size_t length);\n    void update(const string &amp;str);\n    void update(ifstream &amp;in);\n    const byte* digest();\n    string toString();\nprivate:\n    void reset();\n    void update(const byte *input, size_t length);\n    void final();\n    void transform(const byte block&#91;64]);\n    void encode(const uint32 *input, byte *output, size_t length);\n    void decode(const byte *input, uint32 *output, size_t length);\n    string bytesToHexString(const byte *input, size_t length);\n    \/* class uncopyable *\/\n    MD5(const MD5&amp;);\nprivate:\n    uint32 _state&#91;4];   \/* state (ABCD) *\/\n    uint32 _count&#91;2];   \/* number of bits, modulo 2^64 (low-order word first) *\/\n    byte _buffer&#91;64];   \/* input buffer *\/\n    byte _digest&#91;16];   \/* message digest *\/\n    bool _finished;     \/* calculate finished ? *\/\n    static const byte PADDING&#91;64];  \/* padding for calculate *\/\n    static const char HEX&#91;16];\n    static const size_t BUFFER_SIZE = 1024;\n};\n#endif\/*MD5_H*\/<\/code><\/pre>\n\n\n\n<h3>4.4<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"md5.h\"\n\n\n\n\/* Constants for MD5Transform routine. *\/\n#define S11 7\n#define S12 12\n#define S13 17\n#define S14 22\n#define S21 5\n#define S22 9\n#define S23 14\n#define S24 20\n#define S31 4\n#define S32 11\n#define S33 16\n#define S34 23\n#define S41 6\n#define S42 10\n#define S43 15\n#define S44 21\n\n\n\/* F, G, H and I are basic MD5 functions.\n*\/\n#define F(x, y, z) (((x) &amp; (y)) | ((~x) &amp; (z)))\n#define G(x, y, z) (((x) &amp; (z)) | ((y) &amp; (~z)))\n#define H(x, y, z) ((x) ^ (y) ^ (z))\n#define I(x, y, z) ((y) ^ ((x) | (~z)))\n\n\/* ROTATE_LEFT rotates x left n bits.\n*\/\n#define ROTATE_LEFT(x, n) (((x) &lt;&lt; (n)) | ((x) >> (32-(n))))\n\n\/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.\nRotation is separate from addition to prevent recomputation.\n*\/\n#define FF(a, b, c, d, x, s, ac) { \\\n    (a) += F ((b), (c), (d)) + (x) + ac; \\\n    (a) = ROTATE_LEFT ((a), (s)); \\\n    (a) += (b); \\\n}\n#define GG(a, b, c, d, x, s, ac) { \\\n    (a) += G ((b), (c), (d)) + (x) + ac; \\\n    (a) = ROTATE_LEFT ((a), (s)); \\\n    (a) += (b); \\\n}\n#define HH(a, b, c, d, x, s, ac) { \\\n    (a) += H ((b), (c), (d)) + (x) + ac; \\\n    (a) = ROTATE_LEFT ((a), (s)); \\\n    (a) += (b); \\\n}\n#define II(a, b, c, d, x, s, ac) { \\\n    (a) += I ((b), (c), (d)) + (x) + ac; \\\n    (a) = ROTATE_LEFT ((a), (s)); \\\n    (a) += (b); \\\n}\nconst byte MD5::PADDING&#91;64] = { 0x80 };\nconst char MD5::HEX&#91;16] = {\n    '0', '1', '2', '3',\n    '4', '5', '6', '7',\n    '8', '9', 'a', 'b',\n    'c', 'd', 'e', 'f'\n};\n\/* Default construct. *\/\nMD5::MD5() {\n    reset();\n}\n\/* Construct a MD5 object with a input buffer. *\/\nMD5::MD5(const void *input, size_t length) {\n    reset();\n    update(input, length);\n}\n\n\/* Construct a MD5 object with a string. *\/\nMD5::MD5(const string &amp;str) {\n    reset();\n    update(str);\n}\n\n\/* Construct a MD5 object with a file. *\/\nMD5::MD5(ifstream &amp;in) {\n    reset();\n    update(in);\n}\n\n\/* Return the message-digest *\/\nconst byte* MD5::digest() {\n    if (!_finished) {\n        _finished = true;\n        final();\n    }\n    return _digest;\n}\n\n\/* Reset the calculate state *\/\nvoid MD5::reset() {\n\n    _finished = false;\n    \/* reset number of bits. *\/\n    _count&#91;0] = _count&#91;1] = 0;\n    \/* Load magic initialization constants. *\/\n    _state&#91;0] = 0x67452301;\n    _state&#91;1] = 0xefcdab89;\n    _state&#91;2] = 0x98badcfe;\n    _state&#91;3] = 0x10325476;\n\n}\n\n\/* Updating the context with a input buffer. *\/\nvoid MD5::update(const void *input, size_t length) {\n    update((const byte*)input, length);\n}\n\n\/* Updating the context with a string. *\/\nvoid MD5::update(const string &amp;str) {\n    update((const byte*)str.c_str(), str.length());\n}\n\n\/* Updating the context with a file. *\/\nvoid MD5::update(ifstream &amp;in) {\n    if (!in)\n        return;\n\n    std::streamsize length;\n    char buffer&#91;BUFFER_SIZE];\n    while (!in.eof()) {\n        in.read(buffer, BUFFER_SIZE);\n        length = in.gcount();\n        if (length > 0)\n            update(buffer, length);\n    }\n    in.close();\n}\n\n\/* MD5 block update operation. Continues an MD5 message-digest\noperation, processing another message block, and updating the\ncontext.\n*\/\nvoid MD5::update(const byte *input, size_t length) {\n\n    uint32 i, index, partLen;\n\n    \/\/_finished = false;\n    this->reset();\n    \/* Compute number of bytes mod 64 *\/\n    index = (uint32)((_count&#91;0] >> 3) &amp; 0x3f);\n\n    \/* update number of bits *\/\n    if ((_count&#91;0] += ((uint32)length &lt;&lt; 3)) &lt; ((uint32)length &lt;&lt; 3))\n        _count&#91;1]++;\n    _count&#91;1] += ((uint32)length >> 29);\n\n    partLen = 64 - index;\n\n    \/* transform as many times as possible. *\/\n    if (length >= partLen) {\n\n        memcpy(&amp;_buffer&#91;index], input, partLen);\n        transform(_buffer);\n\n        for (i = partLen; i + 63 &lt; length; i += 64)\n            transform(&amp;input&#91;i]);\n        index = 0;\n\n    }\n    else {\n        i = 0;\n    }\n\n    \/* Buffer remaining input *\/\n    memcpy(&amp;_buffer&#91;index], &amp;input&#91;i], length - i);\n}\n\n\/* MD5 finalization. Ends an MD5 message-_digest operation, writing the\nthe message _digest and zeroizing the context.\n*\/\nvoid MD5::final() {\n\n    byte bits&#91;8];\n    uint32 oldState&#91;4];\n    uint32 oldCount&#91;2];\n    uint32 index, padLen;\n\n    \/* Save current state and count. *\/\n    memcpy(oldState, _state, 16);\n    memcpy(oldCount, _count, 8);\n\n    \/* Save number of bits *\/\n    encode(_count, bits, 8);\n\n    \/* Pad out to 56 mod 64. *\/\n    index = (uint32)((_count&#91;0] >> 3) &amp; 0x3f);\n    padLen = (index &lt; 56) ? (56 - index) : (120 - index);\n    update(PADDING, padLen);\n\n    \/* Append length (before padding) *\/\n    update(bits, 8);\n\n    \/* Store state in digest *\/\n    encode(_state, _digest, 16);\n\n    \/* Restore current state and count. *\/\n    memcpy(_state, oldState, 16);\n    memcpy(_count, oldCount, 8);\n}\n\n\/* MD5 basic transformation. Transforms _state based on block. *\/\nvoid MD5::transform(const byte block&#91;64]) {\n\n    uint32 a = _state&#91;0], b = _state&#91;1], c = _state&#91;2], d = _state&#91;3], x&#91;16];\n\n    decode(block, x, 64);\n\n    \/* Round 1 *\/\n    FF(a, b, c, d, x&#91;0], S11, 0xd76aa478); \/* 1 *\/\n    FF(d, a, b, c, x&#91;1], S12, 0xe8c7b756); \/* 2 *\/\n    FF(c, d, a, b, x&#91;2], S13, 0x242070db); \/* 3 *\/\n    FF(b, c, d, a, x&#91;3], S14, 0xc1bdceee); \/* 4 *\/\n    FF(a, b, c, d, x&#91;4], S11, 0xf57c0faf); \/* 5 *\/\n    FF(d, a, b, c, x&#91;5], S12, 0x4787c62a); \/* 6 *\/\n    FF(c, d, a, b, x&#91;6], S13, 0xa8304613); \/* 7 *\/\n    FF(b, c, d, a, x&#91;7], S14, 0xfd469501); \/* 8 *\/\n    FF(a, b, c, d, x&#91;8], S11, 0x698098d8); \/* 9 *\/\n    FF(d, a, b, c, x&#91;9], S12, 0x8b44f7af); \/* 10 *\/\n    FF(c, d, a, b, x&#91;10], S13, 0xffff5bb1); \/* 11 *\/\n    FF(b, c, d, a, x&#91;11], S14, 0x895cd7be); \/* 12 *\/\n    FF(a, b, c, d, x&#91;12], S11, 0x6b901122); \/* 13 *\/\n    FF(d, a, b, c, x&#91;13], S12, 0xfd987193); \/* 14 *\/\n    FF(c, d, a, b, x&#91;14], S13, 0xa679438e); \/* 15 *\/\n    FF(b, c, d, a, x&#91;15], S14, 0x49b40821); \/* 16 *\/\n\n    \/* Round 2 *\/\n    GG(a, b, c, d, x&#91;1], S21, 0xf61e2562); \/* 17 *\/\n    GG(d, a, b, c, x&#91;6], S22, 0xc040b340); \/* 18 *\/\n    GG(c, d, a, b, x&#91;11], S23, 0x265e5a51); \/* 19 *\/\n    GG(b, c, d, a, x&#91;0], S24, 0xe9b6c7aa); \/* 20 *\/\n    GG(a, b, c, d, x&#91;5], S21, 0xd62f105d); \/* 21 *\/\n    GG(d, a, b, c, x&#91;10], S22, 0x2441453); \/* 22 *\/\n    GG(c, d, a, b, x&#91;15], S23, 0xd8a1e681); \/* 23 *\/\n    GG(b, c, d, a, x&#91;4], S24, 0xe7d3fbc8); \/* 24 *\/\n    GG(a, b, c, d, x&#91;9], S21, 0x21e1cde6); \/* 25 *\/\n    GG(d, a, b, c, x&#91;14], S22, 0xc33707d6); \/* 26 *\/\n    GG(c, d, a, b, x&#91;3], S23, 0xf4d50d87); \/* 27 *\/\n    GG(b, c, d, a, x&#91;8], S24, 0x455a14ed); \/* 28 *\/\n    GG(a, b, c, d, x&#91;13], S21, 0xa9e3e905); \/* 29 *\/\n    GG(d, a, b, c, x&#91;2], S22, 0xfcefa3f8); \/* 30 *\/\n    GG(c, d, a, b, x&#91;7], S23, 0x676f02d9); \/* 31 *\/\n    GG(b, c, d, a, x&#91;12], S24, 0x8d2a4c8a); \/* 32 *\/\n\n    \/* Round 3 *\/\n    HH(a, b, c, d, x&#91;5], S31, 0xfffa3942); \/* 33 *\/\n    HH(d, a, b, c, x&#91;8], S32, 0x8771f681); \/* 34 *\/\n    HH(c, d, a, b, x&#91;11], S33, 0x6d9d6122); \/* 35 *\/\n    HH(b, c, d, a, x&#91;14], S34, 0xfde5380c); \/* 36 *\/\n    HH(a, b, c, d, x&#91;1], S31, 0xa4beea44); \/* 37 *\/\n    HH(d, a, b, c, x&#91;4], S32, 0x4bdecfa9); \/* 38 *\/\n    HH(c, d, a, b, x&#91;7], S33, 0xf6bb4b60); \/* 39 *\/\n    HH(b, c, d, a, x&#91;10], S34, 0xbebfbc70); \/* 40 *\/\n    HH(a, b, c, d, x&#91;13], S31, 0x289b7ec6); \/* 41 *\/\n    HH(d, a, b, c, x&#91;0], S32, 0xeaa127fa); \/* 42 *\/\n    HH(c, d, a, b, x&#91;3], S33, 0xd4ef3085); \/* 43 *\/\n    HH(b, c, d, a, x&#91;6], S34, 0x4881d05); \/* 44 *\/\n    HH(a, b, c, d, x&#91;9], S31, 0xd9d4d039); \/* 45 *\/\n    HH(d, a, b, c, x&#91;12], S32, 0xe6db99e5); \/* 46 *\/\n    HH(c, d, a, b, x&#91;15], S33, 0x1fa27cf8); \/* 47 *\/\n    HH(b, c, d, a, x&#91;2], S34, 0xc4ac5665); \/* 48 *\/\n\n    \/* Round 4 *\/\n    II(a, b, c, d, x&#91;0], S41, 0xf4292244); \/* 49 *\/\n    II(d, a, b, c, x&#91;7], S42, 0x432aff97); \/* 50 *\/\n    II(c, d, a, b, x&#91;14], S43, 0xab9423a7); \/* 51 *\/\n    II(b, c, d, a, x&#91;5], S44, 0xfc93a039); \/* 52 *\/\n    II(a, b, c, d, x&#91;12], S41, 0x655b59c3); \/* 53 *\/\n    II(d, a, b, c, x&#91;3], S42, 0x8f0ccc92); \/* 54 *\/\n    II(c, d, a, b, x&#91;10], S43, 0xffeff47d); \/* 55 *\/\n    II(b, c, d, a, x&#91;1], S44, 0x85845dd1); \/* 56 *\/\n    II(a, b, c, d, x&#91;8], S41, 0x6fa87e4f); \/* 57 *\/\n    II(d, a, b, c, x&#91;15], S42, 0xfe2ce6e0); \/* 58 *\/\n    II(c, d, a, b, x&#91;6], S43, 0xa3014314); \/* 59 *\/\n    II(b, c, d, a, x&#91;13], S44, 0x4e0811a1); \/* 60 *\/\n    II(a, b, c, d, x&#91;4], S41, 0xf7537e82); \/* 61 *\/\n    II(d, a, b, c, x&#91;11], S42, 0xbd3af235); \/* 62 *\/\n    II(c, d, a, b, x&#91;2], S43, 0x2ad7d2bb); \/* 63 *\/\n    II(b, c, d, a, x&#91;9], S44, 0xeb86d391); \/* 64 *\/\n\n    _state&#91;0] += a;\n    _state&#91;1] += b;\n    _state&#91;2] += c;\n    _state&#91;3] += d;\n}\n\n\/* Encodes input (ulong) into output (byte). Assumes length is\na multiple of 4.\n*\/\nvoid MD5::encode(const uint32 *input, byte *output, size_t length) {\n\n    for (size_t i = 0, j = 0; j &lt; length; i++, j += 4) {\n        output&#91;j] = (byte)(input&#91;i] &amp; 0xff);\n        output&#91;j + 1] = (byte)((input&#91;i] >> 8) &amp; 0xff);\n        output&#91;j + 2] = (byte)((input&#91;i] >> 16) &amp; 0xff);\n        output&#91;j + 3] = (byte)((input&#91;i] >> 24) &amp; 0xff);\n    }\n}\n\n\/* Decodes input (byte) into output (ulong). Assumes length is\na multiple of 4.\n*\/\nvoid MD5::decode(const byte *input, uint32 *output, size_t length) {\n\n    for (size_t i = 0, j = 0; j &lt; length; i++, j += 4) {\n        output&#91;i] = ((uint32)input&#91;j]) | (((uint32)input&#91;j + 1]) &lt;&lt; 8) |\n            (((uint32)input&#91;j + 2]) &lt;&lt; 16) | (((uint32)input&#91;j + 3]) &lt;&lt; 24);\n    }\n}\n\n\/* Convert byte array to hex string. *\/\nstring MD5::bytesToHexString(const byte *input, size_t length) {\n    string str;\n    str.reserve(length &lt;&lt; 1);\n    for (size_t i = 0; i &lt; length; i++) {\n        int t = input&#91;i];\n        int a = t \/ 16;\n        int b = t % 16;\n        str.append(1, HEX&#91;a]);\n        str.append(1, HEX&#91;b]);\n    }\n    return str;\n}\n\n\n\/* Convert digest to string value *\/\nstring MD5::toString() {\n    return bytesToHexString(digest(), 16);\n}<\/code><\/pre>\n\n\n\n<h2>5.\u6d4b\u8bd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;iostream>\n#include\"Md5.h\"\n#include&lt;string>\n#include\"EncryptUtil.h\"\nusing namespace std;\n\nvoid test() {\n    string text = \"abcd\";\n    EncryptUtil util;\n    string encodeText = util.encryptBySalt(text);\n    string encodeText1 = util.encryptBySalt(text);\n    cout &lt;&lt; \"md5\u52a0\u76d0\u540e\u52a0\u5bc6\u7684md5\u503c:\" + encodeText &lt;&lt; endl;\n    cout &lt;&lt; \"md5\u52a0\u76d0\u540e\u52a0\u5bc6\u7684md5\u503c:\" + encodeText1 &lt;&lt; endl;\n    if (encodeText.compare(encodeText1) == 0)\n    {\n        cout &lt;&lt; \"\u4e24\u6b21\u751f\u6210\u7684\u52a0\u76d0\u5bc6\u6587\u76f8\u7b49\uff01\" &lt;&lt; endl;\n    }\n    else {\n        cout &lt;&lt; \"\u4e24\u6b21\u751f\u6210\u7684\u52a0\u76d0\u5bc6\u6587\u4e0d\u76f8\u7b49\uff01\" &lt;&lt; endl;\n    }\n    bool fla = util.verify(text, encodeText);\n    if (fla == true)\n    {\n        cout &lt;&lt; \"\u539f\u6765\u7684\u660e\u6587\u548c\u52a0\u76d0\u540e\u7684\u660e\u6587\u4e00\u81f4\u5f97\u5230\u9a8c\u8bc1!\" &lt;&lt; endl;\n    }\n    else {\n        cout &lt;&lt; \"\u539f\u6765\u7684\u660e\u6587\u548c\u52a0\u76d0\u540e\u7684\u660e\u6587\u4e0d\u4e00\u81f4!\" &lt;&lt; endl;\n    }\n  cout&lt;&lt;\"\u76f8\u4f3c\u5ea6\u4e3a\uff1a\"&lt;&lt;util.similarityDegree(encodeText,encodeText1)&lt;&lt;endl;\n}\nint main() {\n    test();\n    system(\"pause\");\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h2>6.\u770b\u5230\u8fd9\u8bf7\u52a8\u4e0b\u624b\u70b9\u4e2a\u8d5e\uff0c\u6bd5\u7adf\u6574\u7406\u7b14\u8bb0\u4e0d\u6613\u3002<\/h2>\n\n\n\n<h2><\/h2>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++\u5b9e\u73b0MD5\u6458\u8981\u7b97\u6cd5\u52a0\u76d0salt\u503c 1.\u4fe1\u606f\u6458\u8981\u51fd\u6570 1.1Hash\u51fd\u6570 \u3000\u3000\u54c8\u5e0c\u51fd\u6570\u5c31\u662f\u80fd\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u6570\u636e\u6620\u5c04\u4e3a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[163],"tags":[168,166,167,165],"_links":{"self":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/429"}],"collection":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/comments?post=429"}],"version-history":[{"count":1,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/429\/revisions"}],"predecessor-version":[{"id":430,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/429\/revisions\/430"}],"wp:attachment":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/media?parent=429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/categories?post=429"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/tags?post=429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}