{"id":186738,"date":"2024-11-11T08:25:26","date_gmt":"2024-11-11T00:25:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/186738\/"},"modified":"2024-11-11T08:25:27","modified_gmt":"2024-11-11T00:25:27","slug":"%e6%95%b8%e6%93%9a%e5%ba%ab%e5%8a%a0%e5%af%86%e6%a8%a1%e5%bc%8f%e8%88%87%e5%ba%8f%e5%88%97%e5%8c%96%e5%ae%89%e6%8e%92-%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%8a%a0%e5%af%86%e6%a8%a1%e5%bc%8f%e5%92%8c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/186738\/","title":{"rendered":"\u6578\u64da\u5eab\u52a0\u5bc6\u6a21\u5f0f\u8207\u5e8f\u5217\u5316\u5b89\u6392 (\u6578\u64da\u5eab\u7684\u52a0\u5bc6\u6a21\u5f0f\u548c\u9806\u5e8f)"},"content":{"rendered":"<h1 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e5%8a%a0%e5%af%86%e6%a8%a1%e5%bc%8f%e8%88%87%e5%ba%8f%e5%88%97%e5%8c%96%e5%ae%89%e6%8e%92-%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%8a%a0%e5%af%86%e6%a8%a1%e5%bc%8f%e5%92%8c-PRenSCJNvU\">\u6578\u64da\u5eab\u52a0\u5bc6\u6a21\u5f0f\u8207\u5e8f\u5217\u5316\u5b89\u6392 (\u6578\u64da\u5eab\u7684\u52a0\u5bc6\u6a21\u5f0f\u548c\u9806\u5e8f)<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u5b89\u5168\u6027\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u96a8\u8457\u6578\u64da\u6d29\u9732\u4e8b\u4ef6\u7684\u983b\u7e41\u767c\u751f\uff0c\u4f01\u696d\u548c\u500b\u4eba\u90fd\u958b\u59cb\u91cd\u8996\u6578\u64da\u7684\u4fdd\u8b77\u3002\u6578\u64da\u5eab\u52a0\u5bc6\u6a21\u5f0f\u548c\u5e8f\u5217\u5316\u5b89\u6392\u662f\u78ba\u4fdd\u6578\u64da\u5b89\u5168\u7684\u5169\u500b\u95dc\u9375\u65b9\u9762\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u9019\u4e9b\u6982\u5ff5\u53ca\u5176\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\u7684\u61c9\u7528\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e5%8a%a0%e5%af%86%e6%a8%a1%e5%bc%8f-PRenSCJNvU\">\u6578\u64da\u5eab\u52a0\u5bc6\u6a21\u5f0f<\/h2>\n<p>\u6578\u64da\u5eab\u52a0\u5bc6\u662f\u6307\u5c07\u6578\u64da\u8f49\u63db\u70ba\u4e0d\u53ef\u8b80\u7684\u683c\u5f0f\uff0c\u4ee5\u9632\u6b62\u672a\u7d93\u6388\u6b0a\u7684\u8a2a\u554f\u3002\u52a0\u5bc6\u6a21\u5f0f\u662f\u6307\u5728\u52a0\u5bc6\u904e\u7a0b\u4e2d\u4f7f\u7528\u7684\u7b97\u6cd5\u548c\u6280\u8853\u3002\u5e38\u898b\u7684\u6578\u64da\u5eab\u52a0\u5bc6\u6a21\u5f0f\u5305\u62ec\u5c0d\u7a31\u52a0\u5bc6\u548c\u975e\u5c0d\u7a31\u52a0\u5bc6\u3002<\/p>\n<h3 id=\"%e5%b0%8d%e7%a8%b1%e5%8a%a0%e5%af%86-PRenSCJNvU\">\u5c0d\u7a31\u52a0\u5bc6<\/h3>\n<p>\u5c0d\u7a31\u52a0\u5bc6\u662f\u6307\u52a0\u5bc6\u548c\u89e3\u5bc6\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u9470\u3002\u9019\u7a2e\u65b9\u6cd5\u7684\u512a\u9ede\u662f\u901f\u5ea6\u5feb\uff0c\u9069\u5408\u8655\u7406\u5927\u91cf\u6578\u64da\u3002\u5e38\u898b\u7684\u5c0d\u7a31\u52a0\u5bc6\u7b97\u6cd5\u5305\u62ec AES\uff08\u9ad8\u7d1a\u52a0\u5bc6\u6a19\u6e96\uff09\u548c DES\uff08\u6578\u64da\u52a0\u5bc6\u6a19\u6e96\uff09\u3002<\/p>\n<pre><code>import javax.crypto.Cipher;\nimport javax.crypto.KeyGenerator;\nimport javax.crypto.SecretKey;\n\npublic class SymmetricEncryption {\n    public static void main(String[] args) throws Exception {\n        KeyGenerator keyGen = KeyGenerator.getInstance(\"AES\");\n        SecretKey secretKey = keyGen.generateKey();\n        \n        Cipher cipher = Cipher.getInstance(\"AES\");\n        cipher.init(Cipher.ENCRYPT_MODE, secretKey);\n        \n        byte[] encryptedData = cipher.doFinal(\"Hello, World!\".getBytes());\n        System.out.println(\"Encrypted Data: \" + new String(encryptedData));\n    }\n}\n<\/code><\/pre>\n<h3 id=\"%e9%9d%9e%e5%b0%8d%e7%a8%b1%e5%8a%a0%e5%af%86-PRenSCJNvU\">\u975e\u5c0d\u7a31\u52a0\u5bc6<\/h3>\n<p>\u975e\u5c0d\u7a31\u52a0\u5bc6\u4f7f\u7528\u4e00\u5c0d\u5bc6\u9470\uff1a\u516c\u9470\u548c\u79c1\u9470\u3002\u516c\u9470\u7528\u65bc\u52a0\u5bc6\u6578\u64da\uff0c\u800c\u79c1\u9470\u5247\u7528\u65bc\u89e3\u5bc6\u3002\u9019\u7a2e\u65b9\u6cd5\u7684\u5b89\u5168\u6027\u8f03\u9ad8\uff0c\u4f46\u901f\u5ea6\u8f03\u6162\u3002\u5e38\u898b\u7684\u975e\u5c0d\u7a31\u52a0\u5bc6\u7b97\u6cd5\u5305\u62ec RSA \u548c ECC\uff08\u6a62\u5713\u66f2\u7dda\u52a0\u5bc6\uff09\u3002<\/p>\n<pre><code>import java.security.KeyPair;\nimport java.security.KeyPairGenerator;\nimport java.security.PublicKey;\nimport javax.crypto.Cipher;\n\npublic class AsymmetricEncryption {\n    public static void main(String[] args) throws Exception {\n        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(\"RSA\");\n        KeyPair keyPair = keyGen.generateKeyPair();\n        PublicKey publicKey = keyPair.getPublic();\n        \n        Cipher cipher = Cipher.getInstance(\"RSA\");\n        cipher.init(Cipher.ENCRYPT_MODE, publicKey);\n        \n        byte[] encryptedData = cipher.doFinal(\"Hello, World!\".getBytes());\n        System.out.println(\"Encrypted Data: \" + new String(encryptedData));\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e5%ba%8f%e5%88%97%e5%8c%96%e5%ae%89%e6%8e%92-PRenSCJNvU\">\u5e8f\u5217\u5316\u5b89\u6392<\/h2>\n<p>\u5e8f\u5217\u5316\u662f\u5c07\u5c0d\u8c61\u8f49\u63db\u70ba\u53ef\u5b58\u5132\u6216\u50b3\u8f38\u7684\u683c\u5f0f\u7684\u904e\u7a0b\u3002\u5728\u6578\u64da\u5eab\u4e2d\uff0c\u5e8f\u5217\u5316\u901a\u5e38\u7528\u65bc\u5c07\u6578\u64da\u5beb\u5165\u78c1\u789f\u6216\u901a\u904e\u7db2\u7d61\u50b3\u8f38\u3002\u5e8f\u5217\u5316\u7684\u5b89\u6392\u5c0d\u65bc\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u5b8c\u6574\u6027\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h3 id=\"%e5%ba%8f%e5%88%97%e5%8c%96%e7%9a%84%e5%ae%89%e5%85%a8%e6%80%a7-PRenSCJNvU\">\u5e8f\u5217\u5316\u7684\u5b89\u5168\u6027<\/h3>\n<p>\u5728\u5e8f\u5217\u5316\u904e\u7a0b\u4e2d\uff0c\u6578\u64da\u53ef\u80fd\u6703\u88ab\u672a\u7d93\u6388\u6b0a\u7684\u7528\u6236\u8a2a\u554f\u3002\u56e0\u6b64\uff0c\u5c0d\u5e8f\u5217\u5316\u6578\u64da\u9032\u884c\u52a0\u5bc6\u662f\u4fdd\u8b77\u6578\u64da\u7684\u4e00\u7a2e\u6709\u6548\u65b9\u6cd5\u3002\u9019\u53ef\u4ee5\u901a\u904e\u5728\u5e8f\u5217\u5316\u4e4b\u524d\u5c0d\u6578\u64da\u9032\u884c\u52a0\u5bc6\u4f86\u5be6\u73fe\u3002<\/p>\n<pre><code>import java.io.*;\nimport javax.crypto.Cipher;\nimport javax.crypto.SecretKey;\n\npublic class SecureSerialization {\n    public static void main(String[] args) throws Exception {\n        SecretKey secretKey = ...; \/\/ Assume this is initialized\n        Cipher cipher = Cipher.getInstance(\"AES\");\n        cipher.init(Cipher.ENCRYPT_MODE, secretKey);\n        \n        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();\n        ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);\n        objectStream.writeObject(\"Hello, World!\");\n        \n        byte[] serializedData = byteStream.toByteArray();\n        byte[] encryptedData = cipher.doFinal(serializedData);\n        System.out.println(\"Encrypted Serialized Data: \" + new String(encryptedData));\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-PRenSCJNvU\">\u7d50\u8ad6<\/h2>\n<p>\u6578\u64da\u5eab\u52a0\u5bc6\u6a21\u5f0f\u548c\u5e8f\u5217\u5316\u5b89\u6392\u662f\u78ba\u4fdd\u6578\u64da\u5b89\u5168\u7684\u91cd\u8981\u7d44\u6210\u90e8\u5206\u3002\u901a\u904e\u9078\u64c7\u5408\u9069\u7684\u52a0\u5bc6\u7b97\u6cd5\u548c\u5b89\u5168\u7684\u5e8f\u5217\u5316\u65b9\u6cd5\uff0c\u4f01\u696d\u548c\u500b\u4eba\u53ef\u4ee5\u6709\u6548\u5730\u4fdd\u8b77\u5176\u6578\u64da\u514d\u53d7\u672a\u7d93\u6388\u6b0a\u7684\u8a2a\u554f\u548c\u6d29\u9732\u3002\u5728\u9078\u64c7\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u6642\uff0c\u8003\u616e\u9019\u4e9b\u56e0\u7d20\u5c07\u6709\u52a9\u65bc\u63d0\u9ad8\u6574\u9ad4\u5b89\u5168\u6027\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c\u6578\u64da\u5eab\u5b89\u5168\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u6578\u64da\u5eab\u52a0\u5bc6\u6a21\u5f0f\u8207\u5e8f\u5217\u5316\u5b89\u6392\uff0c\u4e86\u89e3\u5982\u4f55\u4fdd\u8b77\u6578\u64da\u5b89\u5168\u53ca\u5176\u5728\u6578\u64da\u5eab\u4e2d\u7684\u6709\u6548\u7ba1\u7406\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-186738","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/186738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=186738"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/186738\/revisions"}],"predecessor-version":[{"id":186739,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/186738\/revisions\/186739"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=186738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=186738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=186738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}