package com.m2pool.common.core.utils.sign;


import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * @Description TODO
 * @Date 2024/6/12 17:35
 * @Author dy
 */
public class RsaUtils {

    // Rsa 私钥
    //public static String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDTla3smq/59AVL\n" +
    //        "g0PayphcAyefuJlgJOHF7nN9eOpnbtDdsgLQ0giAoByujHcp7mZv6EngPmlgVudF\n" +
    //        "1pELpfXrhAMwszE13aOBWtJXAlsQgloE14sjNxLpnOjNUQyAiq44F+6vC3yKXTvR\n" +
    //        "4VzOgEuIVed8uWZOnHR9J6bilMEshE+4zR9BAB4PjX0+7+nTrsS5gEw7LX1zql/R\n" +
    //        "BCdRVt09mMO9lvxYtde2kqEBZP/2joKMmSJ5OaSOyU/jv1+T8iZFduYAvCgHJWlj\n" +
    //        "6Rfo1NfqAETLney+LtquULUgr4+n2df9FtSQtGhcfaF7p2B3kX/xAJXh4cwnoJ4u\n" +
    //        "5Q5FXHYVAgMBAAECggEAMEj8Q/6ZIkWZ3725Ankxg+4EYOLTvaktDOp52Kx7cddM\n" +
    //        "OwugsN79qbzgTsUnfJ43KlqsYUxc5+ttI/bvauUY1gJuZ/K8zDokUiTT059qAL5+\n" +
    //        "pJQ74HF1E3MHfbN9UuhTEeIESlYoubrFKARyFX4Zvqc8CK6WtmHmA5nE7/hajTnK\n" +
    //        "aToKPLP8KXa/U8W8paKUhUV1aoEBtqxOLQs1rg8PREGCUmUyTRzzxblm/SVd6YYB\n" +
    //        "vCKcbZgKQwt7lrKrUVcVc+VDrJ6UWU+XNMABWT+NwUvrTayQehot5lxfZxfPk0IG\n" +
    //        "h9OhOtclOQ8FfRh2UeLmkAG4PTadr+r7Lrbcm9NokQKBgQDtKbNcvJIuXKqecVxA\n" +
    //        "+QiINx/PPp44wFhZk9iIHh02MVRNtgxEIMpUleyIHlEGMmTKcchO56GRXiFfqNu5\n" +
    //        "8DduoU4tbKztl1YtYctMRWvYDvcNcveEhm/+J01e/Zyej4ElLjTZeg+rOSREhkMr\n" +
    //        "EKDSA6AoltjpcCThYxG+Rtr0SwKBgQDkY+PRDc5BWw+icKXu6DgajUw+i3pzVRMW\n" +
    //        "lF/Q4sNi7zlTP+YcnuhqgHu0ILFUgTQAgbdqVKZjG3g9bI7Ziaus10H+LXTizNue\n" +
    //        "QNibe/UZ29Q2SsDeATMwDGrUQ2d0FuGaVWJoY4auNWuZr+6XaEnih2x5FniktdQk\n" +
    //        "+YbYinQDHwKBgFBCs4un2YTNIYS7cnAel0+Z8C7vzxX/qiaujTILlvE3IoOmH2KT\n" +
    //        "AkY78q9iKyOAvHFyrkpdw2TxyTOZbrrvW6MZ/d4LkD5b3/M9zFJEkCmvbtZjWPbF\n" +
    //        "lHMbk+iYxX83q4oMqCANWe4lSWvTUDnrx7ErPvFdk4z0wdZw85lEW2cfAoGBAICn\n" +
    //        "3I/JTST33QjOmErKucALVKXvAF2z0PrpPkh3VUWIKSzCVChPQ/GqywSfXgWSeu7G\n" +
    //        "I8JcSRaPRN6lJptYuEK3R8+dX7jbWeP994cu/tVARn0HAzqMRn+MnylPhxmYQiIk\n" +
    //        "czkGx7mfEiwTNT5JW0Wmr+5OQEvYudbSUant5IhVAoGBAM0rAsaQ2ZQEeUCZGuu1\n" +
    //        "IfW21gwl13G4mWt2ng1rV8iv8Kn9yu0o7CRGAf4IlUYqOcJtfh8F5liQBcJzd2Bc\n" +
    //        "odT7Zo+P4Zqtgg9nKehkd5Z7+zmFNog3Z/lZMj/dJuwO+Q8Ef6qKMYdPQuwrsVBD\n" +
    //        "RY91AS8cjE/RsmNeWNJjTkq5";

    public static String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwgg" +
            "JcAgEAAoGBALAeRR9PYYC6lkAvkMyfARNLLC48sgpQBHeGOEq86Wza7Ri0geVx+O" +
            "GoYF+DQ0Q+LoiDYaYrkqeNx0iml818GlKIkES0zAzYTtYltcGxhTQHjAHzpcZb18" +
            "VCLR7ykZi9Vsq/A/0xB4udQLjF/Yv7o3DfFBn0YCwToZQni1Q5Ior9AgMBAAECgY" +
            "BZ+f0CdAGyTKE6hZKKl6lq+/rEXInxLtQ5ZD8aH815qZd8CaxbVpD6aJVj9qHFGa" +
            "eYYtbemGBCbfKkMUhY2NBxAYJ8WVbfHPav4WoLk4lerW2/+6w/sr/MSoMd3D1gNV" +
            "Zkt3L61/d2lJqWiD94L+fEV4fj6sC+IayAegb/TpXOWQJBAP5wlmxDlnEmKxMi9r" +
            "PxezlsxCTpyAWtZd02xIxfIB0KlC0TqjdiJr+IbC/fS1U9OBuNJX3Wfecc8PuD6I" +
            "95F08CQQCxMrxG04qySfuNcY5oWjkcz3QNTn5l1AMLeq982aNBFhxBDCcoLToUQb" +
            "+a285sz4y8hZFOXHiWl6yt79SVPSXzAkBl+YWwGl5/NsbowoiRkeTLHRZ3nOK3s8" +
            "kxobOgdreOLCE697iuvb294dVKUnoEzaZhFG7EQmTvbZ//jdx1NBjtAkEApb1R02" +
            "F8IkUG0Aa976csMmFsKCIaIb6LVopbaAjdJTy8Eq9+VJrw7w+MWxlrW+VJuu+nuM" +
            "bPV/PoZQylJy35twJAEayAR+lE4G5LTY8Oonr4LBYgJebviJFto9gDNbJkpiYlpN" +
            "tXT0At+fI61B7n3Wvm4piv+8FBPxNKXZbliGD4tw==";

    /**
     * 私钥解密
     *
     * @param text 待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String text) throws Exception
    {
        return decryptByPrivateKey(privateKey, text);
    }

    /**
     * 公钥解密
     *
     * @param publicKeyString 公钥
     * @param text 待解密的信息
     * @return 解密后的文本
     */
    public static String decryptByPublicKey(String publicKeyString, String text) throws Exception
    {
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(Base64.decodeBase64(text));
        return new String(result);
    }

    /**
     * 私钥加密
     *
     * @param privateKeyString 私钥
     * @param text 待加密的信息
     * @return 加密后的文本
     */
    public static String encryptByPrivateKey(String privateKeyString, String text) throws Exception
    {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Base64.encodeBase64String(result);
    }

    /**
     * 私钥解密
     *
     * @param privateKeyString 私钥
     * @param text 待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String privateKeyString, String text) throws Exception
    {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(Base64.decodeBase64(text));
        return new String(result);
    }

    /**
     * 公钥加密
     *
     * @param publicKeyString 公钥
     * @param text 待加密的文本
     * @return 加密后的文本
     */
    public static String encryptByPublicKey(String publicKeyString, String text) throws Exception
    {
        X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Base64.encodeBase64String(result);
    }

    /**
     * 构建RSA密钥对
     *
     * @return 生成后的公私钥信息
     */
    public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException
    {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
        String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
        String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
        return new RsaKeyPair(publicKeyString, privateKeyString);
    }

    /**
     * RSA密钥对对象
     */
    public static class RsaKeyPair
    {
        private final String publicKey;
        private final String privateKey;

        public RsaKeyPair(String publicKey, String privateKey)
        {
            this.publicKey = publicKey;
            this.privateKey = privateKey;
        }

        public String getPublicKey()
        {
            return publicKey;
        }

        public String getPrivateKey()
        {
            return privateKey;
        }
    }

}
