{"id":2598,"date":"2018-03-31T19:06:47","date_gmt":"2018-03-31T11:06:47","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2598"},"modified":"2018-03-31T19:06:47","modified_gmt":"2018-03-31T11:06:47","slug":"securerandom-%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2598","title":{"rendered":"SecureRandom \u6559\u7a0b"},"content":{"rendered":"<p>\n\t\u8bf4\u5b9e\u8bdd\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u5de5\u4f5c\u4e86\u597d\u51e0\u5e74\u7684\u4e00\u4e2a\u8001\u7a0b\u5e8f\u5458\u3002\u6211\u5bf9&nbsp;SecureRandom \u4e86\u89e3\u7684\u4e5f\u4e0d\u662f\u5f88\u591a\uff0c\u77e5\u9053\u6700\u8fd1\u5728\u8bfb\u4e00\u4e2a\u6846\u67b6\u7684\u6e90\u7801\u65f6\u9047\u5230\u4e86 SecureRandom \u8fd9\u4e2a\u7c7b\u3002\u6211\u4e00\u4e0b\u5b50\u5c31\u88ab\u4ed6\u5438\u5f15\u4f4f\u4e86\uff0cSecureRandom \u7c7b\u63d0\u4f9b\u52a0\u5bc6\u7684\u5f3a\u968f\u673a\u6570\u751f\u6210\u5668\u3002\u4e0b\u9762\u6211\u4eec\u4e00\u8d77\u6765\u5b66\u4e60\u5b83\u5427\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"SecureRandom \u6559\u7a0b\" src=\"http:\/\/img.bbs.csdn.net\/upload\/201705\/29\/1496043392_683305.png\" style=\"width: 640px;height: 287px\" \/>\n<\/p>\n<p>\n\tRandom \u7c7b\u4e2d\u5b9e\u73b0\u7684\u968f\u673a\u7b97\u6cd5\u662f\u4f2a\u968f\u673a\uff0c\u4e5f\u5c31\u662f\u6709\u89c4\u5219\u7684\u968f\u673a\u3002\u5728\u8fdb\u884c\u968f\u673a\u65f6\uff0c\u968f\u673a\u7b97\u6cd5\u7684\u8d77\u6e90\u6570\u5b57\u79f0\u4e3a\u79cd\u5b50\u6570(seed)\uff0c\u5728\u79cd\u5b50\u6570\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u4e00\u5b9a\u7684\u53d8\u6362\uff0c\u4ece\u800c\u4ea7\u751f\u9700\u8981\u7684\u968f\u673a\u6570\u5b57\u3002\u76f8\u540c\u79cd\u5b50\u6570\u7684 Random \u5bf9\u8c61\uff0c\u76f8\u540c\u6b21\u6570\u751f\u6210\u7684\u968f\u673a\u6570\u5b57\u662f\u5b8c\u5168\u76f8\u540c\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e24\u4e2a\u79cd\u5b50\u6570\u76f8\u540c\u7684 Random \u5bf9\u8c61\uff0c\u751f\u6210\u7684\u968f\u673a\u6570\u5b57\u5b8c\u5168\u76f8\u540c\u3002\u6240\u4ee5\u5728\u9700\u8981\u9891\u7e41\u751f\u6210\u968f\u673a\u6570\uff0c\u6216\u8005\u5b89\u5168\u8981\u6c42\u8f83\u9ad8\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u4f7f\u7528Random\uff0c\u56e0\u4e3a\u5176\u751f\u6210\u7684\u503c\u5176\u5b9e\u662f\u53ef\u4ee5\u9884\u6d4b\u7684\u3002\n<\/p>\n<p>\n\tSecureRandom \u548c Random \u90fd\u662f\uff0c\u4e5f\u662f\u5982\u679c\u79cd\u5b50\u4e00\u6837\uff0c\u4ea7\u751f\u7684\u968f\u673a\u6570\u4e5f\u4e00\u6837\uff1a \u56e0\u4e3a\u79cd\u5b50\u786e\u5b9a\uff0c\u968f\u673a\u6570\u7b97\u6cd5\u4e5f\u786e\u5b9a\uff0c\u56e0\u6b64\u8f93\u51fa\u662f\u786e\u5b9a\u7684\u3002\u53ea\u662f\u8bf4\uff0cSecureRandom \u7c7b\u6536\u96c6\u4e86\u4e00\u4e9b\u968f\u673a\u4e8b\u4ef6\uff0c\u6bd4\u5982\u9f20\u6807\u70b9\u51fb\uff0c\u952e\u76d8\u70b9\u51fb\u7b49\u7b49\uff0cSecureRandom \u4f7f\u7528\u8fd9\u4e9b\u968f\u673a\u4e8b\u4ef6\u4f5c\u4e3a\u79cd\u5b50\u3002\u8fd9\u610f\u5473\u7740\uff0c\u79cd\u5b50\u662f\u4e0d\u53ef\u9884\u6d4b\u7684\uff0c\u800c\u4e0d\u50cf Random \u9ed8\u8ba4\u4f7f\u7528\u7cfb\u7edf\u5f53\u524d\u65f6\u95f4\u7684\u6beb\u79d2\u6570\u4f5c\u4e3a\u79cd\u5b50\uff0c\u6709\u89c4\u5f8b\u53ef\u5bfb\u3002\n<\/p>\n<p>\n\tSecureRandom \u5185\u7f6e\u4e24\u79cd\u968f\u673a\u6570\u7b97\u6cd5\uff0cNativePRNG \u548c SHA1PRNG\u3002\u4e0b\u9762\u6211\u4eec\u5206\u522b\u6765\u5b66\u4e60\u5b83\u4eec\u3002\n<\/p>\n<p>\n\t\u901a\u8fc7 new \u6765\u521d\u59cb\u5316\uff0c\u9ed8\u8ba4\u6765\u8bf4\u4f1a\u4f7f\u7528 NativePRNG \u7b97\u6cd5\u751f\u6210\u968f\u673a\u6570\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u914d\u7f6e -Djava.security \u53c2\u6570\u6765\u4fee\u6539\u8c03\u7528\u7684\u7b97\u6cd5\uff0c\u5982\u679c\u662f \/dev\/[u]random \u4e24\u8005\u4e4b\u4e00\u5c31\u662f NativePRNG\uff0c\u5426\u5219\u5c31\u662f SHA1PRNG\u3002\u5728JVM\u542f\u52a8\u65f6\u8bbe\u7f6e\u4e0b\u9762\u7684\u53c2\u6570\uff0c\u5373\u53ef\u5207\u6362\u7b97\u6cd5\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n-Djava.security=file:\/dev\/urandom \r\n-Djava.security=file:\/dev\/random<\/pre>\n<p>\n\t\u4e0a\u9762\u4e24\u4e2a\u8bbe\u7f6e\u4e00\u4e2a\u5373\u53ef\uff0c\u4e0d\u8981\u91cd\u590d\u8bbe\u7f6e\u53c2\u6570\u3002\n<\/p>\n<p>\n\t\u53e6\u5916\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7 SecureRandom.getInstance \u83b7\u53d6\u4e0d\u540c\u7684\u7b97\u6cd5\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nSecureRandom secureRandom = new SecureRandom();\r\nSecureRandom secureRandom3 = SecureRandom.getInstance(&quot;SHA1PRNG&quot;);\r\nSecureRandom secureRandom2 = SecureRandom.getInstance(&quot;SHA1PRNG&quot;, &quot;SUN&quot;);<\/pre>\n<p>\n\tSecureRandom \u63d0\u4f9b\u7684\u4e00\u4e9b\u65b9\u6cd5\u548c&nbsp;Random \u7c7b\u4f3c\u3002\u901a\u8fc7&nbsp;nextInt \u5373\u53ef\u83b7\u53d6\u968f\u673a\u6570\u3002\n<\/p>\n<p>\n\tnextBytes(byte[] bytes) \u53ef\u4ee5\u83b7\u53d6\u968f\u673a\u7684\u4e00\u4e2abyte\u6570\u7ec4\uff0c\u6ce8\u610f\u8fd9\u91cc\u4e0d\u662f\u8fd4\u56de\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u662fvoid\u8fd4\u56de\u7c7b\u578b\uff0c\u662f\u76f4\u63a5\u968f\u673a\u6539\u53d8\u4e86 test\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nSecureRandom random = new SecureRandom();\r\nbyte[] test = new byte[20];\r\nrandom.nextBytes(test);<\/pre>\n<p>\n\tgenerateSeed(int numBytes) \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u4e00\u4e2a\u968f\u673a\u7684byte\u6570\u7ec4\uff0c\u8fd9\u4e2a\u6570\u7ec4\u4e2d\u7684\u6570\u901a\u5e38\u53ef\u4ee5\u7528\u6765\u505a\u5176\u4ed6\u968f\u673a\u751f\u6210\u5668\u7684\u79cd\u5b50\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nbyte seed[] = random.generateSeed(20);<\/pre>\n<p>\n\t\u4ea7\u751f\u9ad8\u5f3a\u5ea6\u7684\u968f\u673a\u6570\uff0c\u6709\u4e24\u4e2a\u91cd\u8981\u7684\u56e0\u7d20\uff1a\u79cd\u5b50\u548c\u7b97\u6cd5\u3002\u5f53\u7136\u7b97\u6cd5\u662f\u53ef\u4ee5\u6709\u5f88\u591a\u7684\uff0c\u4f46\u662f\u5982\u4f55\u9009\u62e9\u79cd\u5b50\u662f\u975e\u5e38\u5173\u952e\u7684\u56e0\u7d20\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nprivate List&lt;List&lt;Integer&gt;&gt; generateCakes(int num, int seedLength, int rowLen) {\r\n\tSecureRandom random = new SecureRandom();\r\n\tbyte[] seeds = SecureRandom.getSeed(seedLength); \/\/\u83b7\u53d6\u968f\u673a\u7684byte\u6570\u7ec4\uff0c\u7528\u6765\u540e\u7eed\u4f5c\u4e3a\u79cd\u5b50\r\n\tint counter = 0;\r\n\tint realCount = 0;\r\n\tint tmprows = 0;\r\n\tList&lt;List&lt;Integer&gt;&gt; CakesList = new ArrayList&lt;List&lt;Integer&gt;&gt;();\r\n\twhile (num &gt; tmprows) {\r\n\t\tList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();\r\n\t\twhile (counter &lt; rowLen) {\r\n\t\t\trandom.setSeed(seeds); \/\/\u8bbe\u7f6e\u79cd\u5b50\r\n\t\t\tint cake = random.nextInt(38); \/\/\u968f\u673a\u751f\u62100-37\u7684\u6570\u5b57\r\n\t\t\tif (!list.contains(cake) &amp;&amp; 0 != cake) {\r\n\t\t\t\tlist.add(cake);\r\n\t\t\t\tcounter++;\r\n\t\t\t}\r\n\t\t\trandom.nextBytes(seeds); \/\/\u968f\u673a\u83b7\u53d6\u65b0\u7684byte\u6570\u7ec4\u7528\u4ee5\u4f5c\u4e3a\u4e0b\u6b21\u7684\u79cd\u5b50\uff0c\u4e0d\u65ad\u5faa\u73af\r\n\t\t\trealCount++;\r\n\t\t}\r\n\t\tCollections.sort(list);\r\n\t\tpairs++;\r\n\t\ttmprows++;\r\n\t\tcounter = 0;\r\n\t\tCakesList.add(list);\r\n\t\tif (pairs % Constants.MSG_COUNT == 0) {\r\n\t\t\tSystem.out.println(pairs + &quot; cakes generated.&quot;);\r\n\t\t}\r\n\t}\r\n\tSystem.out.println(&quot;\u4e71\u6570\u53d6\u5f97\u56de\u6570\uff1a&quot; + realCount);\r\n\treturn CakesList;\r\n}<\/pre>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"http:\/\/calvin1978.blogcn.com\/articles\/securerandom.html\" name=\"SecureRandom\u7684\u6c5f\u6e56\u504f\u65b9\u4e0e\u771f\u5b9e\u6548\u679c\" target=\"_blank\" title=\"SecureRandom\u7684\u6c5f\u6e56\u504f\u65b9\u4e0e\u771f\u5b9e\u6548\u679c\">SecureRandom\u7684\u6c5f\u6e56\u504f\u65b9\u4e0e\u771f\u5b9e\u6548\u679c<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/www.jianshu.com\/p\/2f6acd169202#\">Java \u968f\u673a\u6570 Random VS SecureRandom<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/www.bbsmax.com\/A\/kjdwxba6zN\/\">Java\u4e2d\u7684\u968f\u673a\u6570\u751f\u6210\u5668\uff1aRandom\uff0cThreadLocalRandom\uff0cSecureRandom\uff08\u8f6c\uff09<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/ju.outofmemory.cn\/entry\/286621\">Java\u4e2d\u751f\u6210\u968f\u673a\u6570Random\u3001ThreadLocalRandom\u3001SecureRandom<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/www.apihome.cn\/api\/java\/SecureRandom.html\">API\u53c2\u8003&nbsp;SecureRandom(\u793a\u4f8b,\u51fa\u9519\u4ee3\u7801)<\/a>\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2598\">SecureRandom \u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u8bf4\u5b9e\u8bdd\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u5de5\u4f5c\u4e86\u597d\u51e0\u5e74\u7684\u4e00\u4e2a\u8001\u7a0b\u5e8f\u5458\u3002\u6211\u5bf9&nbsp;SecureRandom \u4e86\u89e3\u7684\u4e5f\u4e0d\u662f\u5f88\u591a\uff0c\u77e5\u9053\u6700 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[69,565,567,308,211],"class_list":["post-2598","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-java-","tag-top4java","tag-211"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2598"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2598\/revisions"}],"predecessor-version":[{"id":2599,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2598\/revisions\/2599"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}