{"id":2425,"date":"2025-07-06T15:27:01","date_gmt":"2025-07-06T07:27:01","guid":{"rendered":"https:\/\/www.ddosidc.com\/wp\/?p=2425"},"modified":"2025-07-06T15:27:01","modified_gmt":"2025-07-06T07:27:01","slug":"%e5%ba%94%e5%af%b9-php-%e4%b8%ad-mcrypt_get_block_size-%e6%8a%a5%e9%94%99%e7%9a%84%e6%9c%89%e6%95%88%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/www.ddosidc.com\/wp\/2425.html","title":{"rendered":"\u5e94\u5bf9 PHP \u4e2d mcrypt_get_block_size \u62a5\u9519\u7684\u6709\u6548\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<p><p>\u5728\u4f7f\u7528 PHP \u8fdb\u884c\u52a0\u5bc6\u65f6\uff0c\u8bb8\u591a\u5f00\u53d1\u8005\u4f1a\u9762\u4e34\u4e0e <strong>mcrypt_get_block_size<\/strong> \u51fd\u6570\u76f8\u5173\u7684\u9519\u8bef\u3002\u6b64\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u83b7\u53d6\u52a0\u5bc6\u64cd\u4f5c\u4e2d\u6240\u4f7f\u7528\u5757\u7684\u5927\u5c0f\uff0c\u4f46\u5728 PHP \u66f4\u65b0\u540e\uff0cmcrypt \u6269\u5c55\u5df2\u4e0d\u518d\u88ab\u652f\u6301\uff0c\u56e0\u6b64\u53ef\u80fd\u5f15\u53d1\u4e00\u7cfb\u5217\u95ee\u9898\u3002\u672c\u6587\u5c06\u6307\u5bfc\u4f60\u5982\u4f55\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u5e76\u8f6c\u5411\u4e00\u4e2a\u66f4\u73b0\u4ee3\u7684\u52a0\u5bc6\u65b9\u6848\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.ddosidc.com\/wp\/wp-content\/uploads\/2025\/05\/DL7ie171J4.jpg\" alt=\"\u5e94\u5bf9 PHP \u4e2d mcrypt_get_block_size \u62a5\u9519\u7684\u6709\u6548\u89e3\u51b3\u65b9\u6848\" title=\"\u5e94\u5bf9 PHP \u4e2d mcrypt_get_block_size \u62a5\u9519\u7684\u6709\u6548\u89e3\u51b3\u65b9\u6848\"><\/p>\n<\/p>\n<p><h2>\u64cd\u4f5c\u524d\u51c6\u5907\u4e0e\u80cc\u666f\u4ecb\u7ecd<\/h2>\n<\/p>\n<p><p>\u5728 PHP 7.1 \u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\uff0c<strong>mcrypt<\/strong> \u6269\u5c55\u5df2\u7ecf\u88ab\u79fb\u9664\u3002\u4f7f\u7528\u6b64\u6269\u5c55\u7684\u4ee3\u7801\u5c06\u5728\u8fd0\u884c\u65f6\u89e6\u53d1\u9519\u8bef\uff0c\u7279\u522b\u662f\u8c03\u7528 <strong>mcrypt_get_block_size<\/strong> \u65f6\u53ef\u80fd\u51fa\u73b0\u201c\u672a\u5b9a\u4e49\u51fd\u6570\u201d\u7684\u9519\u8bef\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u5efa\u8bae\u4f7f\u7528 <strong>openssl<\/strong> \u6269\u5c55\u6216\u5176\u4ed6\u73b0\u4ee3\u5e93\uff0c\u4f8b\u5982 <strong>defuse\/php-encryption<\/strong>\u3002<\/p>\n<\/p>\n<p><h2>\u89e3\u51b3\u6b65\u9aa4<\/h2>\n<\/p>\n<p><h3>1. \u68c0\u67e5 PHP \u7248\u672c<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u786e\u8ba4\u4f60\u7684 PHP \u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u5f53\u524d\u5b89\u88c5\u7684 PHP \u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code>php -v<\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u786e\u4fdd\u4f60\u7684\u7248\u672c\u662f 7.1 \u4ee5\u4e0a\u3002\u5982\u679c\u662f\uff0c\u53ef\u4ee5\u7ee7\u7eed\u8fdb\u884c\u4e0b\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><h3>2. \u66f4\u65b0\u52a0\u5bc6\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u5bfb\u627e\u6240\u6709\u4f7f\u7528 <strong>mcrypt<\/strong> \u51fd\u6570\u7684\u90e8\u5206\uff0c\u7279\u522b\u662f <strong>mcrypt_get_block_size<\/strong>\u3002\u4f8b\u5982\uff0c\u539f\u6765\u7684\u4ee3\u7801\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>$block_size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u6b64\u4ee3\u7801\u66ff\u6362\u4e3a\u4f7f\u7528 <strong>openssl<\/strong> \u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>$block_size = openssl_cipher_iv_length('aes-128-cbc'); \/\/ \u66ff\u6362\u4e3a\u4f60\u6240\u4f7f\u7528\u7684\u52a0\u5bc6\u7b97\u6cd5<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86 <strong>openssl_cipher_iv_length<\/strong> \u51fd\u6570\u83b7\u53d6\u5fc5\u8981\u7684\u5757\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>3. \u5b8c\u6574\u7684\u52a0\u5bc6\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 OpenSSL \u7684\u5b8c\u6574\u52a0\u5bc6\u548c\u89e3\u5bc6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>\/\/ \u52a0\u5bc6<\/p>\r\n<p>function encrypt($data, $key) {<\/p>\r\n<p>    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-128-cbc'));<\/p>\r\n<p>    $encrypted = openssl_encrypt($data, 'aes-128-cbc', $key, 0, $iv);<\/p>\r\n<p>    return base64_encode($encrypted . '::' . $iv);<\/p>\r\n<p>}<\/p>\r\n\r\n<p>\/\/ \u89e3\u5bc6<\/p>\r\n<p>function decrypt($data, $key) {<\/p>\r\n<p>    list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);<\/p>\r\n<p>    return openssl_decrypt($encrypted_data, 'aes-128-cbc', $key, 0, $iv);<\/p>\r\n<p>}<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6ce8\u610f\uff1a\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8bf7\u786e\u4fdd\u5bc6\u94a5\u7684\u5b89\u5168\u6027\uff0c\u5e76\u4f7f\u7528\u5f3a\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><h3>4. \u5b89\u88c5 OpenSSL \u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684 PHP \u6ca1\u6709\u542f\u7528 OpenSSL \u6269\u5c55\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e Ubuntu \u7cfb\u7edf\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/li>\n<p><pre><code>sudo apt-get install php-openssl<\/code><\/pre>\n<\/p>\n<li>\u5bf9\u4e8e CentOS \u7cfb\u7edf\uff0c\u4f7f\u7528\uff1a<\/li>\n<p><pre><code>sudo yum install php-openssl<\/code><\/pre>\n<\/p>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u91cd\u542f\u4f60\u7684 web \u670d\u52a1\u5668\uff1a<\/li>\n<p><pre><code>sudo service apache2 restart<\/code><\/pre>\n<\/p>\n<p><pre><code>sudo service nginx restart<\/code><\/pre>\n<\/p>\n<\/ul>\n<p><h2>\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><h3>1. PHP \u6269\u5c55\u672a\u542f\u7528<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5728\u4f7f\u7528 OpenSSL \u65f6\u4ecd\u7136\u9047\u5230\u95ee\u9898\uff0c\u68c0\u67e5 PHP \u914d\u7f6e\u6587\u4ef6\uff08<strong>php.ini<\/strong>\uff09\uff0c\u786e\u4fdd\u4ee5\u4e0b\u884c\u6ca1\u6709\u88ab\u6ce8\u91ca\uff1a<\/p>\n<\/p>\n<p><pre><code>extension=openssl<\/code><\/pre>\n<\/p>\n<p><h3>2. \u52a0\u5bc6\u7b97\u6cd5\u7684\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u52a0\u5bc6\u7b97\u6cd5\u65f6\uff0c\u8bf7\u786e\u4fdd\u9009\u62e9\u7b26\u5408\u4f60\u5b89\u5168\u9700\u6c42\u7684\u7b97\u6cd5\uff0c\u5e76\u6ce8\u610f\u4e00\u4e9b\u7b97\u6cd5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4e0d\u518d\u5b89\u5168\uff0c\u4f8b\u5982\u4f7f\u7528\u5c0f\u4e8e 128 \u4f4d\u7684\u5bc6\u94a5\u6216\u8fc7\u65f6\u7684\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>3. \u8f6f\u4ef6\u7248\u672c\u66f4\u65b0<\/h3>\n<\/p>\n<p><p>\u4fdd\u6301 PHP \u548c\u76f8\u5173\u5e93\u7684\u6700\u65b0\u7248\u672c\uff0c\u53ef\u4ee5\u63d0\u5347\u7cfb\u7edf\u7684\u5b89\u5168\u6027\uff0c\u540c\u65f6\u51cf\u5c0f\u517c\u5bb9\u95ee\u9898\u7684\u53d1\u751f\u51e0\u7387\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u968f\u7740 PHP \u7684\u4e0d\u65ad\u66f4\u65b0\uff0c\u4f7f\u7528\u8fc7\u65f6\u7684\u6269\u5c55\u5982 <strong>mcrypt<\/strong> \u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u8fd0\u884c\u4e0d\u6b63\u5e38\u3002\u901a\u8fc7\u4f7f\u7528 <strong>openssl<\/strong> \u6765\u66ff\u4ee3\u4e4b\u524d\u7684\u52a0\u5bc6\u65b9\u6cd5\uff0c\u4e0d\u4ec5\u89e3\u51b3\u4e86\u517c\u5bb9\u6027\u7684\u95ee\u9898\uff0c\u4e5f\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u5b89\u5168\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u786e\u4fdd\u4ee3\u7801\u53ef\u4ee5\u987a\u5229\u8fd0\u884c\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4f7f\u7528 PHP \u8fdb\u884c\u52a0\u5bc6\u65f6\uff0c\u8bb8\u591a\u5f00\u53d1\u8005\u4f1a\u9762\u4e34\u4e0e mcrypt_get_block_size \u51fd\u6570\u76f8\u5173\u7684\u9519\u8bef\u3002\u6b64\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u83b7\u53d6\u52a0\u5bc6\u64cd\u4f5c\u4e2d\u6240\u4f7f\u7528\u5757\u7684\u5927\u5c0f\uff0c\u4f46\u5728 PHP \u66f4\u65b0\u540e\uff0cmcrypt \u6269\u5c55\u5df2\u4e0d\u518d\u88ab\u652f\u6301\uff0c\u56e0\u6b64\u53ef\u80fd\u5f15\u53d1\u4e00\u7cfb\u5217\u95ee\u9898\u3002\u672c\u6587\u5c06\u6307\u5bfc&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2426,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-2425","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-baike"],"_links":{"self":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts\/2425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/comments?post=2425"}],"version-history":[{"count":1,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts\/2425\/revisions"}],"predecessor-version":[{"id":2428,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts\/2425\/revisions\/2428"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/media\/2426"}],"wp:attachment":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/media?parent=2425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/categories?post=2425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/tags?post=2425"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/topic?post=2425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}