All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eu.vooo.commons.security.util.RSAUtils Maven / Gradle / Ivy

The newest version!
package org.eu.vooo.commons.security.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * @author [email protected]
 */
public class RSAUtils {

    /**
     * openssl genrsa -out private-key.pem 4096
     * openssl rsa -in private-key.pem -pubout -out public-key.pem
     * 私钥格式需要转换程pkcs8格式, 公钥为生成默认x509格式
     * openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in private-key.pem -out private-key.key
     */
    private static final String DEFAULT_PUBLIC_KEY =
            "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAp6fhCZHqrMdgYTKQlwXN" +
                    "DKM5jSmcinnPjs+j+gETYoj6G/J0nuMWJWgapGaYWtBraECUlrrBHgYdA+RH6IjJ" +
                    "NTsU9DgUxEim/AAHYPyQyltYbIcxX/XDx73BKOBynCO0gqAVOd8tuTtKQ4r/MWK+" +
                    "Tab4ahNPwiq+EOjFLrdcQrwcjSDYR8MfJwDQb6IKZnGq1cr6eJKlByNKWr9hTexl" +
                    "5RKE0mv4oSv1iP9p5bwYocXHeFSCglpmCya/6q0Z2n3KVNx8UMikrI/cRzLuOOnd" +
                    "pVk/pz35Q98mwS41tO0Y4BNVj2UVtJhc87fZAztz+Het/GGcM9EPl9hJt6rXy9It" +
                    "GvKIsP59YR2rn1UAzxfgE08dlWj1WPmWWsUIdQ3xeoX68f/VrxXXxhkP8KKAvZM3" +
                    "4rYnQDy1IdcbNYQxZon5KQtVEIrQqGrOvuSNTm+kIHo7Mx7jrKORdpjrzebCPggx" +
                    "jr+gpPifv/isD2nZMGt4moe8I0Ti1wgEbN9lFsQ6KdVAmvmBlHqkhTE4wMCZw4y0" +
                    "9wshqcYbecn8JxaEfKktJFVVFUsPvlGpXU44mOEopoDsrfZBZX0dUHINR3p/CYEd" +
                    "+D7ynXOe9iPkcRCYRjtWEXDJjw4T8F468MwVcedPddQ4r2XI5ypQuHsLj5YGmGAc" +
                    "+i5sN3Ofloe1yO4HgPWS8dMCAwEAAQ==";

    private static final String DEFAULT_PRIVATE_KEY =
            "MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCnp+EJkeqsx2Bh" +
            "MpCXBc0MozmNKZyKec+Oz6P6ARNiiPob8nSe4xYlaBqkZpha0GtoQJSWusEeBh0D" +
            "5EfoiMk1OxT0OBTESKb8AAdg/JDKW1hshzFf9cPHvcEo4HKcI7SCoBU53y25O0pD" +
            "iv8xYr5NpvhqE0/CKr4Q6MUut1xCvByNINhHwx8nANBvogpmcarVyvp4kqUHI0pa" +
            "v2FN7GXlEoTSa/ihK/WI/2nlvBihxcd4VIKCWmYLJr/qrRnafcpU3HxQyKSsj9xH" +
            "Mu446d2lWT+nPflD3ybBLjW07RjgE1WPZRW0mFzzt9kDO3P4d638YZwz0Q+X2Em3" +
            "qtfL0i0a8oiw/n1hHaufVQDPF+ATTx2VaPVY+ZZaxQh1DfF6hfrx/9WvFdfGGQ/w" +
            "ooC9kzfitidAPLUh1xs1hDFmifkpC1UQitCoas6+5I1Ob6QgejszHuOso5F2mOvN" +
            "5sI+CDGOv6Ck+J+/+KwPadkwa3iah7wjROLXCARs32UWxDop1UCa+YGUeqSFMTjA" +
            "wJnDjLT3CyGpxht5yfwnFoR8qS0kVVUVSw++UaldTjiY4SimgOyt9kFlfR1Qcg1H" +
            "en8JgR34PvKdc572I+RxEJhGO1YRcMmPDhPwXjrwzBVx50911DivZcjnKlC4ewuP" +
            "lgaYYBz6Lmw3c5+Wh7XI7geA9ZLx0wIDAQABAoICACtfwFWUfY0jq6iZx1R6+LiL" +
            "ReliWZBtwSfmSjr0qko0u9gdGtS8VU9qJnJf5IyiHReElayFNk8JL9qA5aWgNcXi" +
            "uiIQnDbHwQOzEjWiQedISJETTM/RqkvzqJRltOD2T833kmY9efvQ/f6JaEojjK5U" +
            "QzJWOq02F6QSRGk4oNLXDd/pHms6bMXPbR1kY6JrR9I1yCUHc1BsbeEaYBcj0Jy5" +
            "9MjnjBROZiCCS6hWyFyZ6WgVHQqSTpIV/VK+Y9Dl+EGKJVNVBV1rfoahA4aP4nrT" +
            "oycmXmWTxtf1l4luSaA3AEyvzzTAslOPPcKbT/55H/cKXbn+HTWgjDnLJjch8e76" +
            "eH2yU7E9osRVoPfhB8HVdiXwCEONnRUcwOq+o/tR40UL+SVVa7TVYD2pbpmA7Re2" +
            "Uic7Oy4Qrchy86k7QYkWAEFmspzWaX5oB6tzCroRk5TNxmhVzsgtHRTLonccw2W3" +
            "JtfWe2Q7INXl8GPL2s+Qgs1iXiyW9em+0kI1DGbkSelWFh+ywLIxp3wLyiPyal+3" +
            "jhteJC5q+KOCT/gqc9dklOHOrMpZ4vzah7DP2bULV6qDfKOmNAGOSh4YltfGrjV0" +
            "Rt6KnUZgM2yED4vuSJGAPwv8Kje0WhkrtTDHJiKoh6WGT2pkoAeN4UzgRXLL4FIn" +
            "hIB6o+l1VTHaa9pbfbthAoIBAQDZ/l4mU5OUBQen49TgaMM8C89XhRv0Tkt5WluH" +
            "+/HO+fNpXVTS/nbRI1ty1v8X5yam4JP/zM8CggTa3QZcGcaL7qsEHFJdMvOr3TBL" +
            "DR6oJLkgdRXslox8d1FPRSNVJoCOS1fj2N/tdaCD/ZGS09Eg3HATaunV99NyJBs7" +
            "0pKx7clEhyhfa/4rS01rjvn01K47Dfd0W0S++GgCG5GHADsFVRooBQUrEgfg8kbX" +
            "VmMREMV72VRGn9s4ecyP59YFTGahNoaWQ3jlFcwEV3m6/OEn5J+YkE/tpP2clkqu" +
            "G2hI5Cv2ICrTnTynOvO8A5l2P/GgZEWXb7Mo5hrT7F4I1HyjAoIBAQDE4szM0Xdk" +
            "5nIava5n2RwWB3CqqG1eakcabWixYyq98L/Jybr9kzPBLnFv62jg85DABkiBLZPF" +
            "wjJLzldJiX9nW4jT7z5ZAcYYVIGSpG2i9GSQNSa5SdnpZvHHGzj/zDjeYxn/PhWf" +
            "Ho/Fd3WIQ3+3Az59+w9oRseHAycO2aXF/FqjTkEIBjgU1bwejDu/MEK+gS3Wu5A8" +
            "x4Zx5PKldZWNmU2okQqsiuGQ5k1Vn+FemwAmlz+4MaKq9TVzhcGpPGvBkwnXm4X6" +
            "+Ks/A6n5Ffy1x0r/87EhZUN3/uG0F/y20zybv51mkEVG3q8+s10cJ9zQ6KUgOz2w" +
            "CGslBuCPdlkRAoIBAQCPkls5zNOmqkVnisMaZYUoF/MgBjfqCGLm8t7TydhxY+ev" +
            "nluOdy+UAEBrezQY7xFi2QhAcgho8271Iprcr9E3OiKCG5EXdsZLi+sRkvt87GrM" +
            "+LBO2LyAASmZFsT1gN0sWmzxUFXzFAR6yddFp6HVjbl24HRPiuBFDYiuNrqM2zoy" +
            "mv25EuwcYYUE8O6pFCYIGOPAS5gUWpBNyxBmpH06vtjyQJQt0LtFqOuFSY0HoU8f" +
            "iXxa40/De1yB7sPjS3VQehPcBWYrItbyiRZQU9qKpqWCGpG5x2+M4OMglkZxs21M" +
            "tdTe+HEJBueeDZTV77lY1JGM8nFB34MNzmAEWsYXAoIBADxXnpkvob4k+tfDRs7B" +
            "NeByi09iq5KCVCR/X9TuJc1tGfrIfyz0jqooh8/bgJthSv3vbw1YYK/9otik9w+s" +
            "uoV9AmSiMKOq7YHLdKLzDj8hVCj2gVXMkqL8UCHL8B5GAy/1g6Q87PeFsF6/DCCl" +
            "P2Fh9yc5aTF2b/SrPK9KeYpCm6ztcPT6ovmPwEZEiJNCQVKN2PS1K7HVHRmPJ+ca" +
            "MKnjUG/XdEQJlLSKQS17AOGz88ngFUFoxQhMewNa/U3XHNeJLz0Lh3ax/BomD1Qu" +
            "LvOvjwb/M0ye8py2W4qMx4FpIGxDwAQNN45O9brr0h+s//pvMxy7y15GVkBU6UJO" +
            "7WECggEAYoIqTyuz/P27Q26akoaqkUEim16cjCeM9w65fZ2h7rwDJrmLLQE5/ouV" +
            "wVp4eI0J4VOUPtygteUJGpfbD5bPOhMhXyTxEMD0tZpU9HAOwK/928cGBFPKrUvq" +
            "jdm9aSKV9MWJuM8bF3/s7iWRBeo4rhG2vRiK+r2GpoY+G0xNM4NZGiVE0iV66HlY" +
            "IHBoR0RrRF9UM1Afg7F0BpQNa1MurC8+CEhbPrfzPYI4FXnyXO5+Q8f94cH7uo6y" +
            "cQ3JcaoNK68QHsx2N1dTh0cha48E300w207Ar5KVPUlKWac1zhvJ/gz219tZ0lPq" +
            "UodCAcmTEvEIgUAopw8FE+Wpjw+3Xw==";

    public static PublicKey publicKey(String publicKeyStr) {
        byte[] keyBytes;
        if (StringUtils.isBlank(publicKeyStr)) {
            publicKeyStr = RSAUtils.DEFAULT_PUBLIC_KEY;
        }
        keyBytes = Base64.decodeBase64(publicKeyStr.getBytes(StandardCharsets.UTF_8));
        return publicKey(keyBytes);
    }

    public static PrivateKey privateKey(String privateKeyStr) {
        byte[] keyBytes;
        if (StringUtils.isBlank(privateKeyStr)) {
            privateKeyStr = RSAUtils.DEFAULT_PRIVATE_KEY;
        }
        keyBytes = Base64.decodeBase64(privateKeyStr.getBytes(StandardCharsets.UTF_8));
        return privateKey(keyBytes);
    }

    private static KeyFactory keyFactory() {
        KeyFactory keyFactory;
        try {
            keyFactory = KeyFactory.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            throw new SecurityException("无此算法");
        }
        return keyFactory;
    }

    private static PrivateKey privateKey(byte[] keyBytes) {
        PrivateKey privateKey;
        try {
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
            privateKey = keyFactory().generatePrivate(pkcs8EncodedKeySpec);
        } catch (InvalidKeySpecException e) {
            throw new SecurityException("key参数无效");
        }
        return privateKey;
    }

    private static PublicKey publicKey(byte[] keyBytes) {
        PublicKey publicKey;
        try {
            X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(keyBytes);
            publicKey = keyFactory().generatePublic(encodedKeySpec);
        } catch (InvalidKeySpecException e) {
            throw new SecurityException("key参数无效");
        }
        return publicKey;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy