package com.gree.mall.miniapp.utils;


import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.Security;
import java.security.interfaces.RSAPublicKey;

public class JkRSA {

    static{
        try{
            Security.addProvider(new BouncyCastleProvider());
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static final String CHARSET = "UTF-8";
    public static final String RSA_ALGORITHM = "RSA";

    public static void main(String[] args) throws Exception {
        /*String publicKeyString = "-----BEGIN PUBLIC KEY-----" +
            "\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuclAHrSV1zsj88gtgmnP9wMWc" +
            "\nRSim/PcL+VEHXKo2WCeFzi8xEILShFa46dam3LlZIYejggd1gifwlYYDSJTgfX9a" +
            "\nS9dVoIU9ibhYbWoonRoUq0nnhz98OFxYYC2Li4TnZnyI9V4cbTedt3ABqM9SuGIb" +
            "\nbK1Y+QBeDrIbIJZ4DwIDAQAB" +
            "\n-----END PUBLIC KEY-----";
        Reader publicKeyReader = new StringReader(publicKeyString);
        PEMParser publicPemParser = new PEMParser(publicKeyReader);
        Object publicObject = publicPemParser.readObject();
        SubjectPublicKeyInfo publicSubjectPublicKeyInfo = (SubjectPublicKeyInfo)publicObject;
        publicPemParser.close();

        JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
        PublicKey publicKey = converter.getPublicKey(publicSubjectPublicKeyInfo);

//        String message = "{\"source\":\"jkmallhaiyin_outlets\",\"userKey\":\"ee6d08caeb4cb429acac3bd03c0f38fa\",\"code\":\"1234567\"}";
        String message = "{\"source\":\"test\",\"userKey\":\"16d05e12ae46028eec9e3de90a894188\",\"code\":\"12345677545\"}";
//        message = message + message+ message;
        byte[] encripted = encrypt((RSAPublicKey) publicKey, message);
        BASE64Encoder encoder = new BASE64Encoder();
        String encodedText = encoder.encode(encripted);
        System.out.println(encodedText);*/
    }

    public static byte[] encrypt(RSAPublicKey pubkey, String text) {
        try {
            Cipher rsa;
            rsa = Cipher.getInstance("RSA");
            rsa.init(Cipher.ENCRYPT_MODE, pubkey);
            return rsaSplitCodec(rsa, text.getBytes("UTF-8"), pubkey.getModulus().bitLength());
//            return rsa.doFinal(text.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private  static  byte[] rsaSplitCodec(Cipher cipher, byte[] datas, int keySize) throws Exception {
        int maxBlock = keySize / 8 - 11;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offset = 0;
        byte[] buff;
        int i = 0;
        while (datas.length > offset) {
            if (datas.length - offset > maxBlock) {
                buff = cipher.doFinal(datas, offset, maxBlock);
            } else {
                buff = cipher.doFinal(datas, offset, datas.length - offset);
            }
            out.write(buff, 0, buff.length);
            i++;
            offset = i * maxBlock;
        }
        byte[] resultDatas = out.toByteArray();
        out.close();
        return  resultDatas;
    }








}