package cn.pluss.platform.klk.util;

import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSAUtil {
    public static final String KEY_ALGORITHM = "RSA";
    private static final String PUBLIC_KEY = "RSAPublicKey";
    private static final String PRIVATE_KEY = "RSAPrivateKey";
    private static final int KEY_SIZE = 1024;
    /**
     * RSA
     */
    private static final int MAX_ENCRYPT_BLOCK = 117;
    /**
     * RSA
     */
    private static final int MAX_DECRYPT_BLOCK = 128;
    public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        int inputLen = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        //
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return encryptedData;
    }
    public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception {
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        int inputLen = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        //
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }
        byte[] dencryptedData = out.toByteArray();
        out.close();
        return dencryptedData;
    }

    public static void main(String[] args) throws Exception {
        String req="{\"customerNo\":\"123676959\"}";
        String pubKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHB7j7gG3ioLnnr7bsuEXTAh/8YSxSp4lQIYGW9gX0Ikgz9JqARdg4iEnU6tgNirxA6Jdg0AWgLJQxQBEZMkwyK2ZfYgesYhlJkv6WVC8v5OkOrhA9NSQ3iS6JsYegsZO0GJSTtLQaTOF8WobPYe5NI+eWU1fRz2ZyxlWlKshBeQIDAQAB";
        String priKey="MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIcHuPuAbeKgueevtuy4RdMCH/xhLFKniVAhgZb2BfQiSDP0moBF2DiISdTq2A2KvEDol2DQBaAslDFAERkyTDIrZl9iB6xiGUmS/pZULy/k6Q6uED01JDeJLomxh6Cxk7QYlJO0tBpM4Xxahs9h7k0j55ZTV9HPZnLGVaUqyEF5AgMBAAECgYAmugRHvXHGiapH1JF2GvOLrRRXYddAbWRoYvOzxCO4/QwQOpsFJwL4U745HpGQcLXFN/ASiBbU9XQbG/DLdS182qAjvdZVJOA2e3H3LhpY3D8ec3webO+0nGk6e4oWGG1efJLM88Nb0FtQiQ0E6z17Paw5AGvxMVBPbL5SoQfq8QJBANHQmvFkAWlBL2XTLPUXaj+NAJzolE90ctAquxfDcTtfEXv0JitC5bwT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kqrzaeiX9s6kKb8CCfv/kqdPw0Vl36ogGNLaDsJDusoFsq4+HGQo6yCcsYregtDkQfMALefosHyKRJZka9Bx1FtEd9Cbwc8mEkbcPS1qquELX1V4/EsIra9boaVRk1bV7K7UPM90Md+P5ItwmK2rytNIl7sWF0oBPl0SpStgJKjmSfO5mIZXEfBwm+bIfC2d+yxjR1qvgjJa71vm/5OYvLOWIrp9Ea/ENV2uMSXeMf57FfizxVbAUCP5g9eFRkeIYFZXRd0UeorDbvakIEojoYopg6tuu0+/ikvHsPApmNiTQBMLC5oMvxLUDa1OZe8tSFtRXKEBs7OfCDoiYt9DNMtShh2iMT/SrAWIQT5lC/hu1MlkfwlLU+4Q6DWbQzmeenRPuF8cpIhraNj5VLJr36zwOKuoPaCNzaMoa23KfXh4k7o3ZSrusPrxPZWl+XzeL+EovtStxwYJw3T5ADI28TuOGAMrxX8Tu+/zT4iH8e8YON15sKNPG83gTqtF3oY/vFbLGCFabI0uOouydS/+ScMLRhbYiHjjUL++DZQ8vjGAvY7KJVkCKHJnI1eNc64R471qew6b8D0tR+aWu2KGCR5ww0b1NNXSoF5neG0XaVwBoXfIDT9CJfLcdT4XU1KJPl21Oiy8i8BH60HAscGnWWCQFMrHTjoRsUr03F7fpD6MtJF9nrJzfCh4sAAA8G5Q9BtG73MBnq0ipZvMN0pMrGM0OKUJ5GWfNynutCEwsk54iEq/Fxrjltbjqylc4u1gznGzDjB4FTN4cJllH3Fy7uPsTCqBM9k7UkqW4NLKLnt8RDt2LV0/boPDs5mnf23iS6N/2IY5GwHVNCR9/P2uxG2wsbbEOwtWJDcHbHTaSzgXB5Zou0ACfRDGUN0+8tHa5J9YQ0B0Nte2hp3vm0vjULKxxStPLbf/jlvBVMUf84m3Kafye6bpnqYKw7HouLPh5I8dC7tthMhO2sQ0=";

        byte[] privateKey = Base64.decodeBase64(priKey);
        byte[] publicKey = Base64.decodeBase64(pubKey);
        byte[] bytes = encryptByPrivateKey(req.getBytes(), privateKey);
        String encrypt = Base64.encodeBase64String(bytes);
        byte[] bytes1 = decryptByPublicKey(Base64.decodeBase64(data), publicKey);

        System.out.println("加密后的数据:======"+encrypt);

        System.out.println("解密后的数据:====="+new String(bytes1));



    }
}
