{"id":332,"date":"2023-02-25T06:45:13","date_gmt":"2023-02-24T22:45:13","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=332"},"modified":"2023-04-30T14:51:17","modified_gmt":"2023-04-30T06:51:17","slug":"detailed-explanation-of-java-rsa-signature-and-unsigning-private-key-encryption-public-key-decryption-and-public-key-encryption-private-key-decryption","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/detailed-explanation-of-java-rsa-signature-and-unsigning-private-key-encryption-public-key-decryption-and-public-key-encryption-private-key-decryption\/","title":{"rendered":"\u5173\u4e8eJava\u4e2dRSA\u52a0\u7b7e\u89e3\u7b7e\uff0c\u79c1\u94a5\u52a0\u5bc6\u516c\u94a5\u89e3\u5bc6\u548c\u516c\u94a5\u52a0\u5bc6\u79c1\u94a5\u89e3\u5bc6\u4ee3\u7801\u8be6\u89e3"},"content":{"rendered":"<p>\u5173\u4e8eJava\u4e2dRSA\u52a0\u7b7e\u89e3\u7b7e\uff0c\u79c1\u94a5\u52a0\u5bc6\u516c\u94a5\u89e3\u5bc6\u548c\u516c\u94a5\u52a0\u5bc6\u79c1\u94a5\u89e3\u5bc6\uff0c\u4e00\u822c\u4e3a\u4e86\u5b89\u5168\u91c7\u7528\u7684\u662f<\/p>\n<ul>\n<li>\u79c1\u94a5\u52a0\u7b7e\uff0c\u516c\u94a5\u9a8c\u7b7e<\/li>\n<li>\u672c\u5730\u79c1\u94a5\u52a0\u5bc6\uff0c\u8fdc\u7a0b\u516c\u94a5\u89e3\u5bc6\uff08\u516c\u94a5\u53ef\u4ee5\u7528Base64\u8f6c\u6362\u540e\u516c\u5f00\uff09<\/li>\n<li>\u8fdc\u7a0b\u516c\u94a5\u52a0\u5bc6\uff0c\u672c\u5730\u79c1\u94a5\u89e3\u5bc6<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">import org.apache.commons.codec.binary.Base64;\n\nimport javax.crypto.Cipher;\nimport java.security.*;\nimport java.security.spec.PKCS8EncodedKeySpec;\nimport java.security.spec.X509EncodedKeySpec;\nimport java.util.Arrays;\n\n\/**\n * &lt;p&gt;\n * \u5c01\u88c5\u540cRSA\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u6709\u5173\u7684\u65b9\u6cd5\uff0c\u53ef\u7528\u4e8e\u6570\u5b57\u7b7e\u540d\uff0cRSA\u52a0\u5bc6\u89e3\u5bc6\n * &lt;\/p&gt;\n *\n * @Copyright:WDSsoft\n *\/\n\npublic class RSAUtil {\n\n    \/**\n     * \u4f7f\u7528\u79c1\u94a5\u52a0\u5bc6\u6570\u636e\n     * \u7528\u4e00\u4e2a\u5df2\u6253\u5305\u6210byte[]\u5f62\u5f0f\u7684\u79c1\u94a5\u52a0\u5bc6\u6570\u636e\uff0c\u5373\u6570\u5b57\u7b7e\u540d\n     *\n     * @param keyInByte \u6253\u5305\u6210byte[]\u7684\u79c1\u94a5\n     * @param source    \u8981\u7b7e\u540d\u7684\u6570\u636e\uff0c\u4e00\u822c\u5e94\u662f\u6570\u5b57\u6458\u8981\n     * @return \u7b7e\u540d byte[]\n     *\/\n    public static byte[] sign(byte[] keyInByte, byte[] source) {\n        try {\n            PKCS8EncodedKeySpec priv_spec = new PKCS8EncodedKeySpec(keyInByte);\n            KeyFactory mykeyFactory = KeyFactory.getInstance(&quot;RSA&quot;);\n            PrivateKey privKey = mykeyFactory.generatePrivate(priv_spec);\n            Signature sig = Signature.getInstance(&quot;SHA1withRSA&quot;);\n            sig.initSign(privKey);\n            sig.update(source);\n            return sig.sign();\n        } catch (Exception e) {\n            return null;\n        }\n    }\n\n    \/**\n     * \u9a8c\u8bc1\u6570\u5b57\u7b7e\u540d\n     *\n     * @param keyInByte \u6253\u5305\u6210byte[]\u5f62\u5f0f\u7684\u516c\u94a5\n     * @param source    \u539f\u6587\u7684\u6570\u5b57\u6458\u8981\n     * @param sign      \u7b7e\u540d\uff08\u5bf9\u539f\u6587\u7684\u6570\u5b57\u6458\u8981\u7684\u7b7e\u540d\uff09\n     * @return \u662f\u5426\u8bc1\u5b9e boolean\n     *\/\n    public static boolean verify(byte[] keyInByte, byte[] source, byte[] sign) {\n        try {\n            KeyFactory mykeyFactory = KeyFactory.getInstance(&quot;RSA&quot;);\n            Signature sig = Signature.getInstance(&quot;SHA1withRSA&quot;);\n            X509EncodedKeySpec pub_spec = new X509EncodedKeySpec(keyInByte);\n            PublicKey pubKey = mykeyFactory.generatePublic(pub_spec);\n            sig.initVerify(pubKey);\n            sig.update(source);\n            return sig.verify(sign);\n        } catch (Exception e) {\n            return false;\n        }\n    }\n\n    \/**\n     * \u5efa\u7acb\u65b0\u7684\u5bc6\u94a5\u5bf9\uff0c\u8fd4\u56de\u6253\u5305\u7684byte[]\u5f62\u5f0f\u79c1\u94a5\u548c\u516c\u94a5\n     *\n     * @return \u5305\u542b\u6253\u5305\u6210byte[]\u5f62\u5f0f\u7684\u79c1\u94a5\u548c\u516c\u94a5\u7684object[]\uff0c\u5176\u4e2d\uff0cobject[0]\u4e3a\u79c1\u94a5byte[]\uff0cobject[1]\u4e3a\u516c\u94a5byte[]\n     *\/\n    public static Object[] giveRSAKeyPairInByte() {\n        KeyPair keyPair = creatKey();\n        if (keyPair == null) {\n            return null;\n        }\n        Object[] re = new Object[2];\n        if (keyPair != null) {\n            PrivateKey pri = keyPair.getPrivate();\n            byte[] priBytes = pri.getEncoded();\n            PublicKey pub = keyPair.getPublic();\n            byte[] pubBytes = pub.getEncoded();\n            re[0] = priBytes;\n            re[1] = pubBytes;\n            return re;\n        }\n        return null;\n    }\n\n    \/**\n     * \u65b0\u5efa\u5bc6\u94a5\u5bf9\n     *\n     * @return KeyPair\u5bf9\u8c61\n     *\/\n    public static KeyPair creatKey() {\n        KeyPair keyPair;\n        long mySeed;\n        mySeed = System.currentTimeMillis();\n        try {\n            KeyPairGenerator keyGen = KeyPairGenerator.getInstance(&quot;RSA&quot;);\n            SecureRandom random = SecureRandom.getInstance(&quot;SHA1PRNG&quot;, &quot;SUN&quot;);\n            random.setSeed(mySeed);\n            keyGen.initialize(1024, random);\n            keyPair = keyGen.generateKeyPair();\n        } catch (Exception e1) {\n            return null;\n        }\n        return keyPair;\n    }\n\n    \/**\n     * \u4f7f\u7528RSA\u516c\u94a5\u52a0\u5bc6\u6570\u636e\n     *\n     * @param pubKeyInByte \u6253\u5305\u7684byte[]\u5f62\u5f0f\u516c\u94a5\n     * @param data         \u8981\u52a0\u5bc6\u7684\u6570\u636e\n     * @return \u52a0\u5bc6\u6570\u636e\n     *\/\n    public static byte[] encryptByPublicKey(byte[] pubKeyInByte, byte[] data) {\n        try {\n            KeyFactory mykeyFactory = KeyFactory.getInstance(&quot;RSA&quot;);\n            X509EncodedKeySpec pub_spec = new X509EncodedKeySpec(pubKeyInByte);\n            PublicKey pubKey = mykeyFactory.generatePublic(pub_spec);\n            Cipher cipher = Cipher.getInstance(&quot;RSA\/ECB\/PKCS1Padding&quot;);\n            cipher.init(Cipher.ENCRYPT_MODE, pubKey);\n            return cipher.doFinal(data);\n        } catch (Exception e) {\n            return null;\n        }\n    }\n\n    \/**\n     * \u7528RSA\u79c1\u94a5\u89e3\u5bc6\n     *\n     * @param privKeyInByte \u79c1\u94a5\u6253\u5305\u6210byte[]\u5f62\u5f0f\n     * @param data          \u8981\u89e3\u5bc6\u7684\u6570\u636e\n     * @return \u89e3\u5bc6\u6570\u636e\n     *\/\n    public static byte[] decryptByPrivateKey(byte[] privKeyInByte, byte[] data) {\n        try {\n            PKCS8EncodedKeySpec priv_spec = new PKCS8EncodedKeySpec(\n                    privKeyInByte);\n            KeyFactory mykeyFactory = KeyFactory.getInstance(&quot;RSA&quot;);\n            PrivateKey privKey = mykeyFactory.generatePrivate(priv_spec);\n            Cipher cipher = Cipher.getInstance(&quot;RSA\/ECB\/PKCS1Padding&quot;);\n            cipher.init(Cipher.DECRYPT_MODE, privKey);\n            return cipher.doFinal(data);\n        } catch (Exception e) {\n            return null;\n        }\n    }\n\n    \/**\n     * \u4f7f\u7528RSA\u79c1\u94a5\u52a0\u5bc6\u6570\u636e\n     *\n     * @param privKeyInByte \u6253\u5305\u7684byte[]\u5f62\u5f0f\u79c1\u94a5\n     * @param data          \u8981\u52a0\u5bc6\u7684\u6570\u636e\n     * @return \u52a0\u5bc6\u6570\u636e\n     *\/\n    public static byte[] encryptByPrivateKey(byte[] privKeyInByte, byte[] data) {\n        try {\n            PKCS8EncodedKeySpec priv_spec = new PKCS8EncodedKeySpec(\n                    privKeyInByte);\n            KeyFactory mykeyFactory = KeyFactory.getInstance(&quot;RSA&quot;);\n            PrivateKey privKey = mykeyFactory.generatePrivate(priv_spec);\n            Cipher cipher = Cipher.getInstance(mykeyFactory.getAlgorithm());\n            cipher.init(Cipher.ENCRYPT_MODE, privKey);\n            return cipher.doFinal(data);\n        } catch (Exception e) {\n            return null;\n        }\n    }\n\n    \/**\n     * \u7528RSA\u516c\u94a5\u89e3\u5bc6\n     *\n     * @param pubKeyInByte \u516c\u94a5\u6253\u5305\u6210byte[]\u5f62\u5f0f\n     * @param data         \u8981\u89e3\u5bc6\u7684\u6570\u636e\n     * @return \u89e3\u5bc6\u6570\u636e\n     *\/\n    public static byte[] decryptByPublicKey(byte[] pubKeyInByte, byte[] data) {\n        try {\n            KeyFactory mykeyFactory = KeyFactory.getInstance(&quot;RSA&quot;);\n            X509EncodedKeySpec pub_spec = new X509EncodedKeySpec(pubKeyInByte);\n            PublicKey pubKey = mykeyFactory.generatePublic(pub_spec);\n            Cipher cipher = Cipher.getInstance(mykeyFactory.getAlgorithm());\n            cipher.init(Cipher.DECRYPT_MODE, pubKey);\n            return cipher.doFinal(data);\n        } catch (Exception e) {\n            return null;\n        }\n    }\n\n    \/**\n     * \u8ba1\u7b97\u5b57\u7b26\u4e32\u7684SHA\u6570\u5b57\u6458\u8981\uff0c\u4ee5byte[]\u5f62\u5f0f\u8fd4\u56de\n     *\/\n    public static byte[] MdigestSHA(String source) {\n        \/\/byte[] nullreturn = { 0 };\n        try {\n            MessageDigest thisMD = MessageDigest.getInstance(&quot;SHA&quot;);\n            byte[] digest = thisMD.digest(source.getBytes(&quot;UTF-8&quot;));\n            return digest;\n        } catch (Exception e) {\n            return null;\n        }\n    }\n\n    \/**\n     * \u6d4b\u8bd5\n     *\/\n    public static void main(String[] args) {\n        try {\n            \/\/\u79c1\u94a5\u52a0\u5bc6 \u516c\u94a5\u89e3\u5bc6\n            \/\/\u751f\u6210\u79c1\u94a5-\u516c\u94a5\u5bf9\n            Object[] v = giveRSAKeyPairInByte();\n            \/\/\u83b7\u5f97\u6458\u8981\n            byte[] source = MdigestSHA(&quot;\u5047\u8bbe\u8fd9\u662f\u8981\u52a0\u5bc6\u7684\u5ba2\u6237\u6570\u636e&quot;);\n            \/\/\u4f7f\u7528\u79c1\u94a5\u5bf9\u6458\u8981\u8fdb\u884c\u52a0\u5bc6 \u83b7\u5f97\u5bc6\u6587 \u5373\u6570\u5b57\u7b7e\u540d\n            byte[] sign = sign((byte[]) v[0], source);\n            \/\/\u4f7f\u7528\u516c\u94a5\u5bf9\u5bc6\u6587\u8fdb\u884c\u89e3\u5bc6,\u89e3\u5bc6\u540e\u4e0e\u6458\u8981\u8fdb\u884c\u5339\u914d\n            boolean yes = verify((byte[]) v[1], source, sign);\n            if (yes) {\n                System.out.println(&quot;\u5339\u914d\u6210\u529f \u5408\u6cd5\u7684\u7b7e\u540d!&quot;);\n            }\n\n            \/\/\u516c\u94a5\u52a0\u5bc6\u79c1\u94a5\u89e3\u5bc6\n            \/\/\u83b7\u5f97\u6458\u8981\n            byte[] sourcepub_pri = (&quot;13265986584||316494646546486498||01||public&quot;).getBytes(&quot;UTF-8&quot;);\n\n            \/\/\u4f7f\u7528\u516c\u94a5\u5bf9\u6458\u8981\u8fdb\u884c\u52a0\u5bc6 \u83b7\u5f97\u5bc6\u6587\n            byte[] signpub_pri = encryptByPublicKey((byte[]) v[1], sourcepub_pri);\n            \/\/System.out.println(&quot;\u516c\u94a5\u52a0\u5bc6\u5bc6\u6587\uff1a&quot;+new String(Base64.encodeBase64(signpub_pri)));\n\n            \/\/\u4f7f\u7528\u79c1\u94a5\u5bf9\u5bc6\u6587\u8fdb\u884c\u89e3\u5bc6 \u8fd4\u56de\u89e3\u5bc6\u540e\u7684\u6570\u636e\n            byte[] newSourcepub_pri = decryptByPrivateKey((byte[]) v[0], signpub_pri);\n\n            System.out.println(&quot;\u79c1\u94a5\u89e3\u5bc6\uff1a&quot; + new String(newSourcepub_pri, &quot;UTF-8&quot;));\n            \/\/\u5bf9\u6bd4\u6e90\u6570\u636e\u4e0e\u89e3\u5bc6\u540e\u7684\u6570\u636e\n            if (Arrays.equals(sourcepub_pri, newSourcepub_pri)) {\n                System.out.println(&quot;\u5339\u914d\u6210\u529f \u5408\u6cd5\u7684\u79c1\u94a5!&quot;);\n            }\n\n            \/\/\u79c1\u94a5\u52a0\u5bc6\u516c\u94a5\u89e3\u5bc6\n            \/\/\u83b7\u5f97\u6458\u8981\n            \/\/byte[] source = MdigestSHA(&quot;\u5047\u8bbe\u8fd9\u662f\u8981\u52a0\u5bc6\u7684\u5ba2\u6237\u6570\u636e&quot;);\n            byte[] source = (&quot;13265986584||316494646546486498||01||private&quot;).getBytes(&quot;UTF-8&quot;);\n\n            \/\/\u4f7f\u7528\u79c1\u94a5\u5bf9\u6458\u8981\u8fdb\u884c\u52a0\u5bc6 \u83b7\u5f97\u5bc6\u6587\n            byte[] encrypted = encryptByPrivateKey((byte[]) v[0], source);\n\n            \/\/System.out.println(&quot;\u79c1\u94a5\u52a0\u5bc6\u5bc6\u6587\uff1a&quot; + new String(Base64.encodeBase64(sign11)));\n            \/\/\u4f7f\u7528\u516c\u94a5\u5bf9\u5bc6\u6587\u8fdb\u884c\u89e3\u5bc6 \u8fd4\u56de\u89e3\u5bc6\u540e\u7684\u6570\u636e\n            byte[] decrypted = decryptByPublicKey((byte[]) v[1], encrypted);\n\n            System.out.println(&quot;\u516c\u94a5\u89e3\u5bc6\uff1a&quot; + new String(decrypted, &quot;UTF-8&quot;));\n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EdaQlcs3ywLrrJ0o3VAR0J9aq59cmp6em017AMnmbF7&quot;;\n\n            byte[] signPrivate = Base64.decodeBase64(PRIVATEKEY.getBytes());\n            byte[] signPublic = Base64.decodeBase64(PUBLICKEY.getBytes());\n\n            String publicPwd = &quot;N\/b4nYbbLFVq0yTAIOpNNydtNQUCQxQy0B7bD6kzxLMW2guYxXtWOC\/9Z5dpWecx\/y7d5CezUJ6cf\/8++msiNie4DcKBaFDFPh5rPbjeEB+DRfhjcdR2BsVGXWLsq3dLYLgZObQXG6Tb9rXakuH34Y+6KIIwCjiODH2QAU+PSiM=&quot;;\n            String privatePwd = &quot;MTMyNjU5ODY1ODR8fDMxNjQ5NDY0NjU0NjQ4NjQ5OHx8MDF8fHByaXZhdGU=&quot;;\n            \/\/\u4f7f\u7528\u79c1\u94a5\u5bf9\u5bc6\u6587\u8fdb\u884c\u89e3\u5bc6 \u8fd4\u56de\u89e3\u5bc6\u540e\u7684\u6570\u636e\n            byte[] newSource = decryptByPrivateKey(signPrivate, Base64.decodeBase64(publicPwd.getBytes()));\n            System.out.println(&quot;\u79c1\u94a5\u89e3\u5bc6\uff1a&quot; + new String(newSource, &quot;UTF-8&quot;));\n\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n\n        \/*\u8fd0\u884c\u7ed3\u679c\uff1a\n        \u5339\u914d\u6210\u529f \u5408\u6cd5\u7684\u7b7e\u540d!\n        \u79c1\u94a5\u89e3\u5bc6\uff1a13265986584||316494646546486498||01||public\n        \u5339\u914d\u6210\u529f \u5408\u6cd5\u7684\u79c1\u94a5!\n        \u516c\u94a5\u89e3\u5bc6\uff1a13265986584||316494646546486498||01||private\n        \u79c1\u94a5\u89e3\u5bc6\uff1a13265986584||316494646546486498||01||3156464564\n        *\/\n    }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8eJava\u4e2dRSA\u52a0\u7b7e\u89e3\u7b7e\uff0c\u79c1\u94a5\u52a0\u5bc6\u516c\u94a5\u89e3\u5bc6\u548c\u516c\u94a5\u52a0\u5bc6\u79c1\u94a5\u89e3\u5bc6\uff0c\u4e00\u822c\u4e3a\u4e86\u5b89\u5168\u91c7\u7528\u7684\u662f \u79c1\u94a5\u52a0\u7b7e\uff0c\u516c\u94a5\u9a8c\u7b7e \u672c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[108],"class_list":["post-332","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-rsa"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=332"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/332\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}