{"id":1209,"date":"2014-09-05T16:25:33","date_gmt":"2014-09-05T08:25:33","guid":{"rendered":"http:\/\/wx.wosign.com\/?p=1209"},"modified":"2014-09-05T16:25:33","modified_gmt":"2014-09-05T08:25:33","slug":"rsa%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95java%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/wx.wosign.com\/?p=1209","title":{"rendered":"RSA\u52a0\u5bc6\u7b97\u6cd5java\u7b80\u5355\u5b9e\u73b0"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p style=\"font: 16px\/28px 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', \u5fae\u8f6f\u96c5\u9ed1, sans-serif; margin: 0px 0px 0.75em; color: #333333; text-transform: none; text-indent: 1em; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #ffffff; -webkit-text-stroke-width: 0px;\">\u7b80\u5355\u5b8c\u6574\u7684\u4ee3\u7801\uff0c\u901a\u8fc7\u8fd9\u4e2a\u4ee3\u7801\u4f60\u5c06\u5bf9RSA\u52a0\u5bc6\u7b97\u6cd5\u5728Java\u4e2d\u7684\u5b9e\u73b0\u65b9\u6cd5\u6709\u4e00\u4e2a\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u8fd9\u4e2a\u7c7b\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\uff0c\u6c34\u5e73\u9ad8\u7684\uff0c\u5c31\u81ea\u5df1\u4fee\u6539\u5b8c\u5584\u4e0b\u4ee3\u7801\u3002<\/p>\n<pre class=\"prettyprint cs\" style=\"font: 16px\/1.5em Monaco, Menlo, Consolas, 'Courier New', monospace; margin: 0px 0px 1.5em; padding: 0.5em; border-radius: 4px; border: 1px solid rgba(0, 0, 0, 0.14902); color: #333333; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; display: block; white-space: pre-wrap; word-break: break-all; overflow-y: auto; word-wrap: break-word; background-color: #f8f8ff; -webkit-text-stroke-width: 0px;\">package security;\r\nimport java.security.*;\r\nimport java.security.spec.*;\r\nimport java.security.interfaces.*;\r\nimport javax.crypto.spec.*;\r\nimport javax.crypto.interfaces.*;\r\nimport java.io.*;\r\nimport java.math.*;\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">public<\/span> <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">class<\/span> RSADemo {\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">public<\/span> RSADemo() {\r\n}\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">public<\/span> <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">static<\/span> <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">void<\/span> generateKey() {\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">try<\/span> {\r\nKeyPairGenerator kpg = KeyPairGenerator.getInstance(<span class=\"string\" style=\"color: #dd1144;\">\"RSA\"<\/span>);\r\nkpg.initialize(<span class=\"number\" style=\"color: #009999;\">1024<\/span>);\r\nKeyPair kp = kpg.genKeyPair();\r\nPublicKey pbkey = kp.getPublic();\r\nPrivateKey prkey = kp.getPrivate();\r\n<span class=\"comment\" style=\"color: #999988; font-style: italic;\">\/\/ \u4fdd\u5b58\u516c\u94a5<\/span>\r\nFileOutputStream f1 = <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> FileOutputStream(<span class=\"string\" style=\"color: #dd1144;\">\"pubkey.dat\"<\/span>);\r\nObjectOutputStream b1 = <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> ObjectOutputStream(f1);\r\nb1.writeObject(pbkey);\r\n<span class=\"comment\" style=\"color: #999988; font-style: italic;\">\/\/ \u4fdd\u5b58\u79c1\u94a5<\/span>\r\nFileOutputStream f2 = <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> FileOutputStream(<span class=\"string\" style=\"color: #dd1144;\">\"privatekey.dat\"<\/span>);\r\nObjectOutputStream b2 = <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> ObjectOutputStream(f2);\r\nb2.writeObject(prkey);\r\n} <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">catch<\/span> (Exception e) {\r\n}\r\n}\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">public<\/span> <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">static<\/span> <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">void<\/span> encrypt() throws Exception {\r\nString s = <span class=\"string\" style=\"color: #dd1144;\">\"Hello World!\"<\/span>;\r\n<span class=\"comment\" style=\"color: #999988; font-style: italic;\">\/\/ \u83b7\u53d6\u516c\u94a5\u53ca\u53c2\u6570e,n<\/span>\r\nFileInputStream f = <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> FileInputStream(<span class=\"string\" style=\"color: #dd1144;\">\"pubkey.dat\"<\/span>);\r\nObjectInputStream b = <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> ObjectInputStream(f);\r\nRSAPublicKey pbk = (RSAPublicKey) b.readObject();\r\nBigInteger e = pbk.getPublicExponent();\r\nBigInteger n = pbk.getModulus();\r\nSystem.<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span>.println(<span class=\"string\" style=\"color: #dd1144;\">\"e= \"<\/span> + e);\r\nSystem.<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span>.println(<span class=\"string\" style=\"color: #dd1144;\">\"n= \"<\/span> + n);\r\n<span class=\"comment\" style=\"color: #999988; font-style: italic;\">\/\/ \u83b7\u53d6\u660e\u6587m<\/span>\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">byte<\/span> ptext[] = s.getBytes(<span class=\"string\" style=\"color: #dd1144;\">\"UTF-8\"<\/span>);\r\nBigInteger m = <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> BigInteger(ptext);\r\n<span class=\"comment\" style=\"color: #999988; font-style: italic;\">\/\/ \u8ba1\u7b97\u5bc6\u6587c<\/span>\r\nBigInteger c = m.modPow(e, n);\r\nSystem.<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span>.println(<span class=\"string\" style=\"color: #dd1144;\">\"c= \"<\/span> + c);\r\n<span class=\"comment\" style=\"color: #999988; font-style: italic;\">\/\/ \u4fdd\u5b58\u5bc6\u6587<\/span>\r\nString cs = c.toString();\r\nBufferedWriter <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span> =\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> BufferedWriter(\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> OutputStreamWriter(<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> FileOutputStream(<span class=\"string\" style=\"color: #dd1144;\">\"encrypt.dat\"<\/span>)));\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span>.write(cs, <span class=\"number\" style=\"color: #009999;\">0<\/span>, cs.length());\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span>.close();\r\n}\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">public<\/span> <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">static<\/span> <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">void<\/span> decrypt() throws Exception {\r\n<span class=\"comment\" style=\"color: #999988; font-style: italic;\">\/\/ \u8bfb\u53d6\u5bc6\u6587<\/span>\r\nBufferedReader <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">in<\/span> =\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> BufferedReader(\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> InputStreamReader(<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> FileInputStream(<span class=\"string\" style=\"color: #dd1144;\">\"encrypt.dat\"<\/span>)));\r\nString ctext = <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">in<\/span>.readLine();\r\nBigInteger c = <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> BigInteger(ctext);\r\n<span class=\"comment\" style=\"color: #999988; font-style: italic;\">\/\/ \u8bfb\u53d6\u79c1\u94a5<\/span>\r\nFileInputStream f = <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> FileInputStream(<span class=\"string\" style=\"color: #dd1144;\">\"privatekey.dat\"<\/span>);\r\nObjectInputStream b = <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">new<\/span> ObjectInputStream(f);\r\nRSAPrivateKey prk = (RSAPrivateKey) b.readObject();\r\nBigInteger d = prk.getPrivateExponent();\r\n<span class=\"comment\" style=\"color: #999988; font-style: italic;\">\/\/ \u83b7\u53d6\u79c1\u94a5\u53c2\u6570\u53ca\u89e3\u5bc6<\/span>\r\nBigInteger n = prk.getModulus();\r\nSystem.<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span>.println(<span class=\"string\" style=\"color: #dd1144;\">\"d= \"<\/span> + d);\r\nSystem.<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span>.println(<span class=\"string\" style=\"color: #dd1144;\">\"n= \"<\/span> + n);\r\nBigInteger m = c.modPow(d, n);\r\n<span class=\"comment\" style=\"color: #999988; font-style: italic;\">\/\/ \u663e\u793a\u89e3\u5bc6\u7ed3\u679c<\/span>\r\nSystem.<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span>.println(<span class=\"string\" style=\"color: #dd1144;\">\"m= \"<\/span> + m);\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">byte<\/span>[] mt = m.toByteArray();\r\nSystem.<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span>.println(<span class=\"string\" style=\"color: #dd1144;\">\"PlainText is \"<\/span>);\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">for<\/span> (<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">int<\/span> i = <span class=\"number\" style=\"color: #009999;\">0<\/span>; i &lt; mt.length; i++) {\r\nSystem.<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span>.print((<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">char<\/span>) mt[i]);\r\n}\r\n}\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">public<\/span> <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">static<\/span> <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">void<\/span> main(String args[]) {\r\n<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">try<\/span> {\r\ngenerateKey();\r\nencrypt();\r\ndecrypt();\r\n} <span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">catch<\/span> (Exception e) {\r\nSystem.<span class=\"keyword\" style=\"color: #333333; font-weight: bold;\">out<\/span>.println(e.toString());\r\n}\r\n}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u7b80\u5355\u5b8c\u6574\u7684\u4ee3\u7801\uff0c\u901a\u8fc7\u8fd9\u4e2a\u4ee3\u7801\u4f60\u5c06\u5bf9RSA\u52a0\u5bc6\u7b97\u6cd5\u5728Java\u4e2d\u7684\u5b9e\u73b0\u65b9\u6cd5\u6709\u4e00\u4e2a\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u8fd9\u4e2a\u7c7b\uff0c &#8230; <a title=\"RSA\u52a0\u5bc6\u7b97\u6cd5java\u7b80\u5355\u5b9e\u73b0\" class=\"read-more\" href=\"https:\/\/wx.wosign.com\/?p=1209\" aria-label=\"More on RSA\u52a0\u5bc6\u7b97\u6cd5java\u7b80\u5355\u5b9e\u73b0\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/wx.wosign.com\/index.php?rest_route=\/wp\/v2\/posts\/1209"}],"collection":[{"href":"https:\/\/wx.wosign.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wx.wosign.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wx.wosign.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wx.wosign.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1209"}],"version-history":[{"count":1,"href":"https:\/\/wx.wosign.com\/index.php?rest_route=\/wp\/v2\/posts\/1209\/revisions"}],"predecessor-version":[{"id":1210,"href":"https:\/\/wx.wosign.com\/index.php?rest_route=\/wp\/v2\/posts\/1209\/revisions\/1210"}],"wp:attachment":[{"href":"https:\/\/wx.wosign.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wx.wosign.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wx.wosign.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}