package com.fdkankan.common.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.time.Instant;

public class RsaUtil {

    private RSA rsa;

    public static RsaUtil create(String privateKeyStr, String publicKeyStr){
        return new RsaUtil(privateKeyStr, publicKeyStr);
    }

    public RsaUtil(String privateKeyStr, String publicKeyStr){
        this.rsa = new RSA(privateKeyStr, publicKeyStr);
    }

    public String encryptByPublicKey(String data){
        byte[] encrypt = this.rsa.encrypt(StrUtil.bytes(data, StandardCharsets.UTF_8), KeyType.PublicKey);
        return  Base64.encode(encrypt);
    }

    public String encryptByPrivateKey(String data){
        byte[] encrypt = this.rsa.encrypt(StrUtil.bytes(data, CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
        return  Base64.encode(encrypt);
    }

    public String decryptByPublicKey(String data){
        byte[] decrypt = this.rsa.decrypt(Base64.decode(data), KeyType.PublicKey);
        return new String(decrypt, StandardCharsets.UTF_8);
    }

    public String decryptByPrivateKey(String data){
        byte[] decrypt = this.rsa.decrypt(Base64.decode(data), KeyType.PrivateKey);
        return new String(decrypt, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) throws Exception {

//        RSA rsa1 = new RSA();
//        System.out.println(rsa1.getPrivateKeyBase64());
//        System.out.println(rsa1.getPublicKeyBase64());

        String data =  "test-" + Instant.now().getEpochSecond();
        String priateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKSawZbtFehoZ3IsN3bXgwR0qV/GmnFkEfA+xiIenX+YNR4IprBQHyT0HddguZIrl7bUA4t6nYXShldBpw2YBlXMIxsgOlO/oPaFEgclV7l33CQIC4pMzPJuKudTM1Nou+uZZwDafU+ap99oZ0at5tvOXmokLlYT5HSWgZ7zJJ0LAgMBAAECgYBcc1Mx+r1oSGN5m94XEJpUwVV0Jq8N6t+saQWNmlZpmMqUsNU2h/nGHpo4Iup3b6aJmHDZQSgCRqkPjx9NokpDtYFjksAQOgNdHR+y7pczhHHdsdiER2qzSQnQG888Fr1gYKzgncL8PK9xgnoYX4i+SUzNGnU2US2mu6kpw/QyWQJBANC3KqH27mBpvz8RKQ5ZQxPamdhAJ2iOfB7pgcRA1Bgg/3t4UWbd/Zfj/1shW0GV3i1ICStmc5r/BEYgEYkCX7UCQQDJ5Usl+W5/Ak9I+XJEaHXLhNbBG2aQbny814W/d/mWdo4EkPzshVD4SVR0ioJU1gmnYYQQY+WSRkyT9G9ssYG/AkBq+vfnPOLCmHFmqi0ZJbe0HYBtz7Rja2VDkvzo0zHjjEZnyywuResOR9dDpr1XyLGxFBNfI4xuDfjq2NUkHQthAkEAuXROW0uMHE7a1YWNWRk7Wv9i5W9t/y3+ltjCmuZx9axhOACW1AyCHA//7C1vGs6hf4cr8IJS1tKMdTwLPiSvsQJAOh6deTVm4y1bnSGpoE6w0RspAwFd80lh+BqOc481qr2mCZvdzwA+DULrSLjhfsKSuIWl2L+FHivwDCQd4aV9+w==";
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCkmsGW7RXoaGdyLDd214MEdKlfxppxZBHwPsYiHp1/mDUeCKawUB8k9B3XYLmSK5e21AOLep2F0oZXQacNmAZVzCMbIDpTv6D2hRIHJVe5d9wkCAuKTMzybirnUzNTaLvrmWcA2n1PmqffaGdGrebbzl5qJC5WE+R0loGe8ySdCwIDAQAB";
        RsaUtil rsaUtil = RsaUtil.create(priateKey, publicKey);
        String encrypt = rsaUtil.encryptByPublicKey(data);
        String decrypt = rsaUtil.decryptByPrivateKey(encrypt);
        System.out.println(encrypt);
        System.out.println(decrypt);

        String encrypt1 = rsaUtil.encryptByPrivateKey(data);
        String decrypt1 = rsaUtil.decryptByPublicKey(encrypt1);
        System.out.println(encrypt1);
        System.out.println(decrypt1);

    }

}
