{"id":215,"date":"2023-03-13T08:29:14","date_gmt":"2023-03-13T08:29:14","guid":{"rendered":"https:\/\/chenyl.fun\/?post_type=docs&#038;p=215"},"modified":"2023-03-13T08:29:14","modified_gmt":"2023-03-13T08:29:14","slug":"ras%e9%9d%9e%e5%af%b9%e7%a7%b0%e5%8a%a0%e5%af%86java%e5%ae%9e%e7%8e%b0demo","status":"publish","type":"docs","link":"https:\/\/chenyl.fun\/?docs=ras%e9%9d%9e%e5%af%b9%e7%a7%b0%e5%8a%a0%e5%af%86java%e5%ae%9e%e7%8e%b0demo","title":{"rendered":"RAS\u975e\u5bf9\u79f0\u52a0\u5bc6java\u5b9e\u73b0demo"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.mine.lea.encrypt;\r\n\r\nimport lombok.SneakyThrows;\r\nimport org.apache.commons.codec.binary.Base64;\r\n\r\nimport javax.crypto.Cipher;\r\nimport java.io.FileReader;\r\nimport java.nio.charset.StandardCharsets;\r\nimport java.security.*;\r\nimport java.security.interfaces.RSAPrivateKey;\r\nimport java.security.interfaces.RSAPublicKey;\r\nimport java.security.spec.PKCS8EncodedKeySpec;\r\nimport java.security.spec.X509EncodedKeySpec;\r\n\r\npublic class EncryptMain {\r\n\r\n\r\n    @SneakyThrows\r\n    public static void main(String[] args) {\r\n\r\n        KeyPairGenerator pairGenerator = KeyPairGenerator.getInstance(\"RSA\");\r\n        pairGenerator.initialize(512,new SecureRandom());\r\n        KeyPair keyPair = pairGenerator.generateKeyPair();\r\n        RSAPrivateKey aPrivate = (RSAPrivateKey)keyPair.getPrivate();\r\n        RSAPublicKey aPublic = (RSAPublicKey)keyPair.getPublic();\r\n        String privateKey = Base64.encodeBase64URLSafeString(aPrivate.getEncoded());\r\n        System.out.println(\"privateKey:\"+privateKey);\r\n        System.out.println();\r\n        String publicKey = Base64.encodeBase64URLSafeString(aPublic.getEncoded());\r\n        System.out.println(\"publicKey:\"+publicKey);\r\n        System.out.println();\r\n\r\n        PublicKey rsaPublicKey = KeyFactory.getInstance(\"RSA\")\r\n                .generatePublic(new X509EncodedKeySpec(Base64.decodeBase64(publicKey)));\r\n        Cipher cipher = Cipher.getInstance(\"RSA\");\r\n        cipher.init(Cipher.ENCRYPT_MODE,rsaPublicKey);\r\n        String toencStr = \"yyyyyyyyyyyyyyyyy\";\/\/getFileContent();\r\n        String encryptStr = Base64.encodeBase64URLSafeString(cipher.doFinal(toencStr.getBytes(StandardCharsets.UTF_8)));\r\n        System.out.println(\"encryptStr:\"+encryptStr);\r\n        System.out.println();\r\n\r\n\r\n        PrivateKey rsaPrivateKey = KeyFactory.getInstance(\"RSA\")\r\n                .generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey)));\r\n        Cipher acipher = Cipher.getInstance(\"RSA\");\r\n        acipher.init(Cipher.DECRYPT_MODE,rsaPrivateKey);\r\n        byte[] bytes = acipher.doFinal(Base64.decodeBase64(encryptStr));\r\n        System.out.println(\"decoded:\"+new String(bytes));\r\n\r\n\r\n    }\r\n\r\n    @SneakyThrows\r\n    public static String getFileContent() {\r\n        FileReader fileReader =\r\n                new FileReader(\"D:\/tmp\/myfile.txt\");\r\n        char[] buf = new char[1024];\r\n        StringBuilder builder = new StringBuilder();\r\n        int read = 0;\r\n        while (read != -1){\r\n            read = fileReader.read(buf);\r\n            builder.append(buf);\r\n            buf = new char[1024];\r\n        }\r\n        return builder.toString();\r\n    }\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"doc_category":[24],"doc_tag":[],"_links":{"self":[{"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/docs\/215"}],"collection":[{"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chenyl.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215"}],"version-history":[{"count":1,"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/docs\/215\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/docs\/215\/revisions\/216"}],"wp:attachment":[{"href":"https:\/\/chenyl.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/chenyl.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fdoc_category&post=215"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/chenyl.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fdoc_tag&post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}