{"id":736,"date":"2023-10-11T09:01:01","date_gmt":"2023-10-11T01:01:01","guid":{"rendered":""},"modified":"2023-10-11T09:01:01","modified_gmt":"2023-10-11T01:01:01","slug":"\u5982\u4f55\u7528java RSA\u751f\u6210\u751f\u6210\u516c\u94a5\u79c1\u94a5\uff08\u975e\u5bf9\u79f0\u52a0\u5bc6\uff09","status":"publish","type":"post","link":"https:\/\/mushiming.com\/736.html","title":{"rendered":"\u5982\u4f55\u7528java RSA\u751f\u6210\u751f\u6210\u516c\u94a5\u79c1\u94a5\uff08\u975e\u5bf9\u79f0\u52a0\u5bc6\uff09"},"content":{"rendered":"<p>\u8a00\u7b80\u610f\u8d45\uff0c\u76f4\u63a5\u89c1\u4ee3\u7801\uff1a<\/p>\n<h3>\u5728\u7ebf\u52a0\u89e3\u5bc6\u5de5\u5177\uff1ahttps:\/\/hao.panziye.com\/sites\/331.html<\/h3>\n<pre class=\"has\"><code class=\"language-java\">package com;\n\n\nimport java.security.Key;\nimport java.security.KeyFactory;\nimport java.security.KeyPair;\nimport java.security.KeyPairGenerator;\nimport java.security.PrivateKey;\nimport java.security.PublicKey;\nimport java.security.SecureRandom;\nimport java.security.Signature;\nimport java.security.interfaces.RSAPrivateKey;\nimport java.security.interfaces.RSAPublicKey;\nimport java.security.spec.PKCS8EncodedKeySpec;\nimport java.security.spec.X509EncodedKeySpec;\nimport java.util.Date;\nimport java.util.HashMap;\nimport java.util.Map;\n\n\nimport javax.crypto.Cipher;\n\n\nimport sun.misc.BASE64Decoder;\nimport sun.misc.BASE64Encoder;\n\n\npublic class CreateSecretKey {\n&nbsp; &nbsp; public static final String KEY_ALGORITHM = \"RSA\";\n&nbsp; &nbsp; private static final String PUBLIC_KEY = \"RSAPublicKey\";\n&nbsp; &nbsp; private static final String PRIVATE_KEY = \"RSAPrivateKey\";\n&nbsp; &nbsp; public static final String SIGNATURE_ALGORITHM=\"MD5withRSA\";\n&nbsp; &nbsp;&nbsp;&nbsp;\/**\n&nbsp; &nbsp; &nbsp;* RSA\u6700\u5927\u52a0\u5bc6\u660e\u6587\u5927\u5c0f\n&nbsp; &nbsp; &nbsp;*\/\n&nbsp; &nbsp; private static final int MAX_ENCRYPT_BLOCK = 117;\n&nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; \/**\n&nbsp; &nbsp; &nbsp;* RSA\u6700\u5927\u89e3\u5bc6\u5bc6\u6587\u5927\u5c0f\n&nbsp; &nbsp; &nbsp;*\/\n&nbsp; &nbsp; private static final int MAX_DECRYPT_BLOCK = 128;\n&nbsp; &nbsp; \/\/\u83b7\u5f97\u516c\u94a5\u5b57\u7b26\u4e32\n&nbsp; &nbsp; public static String getPublicKeyStr(Map&lt;String, Object&gt; keyMap) throws Exception {\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/\u83b7\u5f97map\u4e2d\u7684\u516c\u94a5\u5bf9\u8c61 \u8f6c\u4e3akey\u5bf9\u8c61\n&nbsp; &nbsp; &nbsp; &nbsp; Key key = (Key) keyMap.get(PUBLIC_KEY);\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/\u7f16\u7801\u8fd4\u56de\u5b57\u7b26\u4e32\n&nbsp; &nbsp; &nbsp; &nbsp; return encryptBASE64(key.getEncoded());\n&nbsp; &nbsp; }\n\n\n&nbsp; &nbsp; \/\/\u83b7\u5f97\u79c1\u94a5\u5b57\u7b26\u4e32\n&nbsp; &nbsp; public static String getPrivateKeyStr(Map&lt;String, Object&gt; keyMap) throws Exception {\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/\u83b7\u5f97map\u4e2d\u7684\u79c1\u94a5\u5bf9\u8c61 \u8f6c\u4e3akey\u5bf9\u8c61\n&nbsp; &nbsp; &nbsp; &nbsp; Key key = (Key) keyMap.get(PRIVATE_KEY);\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/\u7f16\u7801\u8fd4\u56de\u5b57\u7b26\u4e32\n&nbsp; &nbsp; &nbsp; &nbsp; return encryptBASE64(key.getEncoded());\n&nbsp; &nbsp; }\n&nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; \/\/\u83b7\u53d6\u516c\u94a5\n&nbsp; &nbsp; public static PublicKey getPublicKey(String key) throws Exception {&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; byte[] keyBytes;&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; keyBytes = (new BASE64Decoder()).decodeBuffer(key);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; PublicKey publicKey = keyFactory.generatePublic(keySpec);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; return publicKey;&nbsp;&nbsp;\n&nbsp; &nbsp; }&nbsp;&nbsp;\n&nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; \/\/\u83b7\u53d6\u79c1\u94a5\n&nbsp; &nbsp; public static PrivateKey getPrivateKey(String key) throws Exception {&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; byte[] keyBytes;&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; keyBytes = (new BASE64Decoder()).decodeBuffer(key);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; PrivateKey privateKey = keyFactory.generatePrivate(keySpec);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; return privateKey;&nbsp;&nbsp;\n&nbsp; &nbsp; }\n&nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; \/\/\u89e3\u7801\u8fd4\u56debyte\n&nbsp; &nbsp; public static byte[] decryptBASE64(String key) throws Exception {\n&nbsp; &nbsp; &nbsp; &nbsp; return (new BASE64Decoder()).decodeBuffer(key);\n&nbsp; &nbsp; }\n\n\n&nbsp; &nbsp; \/\/\u7f16\u7801\u8fd4\u56de\u5b57\u7b26\u4e32\n&nbsp; &nbsp; public static String encryptBASE64(byte[] key) throws Exception {\n&nbsp; &nbsp; &nbsp; &nbsp; return (new BASE64Encoder()).encodeBuffer(key);\n&nbsp; &nbsp; }\n&nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; \/\/***************************\u7b7e\u540d\u548c\u9a8c\u8bc1*******************************&nbsp;&nbsp;\n&nbsp; &nbsp; public static byte[] sign(byte[] data,String privateKeyStr) throws Exception{&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; PrivateKey priK = getPrivateKey(privateKeyStr);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM);&nbsp; &nbsp; &nbsp; &nbsp;\n&nbsp; &nbsp; &nbsp; sig.initSign(priK);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; sig.update(data);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; return sig.sign();&nbsp;&nbsp;\n&nbsp; &nbsp; }&nbsp;&nbsp;\n&nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; public static boolean verify(byte[] data,byte[] sign,String publicKeyStr) throws Exception{&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; PublicKey pubK = getPublicKey(publicKeyStr);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; sig.initVerify(pubK);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; sig.update(data);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; return sig.verify(sign);&nbsp;&nbsp;\n&nbsp; &nbsp; }&nbsp;&nbsp;\n&nbsp; &nbsp;&nbsp;\n&nbsp; \/\/************************\u52a0\u5bc6\u89e3\u5bc6**************************&nbsp;&nbsp;\n&nbsp; &nbsp; public static byte[] encrypt(byte[] plainText,String publicKeyStr)throws Exception{&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; PublicKey publicKey = getPublicKey(publicKeyStr);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; cipher.init(Cipher.ENCRYPT_MODE, publicKey);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; int inputLen = plainText.length;\n&nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream out = new ByteArrayOutputStream();\n&nbsp; &nbsp; &nbsp; &nbsp; int offSet = 0;\n&nbsp; &nbsp; &nbsp; &nbsp; int i = 0;\n&nbsp; &nbsp; &nbsp; &nbsp; byte[] cache;\n&nbsp; &nbsp; &nbsp; &nbsp; while (inputLen - offSet &gt; 0) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (inputLen - offSet &gt; MAX_ENCRYPT_BLOCK) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache = cipher.doFinal(plainText, offSet, MAX_ENCRYPT_BLOCK);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache = cipher.doFinal(plainText, offSet, inputLen - offSet);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.write(cache, 0, cache.length);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offSet = i * MAX_ENCRYPT_BLOCK;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; byte[] encryptText = out.toByteArray();\n&nbsp; &nbsp; &nbsp; &nbsp; out.close();&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; return encryptText;&nbsp;&nbsp;\n&nbsp; &nbsp; }&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; public static byte[] decrypt(byte[] encryptText,String privateKeyStr)throws Exception{&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; PrivateKey privateKey = getPrivateKey(privateKeyStr);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; cipher.init(Cipher.DECRYPT_MODE, privateKey);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; int inputLen = encryptText.length;\n&nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream out = new ByteArrayOutputStream();\n&nbsp; &nbsp; &nbsp; &nbsp; int offSet = 0;\n&nbsp; &nbsp; &nbsp; &nbsp; byte[] cache;\n&nbsp; &nbsp; &nbsp; &nbsp; int i = 0;\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ \u5bf9\u6570\u636e\u5206\u6bb5\u89e3\u5bc6\n&nbsp; &nbsp; &nbsp; &nbsp; while (inputLen - offSet &gt; 0) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (inputLen - offSet &gt; MAX_DECRYPT_BLOCK) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache = cipher.doFinal(encryptText, offSet, MAX_DECRYPT_BLOCK);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache = cipher.doFinal(encryptText, offSet, inputLen - offSet);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.write(cache, 0, cache.length);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offSet = i * MAX_DECRYPT_BLOCK;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; byte[] plainText = out.toByteArray();\n&nbsp; &nbsp; &nbsp; &nbsp; out.close();&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; return plainText;&nbsp;&nbsp;\n&nbsp; &nbsp; }&nbsp;&nbsp;\n\n\n&nbsp; &nbsp; public static void main(String[] args) {\n&nbsp; &nbsp; &nbsp; &nbsp; Map&lt;String, Object&gt; keyMap;\n&nbsp; &nbsp; &nbsp; &nbsp; byte[] cipherText;\n&nbsp; &nbsp; &nbsp; &nbsp; String input = \"Hello World!\";\n&nbsp; &nbsp; &nbsp; &nbsp; try {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyMap = initKey();\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String publicKey = getPublicKeyStr(keyMap);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"\u516c\u94a5------------------\");\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(publicKey);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String privateKey = getPrivateKeyStr(keyMap);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"\u79c1\u94a5------------------\");\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(privateKey);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"\u6d4b\u8bd5\u53ef\u884c\u6027-------------------\");\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"\u660e\u6587=======\"+input);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cipherText = encrypt(input.getBytes(),publicKey);&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u52a0\u5bc6\u540e\u7684\u4e1c\u897f&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"\u5bc6\u6587=======\"+new String(cipherText));\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u5f00\u59cb\u89e3\u5bc6&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] plainText = decrypt(cipherText,privateKey);&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"\u89e3\u5bc6\u540e\u660e\u6587===== \" + new String(plainText));\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"\u9a8c\u8bc1\u7b7e\u540d-----------\");\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String str=\"\u88ab\u7b7e\u540d\u7684\u5185\u5bb9\";&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"\\n\u539f\u6587:\"+str);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] signature=sign(str.getBytes(),privateKey);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean status=verify(str.getBytes(), signature,publicKey);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"\u9a8c\u8bc1\u60c5\u51b5\uff1a\"+status);&nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; }\n\n\n}<\/code><\/pre>\n<p>AES\/DES\u7b49\u5bf9\u79f0\u52a0\u5bc6\/\u89e3\u5bc6 | \u7a0b\u5e8f\u5458\u5bfc\u822a\u7f51&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528java RSA\u751f\u6210\u751f\u6210\u516c\u94a5\u79c1\u94a5\uff08\u975e\u5bf9\u79f0\u52a0\u5bc6\uff09\u8a00\u7b80\u610f\u8d45\uff0c\u76f4\u63a5\u89c1\u4ee3\u7801\uff1apackagecom;importjava.security.Key;importjava.secur...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/736"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=736"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/736\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}