{"id":193341,"date":"2020-11-16T02:40:15","date_gmt":"2020-11-15T18:40:15","guid":{"rendered":"http:\/\/4563.org\/?p=193341"},"modified":"2020-11-16T02:40:15","modified_gmt":"2020-11-15T18:40:15","slug":"aes-256-%e7%94%9f%e6%88%90%e7%a7%98%e9%92%a5%e7%9a%84%e6%97%b6%e5%80%99-%e5%8f%af%e4%bb%a5%e4%b8%8d%e4%bd%bf%e7%94%a8-securerandom-%e7%9a%84%e7%94%9f%e6%88%90%e7%9a%84%e5%86%99%e6%b3%95%e5%90%97","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=193341","title":{"rendered":"AES 256 \u751f\u6210\u79d8\u94a5\u7684\u65f6\u5019 \u53ef\u4ee5\u4e0d\u4f7f\u7528 SecureRandom \u7684\u751f\u6210\u7684\u5199\u6cd5\u5417"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  AES 256 \u751f\u6210\u79d8\u94a5\u7684\u65f6\u5019 \u53ef\u4ee5\u4e0d\u4f7f\u7528 SecureRandom \u7684\u751f\u6210\u7684\u5199\u6cd5\u5417               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rqxiao <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u67e5\u5230\u4e00\u822c\u7684\u5199\u6cd5\u662f \u5229\u7528\u4e86 SecureRandom<\/p>\n<p>private static SecretKeySpec getSecretKey(final String key) { \/\/\u8fd4\u56de\u751f\u6210\u6307\u5b9a\u7b97\u6cd5\u5bc6\u94a5\u751f\u6210\u5668\u7684 KeyGenerator \u5bf9\u8c61 KeyGenerator kg = null;<\/p>\n<pre><code>    try {         kg = KeyGenerator.getInstance(KEY_ALGORITHM);          SecureRandom secureRandom = SecureRandom.getInstance(\"SHA1PRNG\");         secureRandom.setSeed(key.getBytes(\"utf-8\"));         kg.init(256, secureRandom);          \/\/\u751f\u6210\u4e00\u4e2a\u5bc6\u94a5         SecretKey secretKey = kg.generateKey();         \/\/ \u8f6c\u6362\u4e3a AES \u4e13\u7528\u5bc6\u94a5         return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);     } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {         Logger.getLogger(AESUtil.class.getName()).log(Level.SEVERE, null, ex);     }      return null; } <\/code><\/pre>\n<p>\u4e0d\u4f7f\u7528 SecureRandom \u4e5f\u80fd\u4e00\u53e5\u8bdd\u9ed8\u8ba4\u751f\u6210 128 \u957f\u5ea6\u7684<\/p>\n<pre><code>        return new SecretKeySpec(key.getBytes(\"ASCII\"), KEY_ALGORITHM); <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>11<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4109236\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BrettD <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528\u5b89\u5168\u968f\u673a\u6570\u751f\u6210\u5668\u751f\u6210\u7684\u968f\u673a\u5bc6\u94a5\u597d\u4e00\u4e9b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4109237\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rqxiao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5c31\u662f\u60f3\u8bf7\u95ee\u4e0b<br \/>AES 256 \u751f\u6210\u79d8\u94a5\u7684\u65f6\u5019 \u4e0d\u4f7f\u7528 SecureRandom \u53ef\u4ee5\u5417\uff0c\u5199\u6cd5\u662f\u4ec0\u4e48\u6837\u7684<\/p>\n<p>\u8fd8\u6709 SecureRandom \u8fd9\u79cd\u5199\u6cd5\u5176\u5b9e\u7528\u8fc7 \uff0c\u4f46\u662f\u4e5f\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u6bcf\u6b21\u52a0\u5bc6\u89e3\u5bc6\u90fd\u662f\u90fd\u662f\u5229\u7528 SecureRandom \u751f\u6210\u7684 key\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u8fd9\u4e2a key \u5e76\u4e0d\u662f\u201c\u968f\u673a\u7684\u201d\uff0c\u52a0\u5bc6\u89e3\u5bc6\u5176\u5b9e\u90fd\u662f\u7528\u540c\u4e00\u4e2a key                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4109238\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wakzz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @rqxiao <br \/>&#8220;`<br \/>SecureRandom secureRandom = SecureRandom.getInstance(&#8220;SHA1PRNG&#8221;);<br \/>secureRandom.setSeed(key.getBytes(&#8220;utf-8&#8221;));<br \/>&#8220;`<br \/>\u8fd9\u4e24\u884c\u7684\u542b\u4e49\u662f\u901a\u8fc7\u7528\u6237\u6307\u5b9a\u7684\u79cd\u5b50\u751f\u6210\u4e00\u4e2a\u5b89\u5168\u79d8\u94a5\uff0c\u540c\u4e00\u4e2a\u79cd\u5b50\u591a\u6b21\u8c03\u7528\u5f53\u7136\u751f\u6210\u7684\u90fd\u662f\u540c\u4e00\u4e2a\u5b89\u5168\u79d8\u94a5\u3002\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u968f\u673a\u751f\u6210\u4e00\u4e2a\u52a0\u5bc6\u79d8\u94a5\uff0c\u53ea\u9700\u8981\u79d8\u94a5\u7684\u6bd4\u7279\u957f\u5ea6\u6ca1\u9519\u5c31\u884c\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4109239\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : GM <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5f53\u7136\u53ef\u4ee5\uff0c\u79d8\u94a5\u5c31\u662f\u4e00\u4e32\u968f\u673a byte[]\u6570\u7ec4\u800c\u5df2\uff0c\u7528\u4efb\u4f55\u65b9\u6cd5\u90fd\u53ef\u4ee5\u751f\u6210\u3002\u4f46\u662f\u7528\u8fd9\u4e2a\u5f88\u660e\u663e\u5b89\u5168\u6027\u66f4\u597d\u554a\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4109240\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jim9606 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e4b\u6240\u4ee5\u8981\u7528 SecureRandom \u662f\u56e0\u4e3a\u4f60\u9700\u8981\u907f\u514d\u4f60\u4f7f\u7528\u7684\u5bc6\u94a5\u88ab\u9884\u6d4b\u51fa\u6765\u3002<br \/>\u5e38\u89c4\u7684\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\uff08 PRNG \uff09\uff0c\u4f8b\u5982\u7ebf\u6027\u540c\u4f59 PRNG\uff0c\u751f\u6210\u7684\u5185\u5bb9\u662f\u53ef\u4ee5\u88ab\u9884\u6d4b\u7684\uff08\u5c31\u662f\u8bf4\uff0c\u653b\u51fb\u8005\u53ef\u4ee5\u6839\u636e\u751f\u6210\u5668\u5df2\u7ecf\u751f\u6210\u7684\u968f\u673a\u6570\u63a8\u6d4b\u4e4b\u540e\u7684\u968f\u673a\u6570\uff09\uff0c\u800c\u4f60\u53ef\u80fd\u5728\u5176\u4ed6\u975e\u4fdd\u5bc6\u5e94\u7528\u4e2d\u4f7f\u7528\u8fc7 PRNG \u5bfc\u81f4\u653b\u51fb\u8005\u53ef\u4ee5\u83b7\u6089\u4ee5\u524d\u4ea7\u751f\u7684\u968f\u673a\u6570\u3002\u56e0\u6b64\uff0c\u5bc6\u7801\u7cfb\u7edf\u751f\u6210\u968f\u673a\u6570\u5e94\u8be5\u4f7f\u7528\u8fd9\u79cd\u4e0d\u53ef\u88ab\u9884\u6d4b\u7684\u5bc6\u7801\u5b66\u5b89\u5168\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\uff08 CSPRNG \uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4109241\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ye4tar <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u4e0d\u5408\u89c4\uff0c\u4e0a\u6b21\u6709\u4e2a\u5bf9\u63a5\u7684\uff0c\u7531\u4e8e\u4ed6\u7684\u4ee3\u7801\u548c Android \u4e0d\u517c\u5bb9\uff0c\u7ed3\u679c\u6ca1\u5f97\u529e\u6cd5\uff0c\u76f4\u63a5 secretKey.getEncoded()\u5199\u6b7b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4109242\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : imdong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bb0\u5f97 Win \u4e0b Putty \u7684 Keygen \u662f\u5728\u751f\u6210\u65f6\u8981\u6c42\u7528\u6237\u968f\u673a\u79fb\u52a8\u9f20\u6807\u6216\u6309\u952e\u6765\u83b7\u53d6\u968f\u673a\u6570\u79cd\u5b50\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4109243\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sunxiansong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8865\u5145\u4e2a\u4f8b\u5b50\uff1a\u751f\u6210\u767b\u9646 token,\u4e00\u822c\u6765\u8bf4\u4e3b\u8981\u7684\u9700\u6c42\u662f token \u552f\u4e00\uff0c\u90a3\u4e48\u662f\u4e0d\u662f\u53ef\u4ee5\u7528 uuid\uff0c\u7b54\u6848\u662f\u4e0d\u884c\uff0c\u56e0\u4e3a\u5bb9\u6613\u88ab\u9884\u6d4b\uff0c\u964d\u4f4e\u78b0\u649e\u96be\u5ea6\u3002\u4f60\u5148\u7528\u6b63\u5e38\u8d26\u53f7\u767b\u9646\uff0c\u62ff\u5230 token\uff0c\u4ece\u8fd9\u4e2a uuid \u5f80\u540e\u78b0\uff0c\u96be\u5ea6\u4e0d\u5927\uff0c\u78b0\u5bf9\u4e86\u5c31\u80fd\u767b\u4e0a\u5176\u4ed6\u4eba\u7684\u8d26\u53f7\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4109244\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jrue0011 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @rqxiao \u7b2c\u4e00\u4e2a\u65b9\u6cd5\u6700\u7ec8\u751f\u6210\u7684 SecretKeySpec \u76f8\u540c\u7684\u539f\u56e0\u662f\u4f20\u5165\u4e86\u76f8\u540c\u7684\u53c2\u6570 key(String)\u5e76\u4f7f\u7528\u8fd9\u4e2a key.getBytes(&#8220;utf-8&#8221;)\u5f97\u5230\u7684\u76f8\u540c byte[]\u4f5c\u4e3a\u968f\u673a\u6570\u79cd\u5b50\u3002<br \/>\u76f4\u63a5\u8c03\u7528 kg.init(256)\u7684\u8bdd\u5185\u90e8\u4f1a\u4f7f\u7528\u4e00\u4e2a\u6ca1\u6709\u4f20\u5165\u968f\u673a\u6570\u79cd\u5b50\u7684 SecureRandom \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4109245\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kahlkn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bdd\u8bf4\u6bcf\u6b21\u751f\u6210\u5bc6\u94a5\u65f6\u90fd\u8981\u8c03\u7528 \u201cSecureRandom.getInstance(&#8220;SHA1PRNG&#8221;);\u201d \u6765\u521b\u5efa\u4e00\u4e2a SecureRandom \u5bf9\u8c61\u771f\u7684\u597d\u5417\uff1f\u636e\u6211\u6240\u77e5 SecureRandom \u7684\u521b\u5efa\u5e94\u8be5\u662f \u6bd4\u8f83\u8017\u8d39\u6027\u80fd\u7684\uff0c\u6240\u4ee5\u4e00\u822c\u7684\u7528\u9014\u4e5f\u662f \u521b\u5efa\u5b8c\u4e4b\u540e\u9759\u6001\u5316\u6301\u6709\uff0c\u7136\u540e init \u7684\u65f6\u5019\u4f20\u8fdb\u53bb\uff08 JDK \u5e95\u5c42\u5c31\u662f\u7528\u4e00\u4e2a\u7c7b\u6765\u9759\u6001\u6301\u6709\uff09\u3002<\/p>\n<p>\u6240\u4ee5\u7531\u6b64\u89e3\u51b3\u4e86\u4f60\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4e0d\u4f7f\u7528 SecureRandom \u751f\u6210\uff0c\u4e0d\u4f7f\u7528\u4f1a\u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684 SecureRandom \u6765\u66ff\u4ee3\u7684\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4109246\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rqxiao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Jrue0011 KeyGenerator.init \uff08 256 \uff09\u7684\u786e\u662f\u6709\u4e2a\u9ed8\u8ba4\u7684 SecureRandom \u7684\uff0c\u6211\u767e\u5ea6\u51fa\u7684\u4e00\u4e9b aes \u52a0\u5bc6\u7f51\u7ad9 \uff0c\u9a8c\u8bc1\u4e0b\u6765 128 \u7684\u65f6\u5019\u4ed6\u4eec\u662f\u6ca1\u6709\u7528 SecureRandom\uff0c\u6240\u4ee5\u5c31\u60f3\u8bd5\u4e0b 256 \u7684\u5199\u6cd5 \u3002\u4e0d\u8fc7\u5927\u5bb6\u90fd\u8bf4 SecureRandom \u7684\u786e\u66f4\u5b89\u5168\uff0c256 \u8fd9\u4e2a\u4e5f\u6ca1\u5fc5\u8981\u8bd5\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>AES 256 \u751f\u6210\u79d8\u94a5\u7684\u65f6\u5019 \u53ef&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\/193341"}],"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=193341"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/193341\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=193341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=193341"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=193341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}