{"id":753,"date":"2023-09-24T09:01:01","date_gmt":"2023-09-24T01:01:01","guid":{"rendered":""},"modified":"2023-09-24T09:01:01","modified_gmt":"2023-09-24T01:01:01","slug":"java\u9a8c\u8bc1\u516c\u94a5\u79c1\u94a5\u662f\u5426\u5339\u914d\u53ca\u516c\u94a5\u79c1\u94a5\u4e0e\u5b57\u7b26\u4e32\u76f8\u4e92\u8f6c\u6362","status":"publish","type":"post","link":"https:\/\/mushiming.com\/753.html","title":{"rendered":"java\u9a8c\u8bc1\u516c\u94a5\u79c1\u94a5\u662f\u5426\u5339\u914d\u53ca\u516c\u94a5\u79c1\u94a5\u4e0e\u5b57\u7b26\u4e32\u76f8\u4e92\u8f6c\u6362"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u672c\u6587\u8bb0\u5f55\u4f7f\u7528java\u751f\u6210\u516c\u94a5\u79c1\u94a5\uff0c\u5c06\u516c\u94a5\u79c1\u94a5\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u9a8c\u8bc1\u516c\u94a5\u79c1\u94a5\u662f\u5426\u5339\u914d\u3002<\/p>\n

\u9a8c\u8bc1\u516c\u94a5\u79c1\u94a5\u662f\u5426\u5339\u914d\u7684\u601d\u60f3\uff1a\u4f7f\u7528\u516c\u94a5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u52a0\u5bc6\uff0c\u518d\u4f7f\u7528\u79c1\u94a5\u8fdb\u884c\u89e3\u5bc6\uff0c\u82e5\u89e3\u5bc6\u751f\u6210\u7684\u5b57\u7b26\u4e32\u4e0e\u539f\u5b57\u7b26\u4e32\u4e00\u81f4\uff0c\u5219\u8bf4\u660e\u516c\u94a5\u79c1\u94a5\u5339\u914d\u3002<\/p>\n

\u5f15\u5165\u7b2c\u4e09\u65b9\u4f9d\u8d56bouncycastle\uff1a<\/p>\n

        <<\/span>dependency<\/span>><\/span><\/span>\n            <<\/span>groupId<\/span>><\/span><\/span>org.bouncycastle<\/<\/span>groupId<\/span>><\/span><\/span>\n            <<\/span>artifactId<\/span>><\/span><\/span>bcprov-jdk15on<\/<\/span>artifactId<\/span>><\/span><\/span>\n            <<\/span>version<\/span>><\/span><\/span>1.68<\/<\/span>version<\/span>><\/span><\/span>\n        <\/<\/span>dependency<\/span>><\/span><\/span>\n<\/code><\/pre>\n

\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n

import<\/span> java.<\/span>security.<\/span><\/span>InvalidKeyException<\/span>;<\/span>\nimport<\/span> java.<\/span>security.<\/span><\/span>KeyFactory<\/span>;<\/span>\nimport<\/span> java.<\/span>security.<\/span><\/span>KeyPair<\/span>;<\/span>\nimport<\/span> java.<\/span>security.<\/span><\/span>KeyPairGenerator<\/span>;<\/span>\nimport<\/span> java.<\/span>security.<\/span><\/span>NoSuchAlgorithmException<\/span>;<\/span>\nimport<\/span> java.<\/span>security.<\/span><\/span>SecureRandom<\/span>;<\/span>\nimport<\/span> java.<\/span>security.<\/span>interfaces.<\/span><\/span>RSAPrivateKey<\/span>;<\/span>\nimport<\/span> java.<\/span>security.<\/span>interfaces.<\/span><\/span>RSAPublicKey<\/span>;<\/span>\nimport<\/span> java.<\/span>security.<\/span>spec.<\/span><\/span>InvalidKeySpecException<\/span>;<\/span>\nimport<\/span> java.<\/span>security.<\/span>spec.<\/span><\/span>PKCS8EncodedKeySpec<\/span>;<\/span>\nimport<\/span> java.<\/span>security.<\/span>spec.<\/span><\/span>X509EncodedKeySpec<\/span>;<\/span>\n\nimport<\/span> javax.<\/span>crypto.<\/span><\/span>BadPaddingException<\/span>;<\/span>\nimport<\/span> javax.<\/span>crypto.<\/span><\/span>Cipher<\/span>;<\/span>\nimport<\/span> javax.<\/span>crypto.<\/span><\/span>IllegalBlockSizeException<\/span>;<\/span>\nimport<\/span> javax.<\/span>crypto.<\/span><\/span>NoSuchPaddingException<\/span>;<\/span>\n\nimport<\/span> org.<\/span>bouncycastle.<\/span>jce.<\/span>provider.<\/span><\/span>BouncyCastleProvider<\/span>;<\/span>\n\nimport<\/span> java.<\/span>util.<\/span><\/span>Base64<\/span>;<\/span>\nimport<\/span> java.<\/span>util.<\/span><\/span>Random<\/span>;<\/span>\n\n\/** * \u8be5\u7c7b\u7528\u4e8e\u751f\u6210\u516c\u94a5\u79c1\u94a5\uff0c\u5e76\u4e14\u9a8c\u8bc1\u516c\u94a5\u79c1\u94a5\u662f\u5426\u5339\u914d * \u4f7f\u7528\u65b9\u6cd5\uff1a * 1.\u751f\u6210\u516c\u94a5\u79c1\u94a5\uff1a \u4f7f\u7528new RSAEncrypt()\u521b\u5efa\u5bf9\u8c61\uff0c\u4e4b\u540e\u8c03\u7528get\u65b9\u6cd5\u5373\u53ef\u83b7\u53d6\u751f\u6210\u7684\u516c\u94a5\u79c1\u94a5\uff08\u5b57\u7b26\u4e32\uff09 * 2.\u9a8c\u8bc1\u516c\u94a5\u79c1\u94a5\u662f\u5426\u5339\u914d\uff1a \u8c03\u7528\u9759\u6001\u65b9\u6cd5verifyPublicAndPrivateKey()\uff0c\u4f20\u5165\u516c\u94a5\u79c1\u94a5\u5b57\u7b26\u4e32\uff0c\u8fd4\u56deboolean\u5224\u65ad\u662f\u5426\u5339\u914d *\/<\/span>\npublic<\/span> class<\/span> RSAEncrypt<\/span> { \n   <\/span>\n\n    private<\/span> RSAPrivateKey<\/span> privateKey;<\/span>\n    private<\/span> RSAPublicKey<\/span> publicKey;<\/span>\n\n    public<\/span> RSAEncrypt<\/span>(<\/span>)<\/span> { \n   <\/span>\n        genKeyPair<\/span>(<\/span>)<\/span>;<\/span>\n    }<\/span>\n\n    \/** * \u83b7\u53d6\u79c1\u94a5\u5b57\u7b26\u4e32 * * @return \u5f53\u524d\u7684\u79c1\u94a5\u5b57\u7b26\u4e32 *\/<\/span>\n    public<\/span> String<\/span> getPrivateKeyStr<\/span>(<\/span>)<\/span> { \n   <\/span>\n        KeyFactory<\/span> keyFactory =<\/span> null<\/span>;<\/span>\n        String<\/span> privateKeyStr =<\/span> null<\/span>;<\/span>\n        try<\/span> { \n   <\/span>\n        \t\/\/\u5c06\u79c1\u94a5\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/span>\n            keyFactory =<\/span> KeyFactory<\/span>.<\/span>getInstance<\/span>(<\/span>\"RSA\"<\/span>)<\/span>;<\/span>\n            PKCS8EncodedKeySpec<\/span> keySpec =<\/span> keyFactory.<\/span>getKeySpec<\/span>(<\/span>privateKey,<\/span> PKCS8EncodedKeySpec<\/span>.<\/span>class<\/span>)<\/span>;<\/span>\n            byte<\/span>[<\/span>]<\/span> buffer =<\/span> keySpec.<\/span>getEncoded<\/span>(<\/span>)<\/span>;<\/span>\n            privateKeyStr =<\/span> Base64<\/span>.<\/span>getEncoder<\/span>(<\/span>)<\/span>.<\/span>encodeToString<\/span>(<\/span>buffer)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>NoSuchAlgorithmException<\/span> e)<\/span> { \n   <\/span>\n            System<\/span>.<\/span>err.<\/span>println<\/span>(<\/span>\"\u65e0\u6b64\u7b97\u6cd5\"<\/span>)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>InvalidKeySpecException<\/span> e)<\/span> { \n   <\/span>\n            System<\/span>.<\/span>err.<\/span>println<\/span>(<\/span>\"\u79c1\u94a5\u975e\u6cd5\"<\/span>)<\/span>;<\/span>\n        }<\/span>\n        return<\/span> privateKeyStr;<\/span>\n    }<\/span>\n\n    \/** * \u83b7\u53d6\u516c\u94a5\u5b57\u7b26\u4e32 * * @return \u5f53\u524d\u7684\u516c\u94a5\u5b57\u7b26\u4e32 *\/<\/span>\n    public<\/span> String<\/span> getPublicKeyStr<\/span>(<\/span>)<\/span> { \n   <\/span>\n        KeyFactory<\/span> keyFactory =<\/span> null<\/span>;<\/span>\n        String<\/span> publicKeyStr =<\/span> null<\/span>;<\/span>\n        try<\/span> { \n   <\/span>\n        \t\/\/\u5c06\u516c\u94a5\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/span>\n            keyFactory =<\/span> KeyFactory<\/span>.<\/span>getInstance<\/span>(<\/span>\"RSA\"<\/span>)<\/span>;<\/span>\n            X509EncodedKeySpec<\/span> keySpec =<\/span> keyFactory.<\/span>getKeySpec<\/span>(<\/span>publicKey,<\/span> X509EncodedKeySpec<\/span>.<\/span>class<\/span>)<\/span>;<\/span>\n            byte<\/span>[<\/span>]<\/span> buffer =<\/span> keySpec.<\/span>getEncoded<\/span>(<\/span>)<\/span>;<\/span>\n            publicKeyStr =<\/span> Base64<\/span>.<\/span>getEncoder<\/span>(<\/span>)<\/span>.<\/span>encodeToString<\/span>(<\/span>buffer)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>NoSuchAlgorithmException<\/span> e)<\/span> { \n   <\/span>\n            System<\/span>.<\/span>err.<\/span>println<\/span>(<\/span>\"\u65e0\u6b64\u7b97\u6cd5\"<\/span>)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>InvalidKeySpecException<\/span> e)<\/span> { \n   <\/span>\n            System<\/span>.<\/span>err.<\/span>println<\/span>(<\/span>\"\u516c\u94a5\u975e\u6cd5\"<\/span>)<\/span>;<\/span>\n        }<\/span>\n        return<\/span> publicKeyStr;<\/span>\n    }<\/span>\n\n    \/** * \u968f\u673a\u751f\u6210\u516c\u94a5-\u79c1\u94a5 *\/<\/span>\n    private<\/span> void<\/span> genKeyPair<\/span>(<\/span>)<\/span> { \n   <\/span>\n        KeyPairGenerator<\/span> keyPairGen =<\/span> null<\/span>;<\/span>\n        try<\/span> { \n   <\/span>\n            keyPairGen =<\/span> KeyPairGenerator<\/span>.<\/span>getInstance<\/span>(<\/span>\"RSA\"<\/span>)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>NoSuchAlgorithmException<\/span> e)<\/span> { \n   <\/span>\n            e.<\/span>printStackTrace<\/span>(<\/span>)<\/span>;<\/span>\n        }<\/span>\n        keyPairGen.<\/span>initialize<\/span>(<\/span>1024<\/span>,<\/span> new<\/span> SecureRandom<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n        KeyPair<\/span> keyPair =<\/span> keyPairGen.<\/span>generateKeyPair<\/span>(<\/span>)<\/span>;<\/span>\n        this<\/span>.<\/span>privateKey =<\/span> (<\/span>RSAPrivateKey<\/span>)<\/span> keyPair.<\/span>getPrivate<\/span>(<\/span>)<\/span>;<\/span>\n        this<\/span>.<\/span>publicKey =<\/span> (<\/span>RSAPublicKey<\/span>)<\/span> keyPair.<\/span>getPublic<\/span>(<\/span>)<\/span>;<\/span>\n    }<\/span>\n\n    \/** * @param publicKeyStr \u516c\u94a5 * @param privateKeyStr \u79c1\u94a5 * @return boolean\u503c\u5224\u65ad\u662f\u5426\u5339\u914d *\/<\/span>\n    public<\/span> static<\/span> boolean<\/span> verifyPublicAndPrivateKey<\/span>(<\/span>String<\/span> publicKeyStr,<\/span> String<\/span> privateKeyStr)<\/span> { \n   <\/span>\n        RSAPublicKey<\/span> publicKeyToVerify =<\/span> null<\/span>;<\/span>\n        RSAPrivateKey<\/span> privateKeyToVerify =<\/span> null<\/span>;<\/span>\n        try<\/span> { \n   <\/span>\n            publicKeyToVerify =<\/span> loadPublicKey<\/span>(<\/span>publicKeyStr)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>Exception<\/span> e)<\/span> { \n   <\/span>\n            System<\/span>.<\/span>err.<\/span>println<\/span>(<\/span>\"\u52a0\u8f7d\u516c\u94a5\u5931\u8d25-\u516c\u94a5\u975e\u6cd5\"<\/span>)<\/span>;<\/span>\n            return<\/span> false<\/span>;<\/span>\n        }<\/span>\n        try<\/span> { \n   <\/span>\n            privateKeyToVerify =<\/span> loadPrivateKey<\/span>(<\/span>privateKeyStr)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>Exception<\/span> e)<\/span> { \n   <\/span>\n            System<\/span>.<\/span>err.<\/span>println<\/span>(<\/span>\"\u52a0\u8f7d\u79c1\u94a5\u5931\u8d25-\u79c1\u94a5\u975e\u6cd5\"<\/span>)<\/span>;<\/span>\n            return<\/span> false<\/span>;<\/span>\n        }<\/span>\n        if<\/span> (<\/span>publicKeyToVerify ==<\/span> null<\/span> ||<\/span> privateKeyToVerify ==<\/span> null<\/span>)<\/span> { \n   <\/span>\n            return<\/span> false<\/span>;<\/span>\n        }<\/span>\n        \/\/\u751f\u6210\u968f\u673a\u6570\u5b57\u7b26\u4e32\u7528\u4e8e\u9a8c\u8bc1\u516c\u94a5\u79c1\u94a5\u662f\u5426\u5339\u914d<\/span>\n        Random<\/span> seed =<\/span> new<\/span> Random<\/span>(<\/span>)<\/span>;<\/span>\n        int<\/span> randomNum =<\/span> seed.<\/span>nextInt<\/span>(<\/span>1000<\/span>)<\/span> +<\/span> 1<\/span>;<\/span>\n        String<\/span> randomStr =<\/span> String<\/span>.<\/span>valueOf<\/span>(<\/span>randomNum)<\/span>;<\/span>\n\n        byte<\/span>[<\/span>]<\/span> cipher =<\/span> null<\/span>;<\/span>\n        byte<\/span>[<\/span>]<\/span> plainText =<\/span> null<\/span>;<\/span>\n        try<\/span> { \n   <\/span>\n            cipher =<\/span> encrypt<\/span>(<\/span>publicKeyToVerify,<\/span> randomStr.<\/span>getBytes<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n            plainText =<\/span> decrypt<\/span>(<\/span>privateKeyToVerify,<\/span> cipher)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>Exception<\/span> e)<\/span> { \n   <\/span>\n            e.<\/span>printStackTrace<\/span>(<\/span>)<\/span>;<\/span>\n            return<\/span> false<\/span>;<\/span>\n        }<\/span>\n\n        String<\/span> plainStr =<\/span> new<\/span> String<\/span>(<\/span>plainText)<\/span>;<\/span>\n        return<\/span> plainStr.<\/span>equals<\/span>(<\/span>randomStr)<\/span>;<\/span>\n    }<\/span>\n\n\n    \/** * \u4ece\u5b57\u7b26\u4e32\u4e2d\u52a0\u8f7d\u516c\u94a5 * * @param publicKeyStr \u516c\u94a5\u6570\u636e\u5b57\u7b26\u4e32 * @throws Exception \u52a0\u8f7d\u516c\u94a5\u65f6\u4ea7\u751f\u7684\u5f02\u5e38 *\/<\/span>\n    private<\/span> static<\/span> RSAPublicKey<\/span> loadPublicKey<\/span>(<\/span>String<\/span> publicKeyStr)<\/span> throws<\/span> Exception<\/span> { \n   <\/span>\n        RSAPublicKey<\/span> loadedPublicKey =<\/span> null<\/span>;<\/span>\n        try<\/span> { \n   <\/span>\n            byte<\/span>[<\/span>]<\/span> buffer =<\/span> Base64<\/span>.<\/span>getDecoder<\/span>(<\/span>)<\/span>.<\/span>decode<\/span>(<\/span>publicKeyStr)<\/span>;<\/span>\n            KeyFactory<\/span> keyFactory =<\/span> KeyFactory<\/span>.<\/span>getInstance<\/span>(<\/span>\"RSA\"<\/span>)<\/span>;<\/span>\n            X509EncodedKeySpec<\/span> keySpec =<\/span> new<\/span> X509EncodedKeySpec<\/span>(<\/span>buffer)<\/span>;<\/span>\n            loadedPublicKey =<\/span> (<\/span>RSAPublicKey<\/span>)<\/span> keyFactory.<\/span>generatePublic<\/span>(<\/span>keySpec)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>NoSuchAlgorithmException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>e)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>InvalidKeySpecException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>e)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>NullPointerException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>e)<\/span>;<\/span>\n        }<\/span>\n        return<\/span> loadedPublicKey;<\/span>\n    }<\/span>\n\n    \/** * \u4ece\u5b57\u7b26\u4e32\u4e2d\u52a0\u8f7d\u79c1\u94a5 * * @param privateKeyStr \u79c1\u94a5\u6570\u636e\u5b57\u7b26\u4e32 * @throws Exception \u52a0\u8f7d\u79c1\u94a5\u65f6\u4ea7\u751f\u7684\u5f02\u5e38 *\/<\/span>\n    private<\/span> static<\/span> RSAPrivateKey<\/span> loadPrivateKey<\/span>(<\/span>String<\/span> privateKeyStr)<\/span> throws<\/span> Exception<\/span> { \n   <\/span>\n        RSAPrivateKey<\/span> loadedPrivateKey =<\/span> null<\/span>;<\/span>\n        try<\/span> { \n   <\/span>\n            byte<\/span>[<\/span>]<\/span> buffer =<\/span> Base64<\/span>.<\/span>getDecoder<\/span>(<\/span>)<\/span>.<\/span>decode<\/span>(<\/span>privateKeyStr)<\/span>;<\/span>\n            PKCS8EncodedKeySpec<\/span> keySpec =<\/span> new<\/span> PKCS8EncodedKeySpec<\/span>(<\/span>buffer)<\/span>;<\/span>\n            KeyFactory<\/span> keyFactory =<\/span> KeyFactory<\/span>.<\/span>getInstance<\/span>(<\/span>\"RSA\"<\/span>)<\/span>;<\/span>\n            loadedPrivateKey =<\/span> (<\/span>RSAPrivateKey<\/span>)<\/span> keyFactory.<\/span>generatePrivate<\/span>(<\/span>keySpec)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>NoSuchAlgorithmException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u65e0\u6b64\u7b97\u6cd5\"<\/span>)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>InvalidKeySpecException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u79c1\u94a5\u975e\u6cd5\"<\/span>)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>NullPointerException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u79c1\u94a5\u6570\u636e\u4e3a\u7a7a\"<\/span>)<\/span>;<\/span>\n        }<\/span>\n        return<\/span> loadedPrivateKey;<\/span>\n    }<\/span>\n\n    \/** * \u52a0\u5bc6\u8fc7\u7a0b * * @param publicKey \u516c\u94a5 * @param plainTextData \u660e\u6587\u6570\u636e * @return \u52a0\u5bc6\u540e\u7684\u6570\u636e * @throws Exception \u52a0\u5bc6\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u4fe1\u606f *\/<\/span>\n    private<\/span> static<\/span> byte<\/span>[<\/span>]<\/span> encrypt<\/span>(<\/span>RSAPublicKey<\/span> publicKey,<\/span> byte<\/span>[<\/span>]<\/span> plainTextData)<\/span> throws<\/span> Exception<\/span> { \n   <\/span>\n        if<\/span> (<\/span>publicKey ==<\/span> null<\/span>)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u52a0\u5bc6\u516c\u94a5\u4e3a\u7a7a, \u8bf7\u8bbe\u7f6e\"<\/span>)<\/span>;<\/span>\n        }<\/span>\n        Cipher<\/span> cipher =<\/span> null<\/span>;<\/span>\n        try<\/span> { \n   <\/span>\n            cipher =<\/span> Cipher<\/span>.<\/span>getInstance<\/span>(<\/span>\"RSA\"<\/span>,<\/span> new<\/span> BouncyCastleProvider<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n            cipher.<\/span>init<\/span>(<\/span>Cipher<\/span>.<\/span>ENCRYPT_MODE,<\/span> publicKey)<\/span>;<\/span>\n            byte<\/span>[<\/span>]<\/span> output =<\/span> cipher.<\/span>doFinal<\/span>(<\/span>plainTextData)<\/span>;<\/span>\n            return<\/span> output;<\/span>\n        }<\/span> catch<\/span> (<\/span>NoSuchAlgorithmException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u65e0\u6b64\u52a0\u5bc6\u7b97\u6cd5\"<\/span>)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>NoSuchPaddingException<\/span> e)<\/span> { \n   <\/span>\n            e.<\/span>printStackTrace<\/span>(<\/span>)<\/span>;<\/span>\n            return<\/span> null<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>InvalidKeyException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u52a0\u5bc6\u516c\u94a5\u975e\u6cd5,\u8bf7\u68c0\u67e5\"<\/span>)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>IllegalBlockSizeException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u660e\u6587\u957f\u5ea6\u975e\u6cd5\"<\/span>)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>BadPaddingException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u660e\u6587\u6570\u636e\u5df2\u635f\u574f\"<\/span>)<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n\n    \/** * \u89e3\u5bc6\u8fc7\u7a0b * * @param privateKey \u79c1\u94a5 * @param cipherData \u5bc6\u6587\u6570\u636e * @return \u660e\u6587 * @throws Exception \u89e3\u5bc6\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u4fe1\u606f *\/<\/span>\n    private<\/span> static<\/span> byte<\/span>[<\/span>]<\/span> decrypt<\/span>(<\/span>RSAPrivateKey<\/span> privateKey,<\/span> byte<\/span>[<\/span>]<\/span> cipherData)<\/span> throws<\/span> Exception<\/span> { \n   <\/span>\n        if<\/span> (<\/span>privateKey ==<\/span> null<\/span>)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u89e3\u5bc6\u79c1\u94a5\u4e3a\u7a7a, \u8bf7\u8bbe\u7f6e\"<\/span>)<\/span>;<\/span>\n        }<\/span>\n        Cipher<\/span> cipher =<\/span> null<\/span>;<\/span>\n        try<\/span> { \n   <\/span>\n            cipher =<\/span> Cipher<\/span>.<\/span>getInstance<\/span>(<\/span>\"RSA\"<\/span>,<\/span> new<\/span> BouncyCastleProvider<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n            cipher.<\/span>init<\/span>(<\/span>Cipher<\/span>.<\/span>DECRYPT_MODE,<\/span> privateKey)<\/span>;<\/span>\n            byte<\/span>[<\/span>]<\/span> output =<\/span> cipher.<\/span>doFinal<\/span>(<\/span>cipherData)<\/span>;<\/span>\n            return<\/span> output;<\/span>\n        }<\/span> catch<\/span> (<\/span>NoSuchAlgorithmException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u65e0\u6b64\u89e3\u5bc6\u7b97\u6cd5\"<\/span>)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>NoSuchPaddingException<\/span> e)<\/span> { \n   <\/span>\n            e.<\/span>printStackTrace<\/span>(<\/span>)<\/span>;<\/span>\n            return<\/span> null<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>InvalidKeyException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u89e3\u5bc6\u79c1\u94a5\u975e\u6cd5,\u8bf7\u68c0\u67e5\"<\/span>)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>IllegalBlockSizeException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u5bc6\u6587\u957f\u5ea6\u975e\u6cd5\"<\/span>)<\/span>;<\/span>\n        }<\/span> catch<\/span> (<\/span>BadPaddingException<\/span> e)<\/span> { \n   <\/span>\n            throw<\/span> new<\/span> Exception<\/span>(<\/span>\"\u5bc6\u6587\u6570\u636e\u5df2\u635f\u574f\"<\/span>)<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n\n\n    public<\/span> static<\/span> void<\/span> main<\/span>(<\/span>String<\/span>[<\/span>]<\/span> args)<\/span> { \n   <\/span>\n        RSAEncrypt<\/span> rsaEncrypt =<\/span> new<\/span> RSAEncrypt<\/span>(<\/span>)<\/span>;<\/span>\n        System<\/span>.<\/span>out.<\/span>println<\/span>(<\/span>\"\u516c\u94a5\uff1a\"<\/span> +<\/span> rsaEncrypt.<\/span>getPublicKeyStr<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n        System<\/span>.<\/span>out.<\/span>println<\/span>(<\/span>\"\u79c1\u94a5\uff1a\"<\/span> +<\/span> rsaEncrypt.<\/span>getPrivateKeyStr<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n        boolean<\/span> res =<\/span> RSAEncrypt<\/span>.<\/span>verifyPublicAndPrivateKey<\/span>(<\/span>rsaEncrypt.<\/span>getPublicKeyStr<\/span>(<\/span>)<\/span>,<\/span> rsaEncrypt.<\/span>getPrivateKeyStr<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n        System<\/span>.<\/span>out.<\/span>println<\/span>(<\/span>\"\u662f\u5426\u5339\u914d\uff1a\"<\/span> +<\/span> res)<\/span>;<\/span>\n    }<\/span>\n}<\/span>\n<\/code><\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a
\"java\u9a8c\u8bc1\u516c\u94a5\u79c1\u94a5\u662f\u5426\u5339\u914d\u53ca\u516c\u94a5\u79c1\u94a5\u4e0e\u5b57\u7b26\u4e32\u76f8\u4e92\u8f6c\u6362
\u53c2\u8003\u6587\u7ae0\uff1aJava\u4e2d\u4f7f\u7528OpenSSL\u751f\u6210\u7684RSA\u516c\u79c1\u94a5\u8fdb\u884c\u6570\u636e\u52a0\u89e3\u5bc6<\/p>\n","protected":false},"excerpt":{"rendered":"java\u9a8c\u8bc1\u516c\u94a5\u79c1\u94a5\u662f\u5426\u5339\u914d\u53ca\u516c\u94a5\u79c1\u94a5\u4e0e\u5b57\u7b26\u4e32\u76f8\u4e92\u8f6c\u6362\u672c\u6587\u8bb0\u5f55\u4f7f\u7528java\u751f\u6210\u516c\u94a5\u79c1\u94a5\uff0c\u5c06\u516c\u94a5\u79c1\u94a5\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u9a8c\u8bc1\u516c\u94a5\u79c1\u94a5...","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\/753"}],"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=753"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/753\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}