{"id":736,"date":"2024-12-20T11:21:14","date_gmt":"2024-12-20T03:21:14","guid":{"rendered":"https:\/\/www.ikaros.love\/?p=736"},"modified":"2024-12-30T12:00:10","modified_gmt":"2024-12-30T04:00:10","slug":"%e8%af%a6%e8%a7%a3java%e7%a7%98%e9%92%a5%e4%ba%a4%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/www.ikaros.love\/?p=736","title":{"rendered":"\u79d8\u94a5\u5982\u4f55\u5b89\u5168\u4ea4\u6362"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"405\" height=\"405\" src=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/123.png\" alt=\"\" class=\"wp-image-737\" srcset=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/123.png 405w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/123-300x300.png 300w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/123-150x150.png 150w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/123-200x200.png 200w\" sizes=\"(max-width: 405px) 100vw, 405px\" \/><\/figure>\n\n\n\n<p class=\"has-large-font-size\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">\u9996\u5148\u770b\u770b\u4ee3\u7801\u5b9e\u73b0<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package java_se.encryption_security_example;\r\n\r\nimport javax.crypto.KeyAgreement;\r\nimport java.security.*;\r\nimport java.security.spec.X509EncodedKeySpec;\r\nimport java.util.HexFormat;\r\n\r\npublic class KeyExchangeAlgorithmExample {\r\n    public static void main(String&#91;] args) {\r\n        \/\/ Bob\u548cAlice:\r\n        Person bob = new Person(\"Bob\");\r\n        Person alice = new Person(\"Alice\");\r\n\r\n        \/\/ \u5404\u81ea\u751f\u6210KeyPair:\r\n        bob.generateKeyPair();\r\n        alice.generateKeyPair();\r\n\r\n        \/\/ \u53cc\u65b9\u4ea4\u6362\u5404\u81ea\u7684PublicKey:\r\n        \/\/ Bob\u6839\u636eAlice\u7684PublicKey\u751f\u6210\u81ea\u5df1\u7684\u672c\u5730\u5bc6\u94a5:\r\n        bob.generateSecretKey(alice.publicKey.getEncoded());\r\n        \/\/ Alice\u6839\u636eBob\u7684PublicKey\u751f\u6210\u81ea\u5df1\u7684\u672c\u5730\u5bc6\u94a5:\r\n        alice.generateSecretKey(bob.publicKey.getEncoded());\r\n\r\n        \/\/ \u68c0\u67e5\u53cc\u65b9\u7684\u672c\u5730\u5bc6\u94a5\u662f\u5426\u76f8\u540c:\r\n        bob.printKeys();\r\n        alice.printKeys();\r\n        \/\/ \u53cc\u65b9\u7684SecretKey\u76f8\u540c\uff0c\u540e\u7eed\u901a\u4fe1\u5c06\u4f7f\u7528SecretKey\u4f5c\u4e3a\u5bc6\u94a5\u8fdb\u884cAES\u52a0\u89e3\u5bc6...\r\n    }\r\n}\r\nclass Person {\r\n    public final String name;\r\n\r\n    public PublicKey publicKey;\r\n    private PrivateKey privateKey;\r\n    private byte&#91;] secretKey;\r\n\r\n    public Person(String name) {\r\n        this.name = name;\r\n    }\r\n\r\n    \/\/ \u751f\u6210\u672c\u5730KeyPair:\r\n    public void generateKeyPair() {\r\n        try {\r\n            KeyPairGenerator kpGen = KeyPairGenerator.getInstance(\"DH\");\r\n            kpGen.initialize(512);\r\n            KeyPair kp = kpGen.generateKeyPair();\r\n            this.privateKey = kp.getPrivate();\r\n            this.publicKey = kp.getPublic();\r\n        } catch (GeneralSecurityException e) {\r\n            throw new RuntimeException(e);\r\n        }\r\n    }\r\n\r\n    public void generateSecretKey(byte&#91;] receivedPubKeyBytes) {\r\n        try {\r\n            \/\/ \u4ecebyte&#91;]\u6062\u590dPublicKey:\r\n            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(receivedPubKeyBytes);\r\n            KeyFactory kf = KeyFactory.getInstance(\"DH\");\r\n            PublicKey receivedPublicKey = kf.generatePublic(keySpec);\r\n            \/\/ \u751f\u6210\u672c\u5730\u5bc6\u94a5:\r\n            KeyAgreement keyAgreement = KeyAgreement.getInstance(\"DH\");\r\n            keyAgreement.init(this.privateKey); \/\/ \u81ea\u5df1\u7684PrivateKey\r\n            keyAgreement.doPhase(receivedPublicKey, true); \/\/ \u5bf9\u65b9\u7684PublicKey\r\n            \/\/ \u751f\u6210SecretKey\u5bc6\u94a5:\r\n            this.secretKey = keyAgreement.generateSecret();\r\n        } catch (GeneralSecurityException e) {\r\n            throw new RuntimeException(e);\r\n        }\r\n    }\r\n\r\n    public void printKeys() {\r\n        System.out.println(\"Name: \" + this.name);\r\n        System.out.println(\"Private key: \" + HexFormat.of().formatHex(this.privateKey.getEncoded()));\r\n        System.out.println(\"Public key: \" + HexFormat.of().formatHex(this.publicKey.getEncoded()));\r\n        System.out.println(\"Secret key: \" + HexFormat.of().formatHex(this.secretKey));\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u8be6\u89e3\u4ee3\u7801\uff1a<\/mark><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8be6\u7ec6\u6b65\u9aa4<\/h3>\n\n\n\n<ol start=\"1\">\n<li><strong>\u521d\u59cb\u5316Person\u5bf9\u8c61 <\/strong>\uff1a\n<ul>\n<li>\u521b\u5efa\u4e24\u4e2aPerson\u5bf9\u8c61\uff0c\u5206\u522b\u4ee3\u8868Bob\u548cAlice\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u751f\u6210\u5bc6\u94a5\u5bf9 <\/strong>\uff1a\n<ul>\n<li>\u6bcf\u4e2aPerson\u5bf9\u8c61\u8c03\u7528<code>generateKeyPair()<\/code>\u65b9\u6cd5\uff0c\u4f7f\u7528DH\u7b97\u6cd5\u751f\u6210\u4e00\u4e2a\u5bc6\u94a5\u5bf9\uff08\u5305\u542b\u4e00\u4e2a\u516c\u94a5\u548c\u4e00\u4e2a\u79c1\u94a5\uff09\u3002\u8fd9\u91cc\u7684\u5bc6\u94a5\u957f\u5ea6\u8bbe\u7f6e\u4e3a512\u4f4d\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u901a\u5e38\u5efa\u8bae\u4f7f\u7528\u66f4\u957f\u7684\u5bc6\u94a5\u957f\u5ea6\u4ee5\u589e\u5f3a\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u4ea4\u6362\u516c\u94a5\u5e76\u751f\u6210\u79d8\u5bc6\u5bc6\u94a5 <\/strong>\uff1a\n<ul>\n<li>Bob\u548cAlice\u4ea4\u6362\u5404\u81ea\u7684\u516c\u94a5\u3002<\/li>\n\n\n\n<li>Bob\u4f7f\u7528Alice\u7684\u516c\u94a5\u548c\u81ea\u5df1\u7684\u79c1\u94a5\u6765\u8ba1\u7b97\u5171\u4eab\u7684\u79d8\u5bc6\u5bc6\u94a5\u3002<\/li>\n\n\n\n<li>Alice\u540c\u6837\u4f7f\u7528Bob\u7684\u516c\u94a5\u548c\u81ea\u5df1\u7684\u79c1\u94a5\u6765\u8ba1\u7b97\u5171\u4eab\u7684\u79d8\u5bc6\u5bc6\u94a5\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u9a8c\u8bc1\u79d8\u5bc6\u5bc6\u94a5\u4e00\u81f4\u6027 <\/strong>\uff1a\n<ul>\n<li>\u8c03\u7528<code>printKeys()<\/code>\u65b9\u6cd5\u6253\u5370\u51faBob\u548cAlice\u7684\u79c1\u94a5\u3001\u516c\u94a5\u4ee5\u53ca\u751f\u6210\u7684\u79d8\u5bc6\u5bc6\u94a5\uff0c\u9a8c\u8bc1\u4e24\u8005\u662f\u5426\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u5173\u952e\u6280\u672f\u70b9<\/h3>\n\n\n\n<ul>\n<li><strong>Diffie-Hellman\u5bc6\u94a5\u4ea4\u6362 <\/strong>\uff1a\u8fd9\u662f\u4e00\u79cd\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u5141\u8bb8\u4e24\u4e2a\u4e0d\u5171\u4eab\u4efb\u4f55\u5171\u540c\u4fe1\u606f\u7684\u7528\u6237\u5b89\u5168\u5730\u5efa\u7acb\u4e00\u4e2a\u5171\u4eab\u7684\u79d8\u5bc6\u5bc6\u94a5\u3002<\/li>\n\n\n\n<li><strong>KeyPairGenerator <\/strong>\uff1a\u7528\u4e8e\u751f\u6210\u516c\u94a5\/\u79c1\u94a5\u5bf9\u3002<\/li>\n\n\n\n<li><strong>X509EncodedKeySpec <\/strong>\uff1a\u7528\u4e8e\u5c06\u516c\u94a5\u4ece\u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u4e3aPublicKey\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong>KeyFactory <\/strong>\uff1a\u7528\u4e8e\u751f\u6210\u516c\u94a5\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong>KeyAgreement <\/strong>\uff1a\u7528\u4e8e\u6267\u884c\u5bc6\u94a5\u534f\u5546\u534f\u8bae\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684\u662fDH\u7b97\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"has-large-font-size\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">\u534f\u5546\u7b97\u6cd5\u8be6\u89e3<\/mark><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Diffie-Hellman \u5bc6\u94a5\u4ea4\u6362\u7b97\u6cd5<\/h3>\n\n\n\n<p>Diffie-Hellman \u5bc6\u94a5\u4ea4\u6362\u7b97\u6cd5\u662f\u4e00\u79cd\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u5b83\u5141\u8bb8\u4e24\u4e2a\u5b9e\u4f53\u5728\u6ca1\u6709\u9884\u5148\u5171\u4eab\u4efb\u4f55\u79d8\u5bc6\u7684\u60c5\u51b5\u4e0b\u5b89\u5168\u5730\u751f\u6210\u4e00\u4e2a\u5171\u4eab\u7684\u79d8\u5bc6\u5bc6\u94a5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"597\" src=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-91-1024x597.png\" alt=\"\" class=\"wp-image-739\" srcset=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-91-1024x597.png 1024w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-91-300x175.png 300w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-91-768x448.png 768w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-91-670x391.png 670w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-91.png 1349w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u76f4\u89c2\u7684\u6570\u5b57\u91cd\u73b0\u7b97\u6cd5<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"855\" height=\"1024\" src=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-92-855x1024.png\" alt=\"\" class=\"wp-image-740\" srcset=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-92-855x1024.png 855w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-92-250x300.png 250w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-92-768x920.png 768w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-92-1282x1536.png 1282w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-92-670x803.png 670w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-92.png 1304w\" sizes=\"(max-width: 855px) 100vw, 855px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"938\" height=\"1024\" src=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-93-938x1024.png\" alt=\"\" class=\"wp-image-741\" srcset=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-93-938x1024.png 938w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-93-275x300.png 275w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-93-768x838.png 768w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-93-670x731.png 670w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-93.png 1405w\" sizes=\"(max-width: 938px) 100vw, 938px\" \/><\/figure>\n\n\n\n<p>\u5982\u679c\u6e90\u6839\u548c\u5927\u7d20\u6570\u662f\u516c\u5f00\u7684\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u901a\u8fc7\u516c\u94a5\u63a8\u7b97\u79c1\u94a5\uff1f<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"629\" src=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-94-1024x629.png\" alt=\"\" class=\"wp-image-742\" srcset=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-94-1024x629.png 1024w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-94-300x184.png 300w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-94-768x471.png 768w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-94-670x411.png 670w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2024\/12\/image-94.png 1409w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-large-font-size\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">\u7a0b\u5e8f\u4e2d\u76f4\u63a5\u5c31\u662f\u4ea4\u6362\u516c\u94a5\uff0c\u5e76\u6ca1\u6709\u534f\u5546\u7d20\u6570\u548c\u6e90\u6839\u554a\uff0c\u5982\u4f55\u5b9e\u73b0\u7684\uff1f<\/mark><\/strong><\/p>\n\n\n\n<p>\u5728\u5b9e\u9645\u7684 Diffie-Hellman \u5bc6\u94a5\u4ea4\u6362\u8fc7\u7a0b\u4e2d\uff0c\u786e\u5b9e\u9700\u8981\u9884\u5148\u786e\u5b9a\u4e00\u7ec4\u53c2\u6570\uff0c\u5305\u62ec\u5927\u7d20\u6570 <em>p<\/em> \u548c\u539f\u6839 <em>g<\/em>\u3002\u8fd9\u4e9b\u53c2\u6570\u901a\u5e38\u662f\u516c\u5f00\u7684\uff0c\u5e76\u4e14\u5728\u4e0d\u540c\u7684\u5b9e\u4f53\u4e4b\u95f4\u5171\u4eab\u3002\u5728\u4f60\u7684 Java \u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u8fd9\u4e9b\u53c2\u6570\u5e76\u6ca1\u6709\u663e\u5f0f\u5730\u5b9a\u4e49\u548c\u4f20\u9012\uff0c\u800c\u662f\u7531 Java \u7684 <code>KeyPairGenerator<\/code> \u548c\u76f8\u5173\u5e93\u81ea\u52a8\u5904\u7406\u7684\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u89e3\u91ca\u8fd9\u4e9b\u53c2\u6570\u662f\u5982\u4f55\u5728\u4ee3\u7801\u4e2d\u9690\u5f0f\u5904\u7406\u7684\uff0c\u5e76\u4e14\u4e3a\u4ec0\u4e48\u5b83\u4eec\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u534f\u5546\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u9ed8\u8ba4\u53c2\u6570\u548c\u9884\u5b9a\u4e49\u53c2\u6570\u96c6<\/h3>\n\n\n\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cDiffie-Hellman \u5bc6\u94a5\u4ea4\u6362\u901a\u5e38\u4f7f\u7528\u9884\u5b9a\u4e49\u7684\u53c2\u6570\u96c6\u3002\u8fd9\u4e9b\u53c2\u6570\u96c6\u7ecf\u8fc7\u7cbe\u5fc3\u8bbe\u8ba1\uff0c\u786e\u4fdd\u4e86\u5b89\u5168\u6027\u548c\u6548\u7387\u3002Java \u7684 <code>KeyPairGenerator<\/code> \u5e93\u652f\u6301\u591a\u79cd\u9884\u5b9a\u4e49\u7684\u53c2\u6570\u96c6\uff0c\u8fd9\u4e9b\u53c2\u6570\u96c6\u5305\u542b\u4e86\u5927\u7d20\u6570 <em>p<\/em> \u548c\u539f\u6839 <em>g<\/em>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u9884\u5b9a\u4e49\u53c2\u6570\u96c6\u793a\u4f8b<\/h4>\n\n\n\n<p>Java \u63d0\u4f9b\u4e86\u4e00\u4e9b\u9884\u5b9a\u4e49\u7684 Diffie-Hellman \u53c2\u6570\u96c6\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<ul>\n<li><code>dh\/DH 1024 bit prime with 160 bit subprime<\/code><\/li>\n\n\n\n<li><code>dh\/DH 2048 bit prime with 224 bit subprime<\/code><\/li>\n\n\n\n<li><code>dh\/DH 2048 bit prime with 256 bit subprime<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e9b\u53c2\u6570\u96c6\u786e\u4fdd\u4e86\u5927\u7d20\u6570 <em>p<\/em> \u548c\u539f\u6839 <em>g<\/em> \u7684\u5b89\u5168\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Java \u4e2d\u7684\u81ea\u52a8\u5904\u7406<\/h3>\n\n\n\n<p>\u5728\u4f60\u7684 Java \u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c<code>KeyPairGenerator<\/code> \u4f1a\u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u9ed8\u8ba4\u53c2\u6570\u96c6\u6765\u751f\u6210\u5bc6\u94a5\u5bf9\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5f53\u4f60\u8c03\u7528 <code>KeyPairGenerator.getInstance(\"DH\")<\/code> \u5e76\u521d\u59cb\u5316\u65f6\uff0c<code>KeyPairGenerator<\/code> \u4f1a\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u53c2\u6570\u96c6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>KeyPairGenerator kpGen = KeyPairGenerator.getInstance(\"DH\");\r\nkpGen.initialize(512); \/\/ \u8fd9\u91cc\u7684 512 \u8868\u793a\u5bc6\u94a5\u957f\u5ea6\r\nKeyPair kp = kpGen.generateKeyPair();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8be6\u7ec6\u6b65\u9aa4<\/h4>\n\n\n\n<ol start=\"1\">\n<li><strong>\u9009\u62e9\u5bc6\u94a5\u957f\u5ea6 <\/strong>\uff1a\n<ul>\n<li>\u901a\u8fc7 <code>initialize(512)<\/code> \u65b9\u6cd5\u6307\u5b9a\u5bc6\u94a5\u957f\u5ea6\u3002Java \u4f1a\u6839\u636e\u8fd9\u4e2a\u957f\u5ea6\u9009\u62e9\u5408\u9002\u7684\u53c2\u6570\u96c6\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u751f\u6210\u5bc6\u94a5\u5bf9 <\/strong>\uff1a\n<ul>\n<li><code>KeyPairGenerator<\/code> \u4f7f\u7528\u9009\u5b9a\u7684\u53c2\u6570\u96c6\u751f\u6210\u516c\u94a5\u548c\u79c1\u94a5\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u53c2\u6570\u96c6\u7684\u9009\u62e9 <\/strong>\uff1a\n<ul>\n<li>Java \u5185\u90e8\u7ef4\u62a4\u4e86\u4e00\u7ec4\u9884\u5b9a\u4e49\u7684\u53c2\u6570\u96c6\uff0c\u6839\u636e\u6307\u5b9a\u7684\u5bc6\u94a5\u957f\u5ea6\u9009\u62e9\u5408\u9002\u7684\u53c2\u6570\u96c6\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u9690\u5f0f\u53c2\u6570\u96c6<\/h3>\n\n\n\n<p>\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\uff0c\u53c2\u6570\u96c6\u7684\u9009\u62e9\u662f\u9690\u5f0f\u7684\uff0c\u5177\u4f53\u6765\u8bf4\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u5bc6\u94a5\u957f\u5ea6 <\/strong>\uff1a<code>initialize(512)<\/code> \u6307\u5b9a\u4e86\u5bc6\u94a5\u957f\u5ea6\u4e3a 512 \u4f4d\u3002<\/li>\n\n\n\n<li><strong>\u53c2\u6570\u96c6 <\/strong>\uff1aJava \u6839\u636e\u5bc6\u94a5\u957f\u5ea6\u9009\u62e9\u5408\u9002\u7684\u53c2\u6570\u96c6\uff0c\u8fd9\u4e9b\u53c2\u6570\u96c6\u5305\u542b\u4e86\u5927\u7d20\u6570 <em>p<\/em> \u548c\u539f\u6839 <em>g<\/em>\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u5206\u6790<\/h4>\n\n\n\n<p>\u5047\u8bbe\u4f60\u8c03\u7528\u4e86 <code>initialize(512)<\/code>\uff0cJava \u4f1a\u81ea\u52a8\u9009\u62e9\u4e00\u4e2a\u9002\u5408\u7684 512 \u4f4d\u53c2\u6570\u96c6\u3002\u8fd9\u4e2a\u53c2\u6570\u96c6\u53ef\u80fd\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<ul>\n<li>\u5927\u7d20\u6570 ( p )\uff1a\u4e00\u4e2a 512 \u4f4d\u7684\u5927\u7d20\u6570\u3002<\/li>\n\n\n\n<li>\u539f\u6839 g \\)\uff1a\u4e00\u4e2a\u4e0e \\( p \u76f8\u5173\u7684\u539f\u6839\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e9b\u53c2\u6570\u96c6\u901a\u5e38\u662f\u7ecf\u8fc7\u4e25\u683c\u6d4b\u8bd5\u548c\u9a8c\u8bc1\u7684\uff0c\u786e\u4fdd\u4e86\u5b89\u5168\u6027\u548c\u6027\u80fd\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u81ea\u5b9a\u4e49\u53c2\u6570\u96c6\u5b9e\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import javax.crypto.KeyAgreement;\r\nimport java.security.*;\r\nimport java.security.spec.DHParameterSpec;\r\nimport java.security.spec.X509EncodedKeySpec;\r\nimport java.util.HexFormat;\r\n\r\npublic class KeyExchangeAlgorithmExample {\r\n    public static void main(String&#91;] args) {\r\n        \/\/ Bob\u548cAlice:\r\n        Person bob = new Person(\"Bob\");\r\n        Person alice = new Person(\"Alice\");\r\n\r\n        \/\/ \u5b9a\u4e49\u81ea\u5b9a\u4e49\u53c2\u6570\u96c6\r\n        int primeSize = 512;\r\n        int exponentSize = 160;\r\n        try {\r\n            AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance(\"DH\");\r\n            paramGen.init(primeSize, exponentSize, new SecureRandom());\r\n            AlgorithmParameters params = paramGen.generateParameters();\r\n            DHParameterSpec dhParams = params.getParameterSpec(DHParameterSpec.class);\r\n\r\n            \/\/ \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u53c2\u6570\u96c6\r\n            bob.setDHParameterSpec(dhParams);\r\n            alice.setDHParameterSpec(dhParams);\r\n        } catch (GeneralSecurityException e) {\r\n            throw new RuntimeException(e);\r\n        }\r\n\r\n        \/\/ \u5404\u81ea\u751f\u6210KeyPair:\r\n        bob.generateKeyPair();\r\n        alice.generateKeyPair();\r\n\r\n        \/\/ \u53cc\u65b9\u4ea4\u6362\u5404\u81ea\u7684PublicKey:\r\n        \/\/ Bob\u6839\u636eAlice\u7684PublicKey\u751f\u6210\u81ea\u5df1\u7684\u672c\u5730\u5bc6\u94a5:\r\n        bob.generateSecretKey(alice.publicKey.getEncoded());\r\n        \/\/ Alice\u6839\u636eBob\u7684PublicKey\u751f\u6210\u81ea\u5df1\u7684\u672c\u5730\u5bc6\u94a5:\r\n        alice.generateSecretKey(bob.publicKey.getEncoded());\r\n\r\n        \/\/ \u68c0\u67e5\u53cc\u65b9\u7684\u672c\u5730\u5bc6\u94a5\u662f\u5426\u76f8\u540c:\r\n        bob.printKeys();\r\n        alice.printKeys();\r\n    }\r\n}\r\n\r\nclass Person {\r\n    public final String name;\r\n\r\n    public PublicKey publicKey;\r\n    private PrivateKey privateKey;\r\n    private byte&#91;] secretKey;\r\n    private DHParameterSpec dhParamSpec;\r\n\r\n    public Person(String name) {\r\n        this.name = name;\r\n    }\r\n\r\n    public void setDHParameterSpec(DHParameterSpec dhParamSpec) {\r\n        this.dhParamSpec = dhParamSpec;\r\n    }\r\n\r\n    \/\/ \u751f\u6210\u672c\u5730KeyPair:\r\n    public void generateKeyPair() {\r\n        try {\r\n            KeyPairGenerator kpGen = KeyPairGenerator.getInstance(\"DH\");\r\n            kpGen.initialize(dhParamSpec);\r\n            KeyPair kp = kpGen.generateKeyPair();\r\n            this.privateKey = kp.getPrivate();\r\n            this.publicKey = kp.getPublic();\r\n        } catch (GeneralSecurityException e) {\r\n            throw new RuntimeException(e);\r\n        }\r\n    }\r\n\r\n    public void generateSecretKey(byte&#91;] receivedPubKeyBytes) {\r\n        try {\r\n            \/\/ \u4ecebyte&#91;]\u6062\u590dPublicKey:\r\n            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(receivedPubKeyBytes);\r\n            KeyFactory kf = KeyFactory.getInstance(\"DH\");\r\n            PublicKey receivedPublicKey = kf.generatePublic(keySpec);\r\n            \/\/ \u751f\u6210\u672c\u5730\u5bc6\u94a5:\r\n            KeyAgreement keyAgreement = KeyAgreement.getInstance(\"DH\");\r\n            keyAgreement.init(this.privateKey); \/\/ \u81ea\u5df1\u7684PrivateKey\r\n            keyAgreement.doPhase(receivedPublicKey, true); \/\/ \u5bf9\u65b9\u7684PublicKey\r\n            \/\/ \u751f\u6210SecretKey\u5bc6\u94a5:\r\n            this.secretKey = keyAgreement.generateSecret();\r\n        } catch (GeneralSecurityException e) {\r\n            throw new RuntimeException(e);\r\n        }\r\n    }\r\n\r\n    public void printKeys() {\r\n        System.out.println(\"Name: \" + this.name);\r\n        System.out.println(\"Private key: \" + HexFormat.of().formatHex(this.privateKey.getEncoded()));\r\n        System.out.println(\"Public key: \" + HexFormat.of().formatHex(this.publicKey.getEncoded()));\r\n        System.out.println(\"Secret key: \" + HexFormat.of().formatHex(this.secretKey));\r\n    }\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u770b\u770b\u4ee3\u7801\u5b9e\u73b0 \u8be6\u89e3\u4ee3\u7801\uff1a \u8be6&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":737,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[185,147,184],"_links":{"self":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts\/736"}],"collection":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=736"}],"version-history":[{"count":3,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts\/736\/revisions"}],"predecessor-version":[{"id":799,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts\/736\/revisions\/799"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/media\/737"}],"wp:attachment":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}