{"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":"
\u8a00\u7b80\u610f\u8d45\uff0c\u76f4\u63a5\u89c1\u4ee3\u7801\uff1a<\/p>\n
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 public static final String KEY_ALGORITHM = \"RSA\";\n private static final String PUBLIC_KEY = \"RSAPublicKey\";\n private static final String PRIVATE_KEY = \"RSAPrivateKey\";\n public static final String SIGNATURE_ALGORITHM=\"MD5withRSA\";\n \/**\n * RSA\u6700\u5927\u52a0\u5bc6\u660e\u6587\u5927\u5c0f\n *\/\n private static final int MAX_ENCRYPT_BLOCK = 117;\n \n \/**\n * RSA\u6700\u5927\u89e3\u5bc6\u5bc6\u6587\u5927\u5c0f\n *\/\n private static final int MAX_DECRYPT_BLOCK = 128;\n \/\/\u83b7\u5f97\u516c\u94a5\u5b57\u7b26\u4e32\n public static String getPublicKeyStr(Map<String, Object> keyMap) throws Exception {\n \/\/\u83b7\u5f97map\u4e2d\u7684\u516c\u94a5\u5bf9\u8c61 \u8f6c\u4e3akey\u5bf9\u8c61\n Key key = (Key) keyMap.get(PUBLIC_KEY);\n \/\/\u7f16\u7801\u8fd4\u56de\u5b57\u7b26\u4e32\n return encryptBASE64(key.getEncoded());\n }\n\n\n \/\/\u83b7\u5f97\u79c1\u94a5\u5b57\u7b26\u4e32\n public static String getPrivateKeyStr(Map<String, Object> keyMap) throws Exception {\n \/\/\u83b7\u5f97map\u4e2d\u7684\u79c1\u94a5\u5bf9\u8c61 \u8f6c\u4e3akey\u5bf9\u8c61\n Key key = (Key) keyMap.get(PRIVATE_KEY);\n \/\/\u7f16\u7801\u8fd4\u56de\u5b57\u7b26\u4e32\n return encryptBASE64(key.getEncoded());\n }\n \n \/\/\u83b7\u53d6\u516c\u94a5\n public static PublicKey getPublicKey(String key) throws Exception { \n byte[] keyBytes; \n keyBytes = (new BASE64Decoder()).decodeBuffer(key); \n X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); \n KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); \n PublicKey publicKey = keyFactory.generatePublic(keySpec); \n return publicKey; \n } \n \n \/\/\u83b7\u53d6\u79c1\u94a5\n public static PrivateKey getPrivateKey(String key) throws Exception { \n byte[] keyBytes; \n keyBytes = (new BASE64Decoder()).decodeBuffer(key); \n PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); \n KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); \n PrivateKey privateKey = keyFactory.generatePrivate(keySpec); \n return privateKey; \n }\n \n \/\/\u89e3\u7801\u8fd4\u56debyte\n public static byte[] decryptBASE64(String key) throws Exception {\n return (new BASE64Decoder()).decodeBuffer(key);\n }\n\n\n \/\/\u7f16\u7801\u8fd4\u56de\u5b57\u7b26\u4e32\n public static String encryptBASE64(byte[] key) throws Exception {\n return (new BASE64Encoder()).encodeBuffer(key);\n }\n \n \/\/***************************\u7b7e\u540d\u548c\u9a8c\u8bc1******************************* \n public static byte[] sign(byte[] data,String privateKeyStr) throws Exception{ \n PrivateKey priK = getPrivateKey(privateKeyStr); \n Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM); \n sig.initSign(priK); \n sig.update(data); \n return sig.sign(); \n } \n \n public static boolean verify(byte[] data,byte[] sign,String publicKeyStr) throws Exception{ \n PublicKey pubK = getPublicKey(publicKeyStr); \n Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM); \n sig.initVerify(pubK); \n sig.update(data); \n return sig.verify(sign); \n } \n \n \/\/************************\u52a0\u5bc6\u89e3\u5bc6************************** \n public static byte[] encrypt(byte[] plainText,String publicKeyStr)throws Exception{ \n PublicKey publicKey = getPublicKey(publicKeyStr); \n Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); \n cipher.init(Cipher.ENCRYPT_MODE, publicKey); \n int inputLen = plainText.length;\n ByteArrayOutputStream out = new ByteArrayOutputStream();\n int offSet = 0;\n int i = 0;\n byte[] cache;\n while (inputLen - offSet > 0) {\n if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {\n cache = cipher.doFinal(plainText, offSet, MAX_ENCRYPT_BLOCK);\n } else {\n cache = cipher.doFinal(plainText, offSet, inputLen - offSet);\n }\n out.write(cache, 0, cache.length);\n i++;\n offSet = i * MAX_ENCRYPT_BLOCK;\n }\n byte[] encryptText = out.toByteArray();\n out.close(); \n return encryptText; \n } \n \n public static byte[] decrypt(byte[] encryptText,String privateKeyStr)throws Exception{ \n PrivateKey privateKey = getPrivateKey(privateKeyStr); \n Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); \n cipher.init(Cipher.DECRYPT_MODE, privateKey); \n int inputLen = encryptText.length;\n ByteArrayOutputStream out = new ByteArrayOutputStream();\n int offSet = 0;\n byte[] cache;\n int i = 0;\n \/\/ \u5bf9\u6570\u636e\u5206\u6bb5\u89e3\u5bc6\n while (inputLen - offSet > 0) {\n if (inputLen - offSet > MAX_DECRYPT_BLOCK) {\n cache = cipher.doFinal(encryptText, offSet, MAX_DECRYPT_BLOCK);\n } else {\n cache = cipher.doFinal(encryptText, offSet, inputLen - offSet);\n }\n out.write(cache, 0, cache.length);\n i++;\n offSet = i * MAX_DECRYPT_BLOCK;\n }\n byte[] plainText = out.toByteArray();\n out.close(); \n return plainText; \n } \n\n\n public static void main(String[] args) {\n Map<String, Object> keyMap;\n byte[] cipherText;\n String input = \"Hello World!\";\n try {\n keyMap = initKey();\n String publicKey = getPublicKeyStr(keyMap);\n System.out.println(\"\u516c\u94a5------------------\");\n System.out.println(publicKey);\n String privateKey = getPrivateKeyStr(keyMap);\n System.out.println(\"\u79c1\u94a5------------------\");\n System.out.println(privateKey);\n \n System.out.println(\"\u6d4b\u8bd5\u53ef\u884c\u6027-------------------\");\n System.out.println(\"\u660e\u6587=======\"+input);\n \n cipherText = encrypt(input.getBytes(),publicKey); \n \/\/\u52a0\u5bc6\u540e\u7684\u4e1c\u897f \n System.out.println(\"\u5bc6\u6587=======\"+new String(cipherText));\n \/\/\u5f00\u59cb\u89e3\u5bc6 \n byte[] plainText = decrypt(cipherText,privateKey); \n System.out.println(\"\u89e3\u5bc6\u540e\u660e\u6587===== \" + new String(plainText));\n System.out.println(\"\u9a8c\u8bc1\u7b7e\u540d-----------\");\n \n String str=\"\u88ab\u7b7e\u540d\u7684\u5185\u5bb9\"; \n System.out.println(\"\\n\u539f\u6587:\"+str); \n byte[] signature=sign(str.getBytes(),privateKey); \n boolean status=verify(str.getBytes(), signature,publicKey); \n System.out.println(\"\u9a8c\u8bc1\u60c5\u51b5\uff1a\"+status); \n } catch (Exception e) {\n e.printStackTrace();\n }\n }\n\n\n}<\/code><\/pre>\nAES\/DES\u7b49\u5bf9\u79f0\u52a0\u5bc6\/\u89e3\u5bc6 | \u7a0b\u5e8f\u5458\u5bfc\u822a\u7f51 <\/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}]}}