package com.qmth.teachcloud.common.util;

import com.qmth.teachcloud.common.contant.SystemConstant;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSAUtils {

    private static final String ALGORITHM_NAME = "RSA";

    public static RSAPrivateKey buildPrivateKey(String key)
            throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeySpecException {
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_NAME);
        PKCS8EncodedKeySpec pkcs8eks = new PKCS8EncodedKeySpec(Base64Util.decode(key));
        return (RSAPrivateKey) keyFactory.generatePrivate(pkcs8eks);
    }

    public static RSAPublicKey buildPublicKey(String key)
            throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeySpecException {
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_NAME);
        X509EncodedKeySpec x509eks = new X509EncodedKeySpec(Base64Util.decode(key));
        return (RSAPublicKey) keyFactory.generatePublic(x509eks);
    }

    public static String encrypt(String input, RSAPrivateKey privateKey)
            throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidKeyException,
            IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return Base64Util.encode(cipher.doFinal(input.getBytes(SystemConstant.CHARSET_NAME)));
    }

    public static String encrypt(byte[] input, RSAPrivateKey privateKey)
            throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidKeyException,
            IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return Base64Util.encode(cipher.doFinal(input));
    }

    public static String decrypt(String input, RSAPublicKey publicKey)
            throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
            UnsupportedEncodingException {
        Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        return new String(cipher.doFinal(Base64Util.decode(input)), SystemConstant.CHARSET_NAME);
    }

    public static byte[] decrypt2byte(String input, RSAPublicKey publicKey)
            throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
            UnsupportedEncodingException {
        Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        return cipher.doFinal(Base64Util.decode(input));
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        // RSA加密算法
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM_NAME);
        // 创建密钥对，长度采用1024
        keyPairGenerator.initialize(1024);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        // 分别得到公钥和私钥
        String publicKey = Base64Util.encode(((RSAPublicKey) keyPair.getPublic()).getEncoded());
        String privateKey = Base64Util.encode(((RSAPrivateKey) keyPair.getPrivate()).getEncoded());
        System.out.println("publicKey:" + publicKey);
        System.out.println("privateKey:" + privateKey);
    }
}
