package com.m2pool.lease.utils;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class RsaUtils {
    public static final String PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsQVIKYozXCfnXUw8+omYLdcdL1pTzmQh35YPsvn22wM4SQJKvMmXmcS6bI5Bu+5zCjL0F56DzfKz0BNZEwb46UshUOO+KFBUr8CxjYE8NOgIsoe5FUn57O6er9/KySaWlkpGZX49K+l3e90R+dFUEfRE/ijYpeZWkLRwcgWZ+2u6HGpl9h/eF6XD0aW9asDjdAbxUQ48TlaWgfP+OHC+Zy2GKGQG16EcDMczrN6a2HbFnwRIUKrFP67UqyRq11BTUziOhXLY8J0MFuwXUk2OY4VpqjrJjHHjlHYADjIL/5K4Io2AhRU9+QSsKFR2wGxi4e8vw2IXDzscrDuah/7YSwIDAQAB"; // 可公开
    private static final String PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCxBUgpijNcJ+ddTDz6iZgt1x0vWlPOZCHflg+y+fbbAzhJAkq8yZeZxLpsjkG77nMKMvQXnoPN8rPQE1kTBvjpSyFQ474oUFSvwLGNgTw06Aiyh7kVSfns7p6v38rJJpaWSkZlfj0r6Xd73RH50VQR9ET+KNil5laQtHByBZn7a7ocamX2H94XpcPRpb1qwON0BvFRDjxOVpaB8/44cL5nLYYoZAbXoRwMxzOs3prYdsWfBEhQqsU/rtSrJGrXUFNTOI6FctjwnQwW7BdSTY5jhWmqOsmMceOUdgAOMgv/krgijYCFFT35BKwoVHbAbGLh7y/DYhcPOxysO5qH/thLAgMBAAECggEAYE00TczuVfMrbdkcz1ttW3rMWMd2EjDtMJqNXduYjNs6kpVZnUy6WZ/pn0bgweFd2i2QFw1YPQENC2SLL8u6PAhwTnYnK/1PpPFaZlXpMAsPODiX2toMyfWih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pxVJKMUVqx+2LFDFqHaaTAN"; // 这里替换为你的实际私钥

    public static String decrypt(String encryptedData) throws Exception {
        // Base64解码加密数据
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);

        // 加载私钥
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(PRIVATE_KEY));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

        // 初始化Cipher进行解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        // 执行解密
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }
}
