{"id":128430,"date":"2020-06-22T03:32:34","date_gmt":"2020-06-21T19:32:34","guid":{"rendered":"http:\/\/4563.org\/?p=128430"},"modified":"2020-06-22T03:32:34","modified_gmt":"2020-06-21T19:32:34","slug":"%e4%b8%80%e4%b8%aa-aes-js-%e5%ba%93-%e9%87%8c%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95%e7%94%a8-php-%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=128430","title":{"rendered":"\u4e00\u4e2a aes-js \u5e93 \u91cc\u52a0\u5bc6\u7b97\u6cd5\u7528 PHP \u600e\u4e48\u5199?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e00\u4e2a aes-js \u5e93 \u91cc\u52a0\u5bc6\u7b97\u6cd5\u7528 PHP \u600e\u4e48\u5199?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wangwenfan <\/span>  <span><i><\/i> 65<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6587\u6863\u5730\u5740\uff1ahttps:\/\/github.com\/ricmoo\/aes-js#readme<\/p>\n<p>\u4e0b\u9762\u662f\u5b98\u65b9\u6587\u6863 demo \u4ee3\u7801<\/p>\n<pre><code>const aesjs = require('aes-js'); \/\/ An example 128-bit key var key = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];  \/\/ The initialization vector (must be 16 bytes) var iv = [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,35, 36 ];  \/\/ Convert text to bytes (must be a multiple of the segment size you choose below) var text = 'TextMustBeAMultipleOfSegmentSize'; var textBytes = aesjs.utils.utf8.toBytes(text);  \/\/ The segment size is optional, and defaults to 1 var segmentSize = 8; \/\/ console.log(iv); \/\/  var aesCfb = new aesjs.ModeOfOperation.cfb(key, iv, segmentSize); \/\/ var encryptedBytes = aesCfb.encrypt(textBytes);  \/\/ To print or store the binary data, you may convert it to hex var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes); console.log(encryptedHex); \/\/ \"55e3af2638c560b4fdb9d26a630733ea60197ec23deb85b1f60f71f10409ce27\"  \/\/ When ready to decrypt the hex string, convert it back to bytes var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);  \/\/ The cipher feedback mode of operation maintains internal state, \/\/ so to decrypt a new instance must be instantiated. var aesCfb = new aesjs.ModeOfOperation.cfb(key, iv, 8); var decryptedBytes = aesCfb.decrypt(encryptedBytes);  \/\/ Convert our bytes back into text var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes); console.log(decryptedText); \/\/ \"TextMustBeAMultipleOfSegmentSize\" <\/code><\/pre>\n<ul>\n<li>\u600e\u4e48\u6539\u9020\u4e0b\u7528 php \u6765\u5199?<\/li>\n<\/ul>\n<pre><code>&lt;?php \/\/ \u52a0\u5bc6\u7b97\u6cd5 $encryptMethod = 'aes-128-cfb'; \/\/ \u660e\u6587\u6570\u636e $data = 'TextMustBeAMultipleOfSegmentSize'; $secret = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; \/\/ \u751f\u6210 IV $ivLength = openssl_cipher_iv_length($encryptMethod); $iv = openssl_random_pseudo_bytes($ivLength, $isStrong); if (false === $iv &amp;&amp; false === $isStrong) {  die('IV generate failed'); } \/\/ \u52a0\u5bc6 $encrypted = openssl_encrypt($data, $encryptMethod, $secret, 8, $iv);  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"43229\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zy445566 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5199\u4e2a\u9524\u5b50\uff0c\u7528 node \u8d77 http \u670d\u52a1\uff0c\u7528 PHP \u53bb\u6389\u7528\u4e0d\u5c31\u5b8c\u4e8b\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"43230\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiangyuecn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528\u56fa\u5b9a iv\u3001\u5bc6\u7801\u3001\u586b\u5145\u65b9\u5f0f \u4e0d\u7ba1\u7528\u4ec0\u4e48\u8bed\u8a00\u540c\u4e00\u660e\u6587 AES \u52a0\u5bc6\u51fa\u6765\u90fd\u662f\u4e00\u6837\u7684\u3002\u6240\u4ee5\u4e0d\u7528\u8003\u8651\u600e\u4e48\u6539\u9020\u3002\u3002\u3002\u76f4\u63a5\u7814\u7a76 PHP \u600e\u4e48\u8fdb\u884c AES \u52a0\u5bc6\u3002\u6ce8\u610f\uff1ajs \u90a3\u91cc\u7684 iv \u662f\u56fa\u5b9a\u7684\uff0c\u4e0d\u80fd\u4e71\u5199\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"43231\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : wangwenfan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xiangyuecn \u6211\u6709\u70b9\u4e0d\u61c2\u7684\u662f\u4e0a\u9762 js \u7684 iv \u5b9a\u4e49\u7684\u662f\u6570\u7ec4\uff0c\u770b\u4e86\u6587\u6863 php \u7684 iv \u4e00\u822c\u662f\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e24\u4e2a\u600e\u4e48\u6765\u7edf\u4e00                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a aes-js \u5e93 \u91cc\u52a0\u5bc6\u7b97\u6cd5&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/128430"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=128430"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/128430\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=128430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=128430"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=128430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}