package com.rehome.mqttclienttemperature;

/**
 * @ Author  : huangwenfei
 * @ Date    : Created in 2021/8/11 10:44 下午
 * @ Version : $1.0.0.0
 * @ Description:
 */

import org.apache.commons.codec.binary.Base64;

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.util.ArrayList;
import java.util.List;


public class RSAAndroid {
    private static String TAG = "RSAAndroid";
    public static final String RSA = "RSA";// 非对称加密密钥算法
    //public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";//加密填充方式
    public static final String ECB_PKCS1_PADDING = "RSA";//加密填充方式 RSA/None/PKCS1Padding
    public static final int DEFAULT_KEY_SIZE = 2048;//秘钥默认长度
    public static final byte[] DEFAULT_SPLIT = "#PART#".getBytes();    // 当要加密的内容超过bufferSize，则采用partSplit进行分块加密
    public static final int DEFAULT_BUFFERSIZE = (DEFAULT_KEY_SIZE / 8) - 11;// 当前秘钥支持加密的最大字节数
    public static String publicRsaKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmMLyJw1CAl25lnDgEeYZvOps+1pSi93Q39djEniGNo5uUKVEkqDIayTli2zreX10HqT2jTtDN9APtwuEhWazP/VgOXoWsztbtZtSwJGM6Eg0R9zDCbKyQt5Qhg3jkTrXrvrGn7j/ZP56VNWELv/i5dsRCTccr1MeIyxjOC2pojCOsrTN4HZzgBj+GEUKPRLcKOiPfOsoP7HgkAua82vTOIgWpqIp+1PIfcjjCqzOsSv5PQnGP75+flIXtz75OKo/9hX9zl5JHNcH3SC6nS8Czii9E292XIsBtKdQijvNMn+YcmKFo6mZOUXHdO506NoKkxRny5fbKiPf/oqTA7Zx5QIDAQAB";
    public static String privateRsaKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCYwvInDUICXbmWcOAR5hm86mz7WlKL3dDf12MSeIY2jm5QpUSSoMhrJOWLbOt5fXQepPaNO0M30A+3C4SFZrM/9WA5ehazO1u1m1LAkYzoSDRH3MMJsrJC3lCGDeOROteu+safuP9k/npU1YQu/+Ll2xEJNxyvUx4jLGM4LamiMI6ytM3gdnOAGP4YRQo9Etwo6I986yg/seCQC5rza9M4iBamoin7U8h9yOMKrM6xK/k9CcY/vn5+Uhe3Pvk4qj/2Ff3OXkkc1wfdILqdLwLOKL0Tb3ZciwG0p1CKO80yf5hyYoWjqZk5Rcd07nTo2gqTFGfLl9sqI9/+ipMDtnHlAgMBAAECggEATJd5yCC6lusdMRO5FOBUyUaUi9X2i1AU+RZKAynQySvSnbavUgExW58tRCHBUrGW9gJp59ft1N8J8hHhSO18NDY4H7laBlVdnwmYjRqtFo2VQO6sD4G8JRDION5f2iIxn/b2fYDI9H8vILfJRbNgtTSILyGlzTYUZzhLKxCh+8IsN96Nic8wa5COd1vZZmdhf2y8TG8clFWmozaScNSAATx7y+8XLVWjjWiIRZ6xQvx0uQPUParc9KihXXTKR2pA22yPIdz+U4MGD4kC0eczlcFKZ/dYv9e7OIGgnJfT0idSCu7nYb1pxJ1LxD9fS6IScNTF5dSe0OIL98e+XdyoAQKBgQDRep+5cW4iAKrEMH+djmcXAkoMiYtNVtnu0efLE8dP6vjYytQi368X9SdcASbfrQ31eEZmr/xQnlUF8oyHGkI38YS8dpAHzQcrkP3BljbbzB/3gJZaUdghGsDrK0xAJIzzmFKQpeKnGtr23vxUgaGrNsCYvQ0eQ7+5056KXS4r5QKBgQC6r8xtRSaje6L4WIydjWvYywsmRO0Of0aJLMDA/Wt2MWhHfh7ba9oI1cKGN80ap7xB2a9lQLgpv+C53wNtE5SpvjxsikAj96nUMMhGy9ojXrUith6HQhiINETz6Shnznd+AyrXP6KI/RpfA5nkDB5nrJxODwtYLP467IL7Cv7OAQKBgQCl4KxKdH/5fP28jYsAgJsxpSZt9xzQCU5Zxu396ZOSvUaApVyGoQpNtluMh3z48lhzYOKevgzW6gn5w69z7F8zXZT2iAxVoQ1kelP2z7RxKJrHqpNkwhqbXEwX7RlcUZUr8BqxYCqymJl7k+fMIzqaEalBSbLxnEReKi0I8/Bz4QKBgHK4b0ZCtVDHPEmimJ6E9l4dv/c/afF7swu+zaCK2ouiJvOwBCRQbYb6XPR/u/GCXASXUdpF4CX/vIhcDE3uN2/r8FO+zVWM7vbvF1OyF5WesG7pPW9e5ZZlkG3WvLa1wOZV6fCmMSo/ZwI2Q05JSDHrd43cXttLotrw1jiQ9C4BAoGBAKi4SOoOVQ5J5HQCDkBwPbG1AOLHFinzfoDl26GF/8Hy7fmmd1JiRTFldQp/A9VTAABz3sVYmMB92HSIaJhuDMoYJNI2Cf/cZifsv7vUL8cbLn+lPsKsebiuB0m0g4P2qLwLfegfNGEgA7lA5HIz3SELqbdp3iuqJeQl1fsJqD74";


    public static byte[] decryptBASE64(String key) throws Exception {
        return Base64.decodeBase64(key);
    }

    public static String encryptBASE64(byte[] key) throws Exception {
        return Base64.encodeBase64String(key);
    }

    /**
     * 随机生成RSA密钥对
     *
     * @param keyLength 密钥长度，范围：512～2048
     *                  一般1024
     * @return
     */
    public static KeyPair generateRSAKeyPair(int keyLength) {
        try {
            KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
            kpg.initialize(keyLength);
            return kpg.genKeyPair();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 用公钥对字符串进行加密
     *
     * @param data 原文
     */
    public static byte[] encryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {
        // 得到公钥
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey keyPublic = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        // 加密数据
        Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);
        cp.init(Cipher.ENCRYPT_MODE, keyPublic);
        return cp.doFinal(data);
    }

    /**
     * 私钥加密
     *
     * @param data       待加密数据
     * @param privateKey 密钥
     * @return byte[] 加密数据
     */
    public static byte[] encryptByPrivateKey(byte[] data, byte[] privateKey) throws Exception {
        // 得到私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey keyPrivate = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        // 数据加密
        Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, keyPrivate);
        return cipher.doFinal(data);
    }

    /**
     * 公钥解密
     *
     * @param data      待解密数据
     * @param publicKey 密钥
     * @return byte[] 解密数据
     */
    public static byte[] decryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {
        // 得到公钥
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey keyPublic = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        // 数据解密
        Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, keyPublic);
        return cipher.doFinal(data);
    }

    /**
     * 使用私钥进行解密
     */
    public static byte[] decryptByPrivateKey(byte[] encrypted, byte[] privateKey) throws Exception {
        // 得到私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey keyPrivate = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));

        // 解密数据
        Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);
        cp.init(Cipher.DECRYPT_MODE, keyPrivate);
        byte[] arr = cp.doFinal(encrypted);
        return arr;
    }

    /**
     * 用公钥对字符串进行分段加密
     */
    public static byte[] encryptByPublicKeyForSpilt(byte[] data, byte[] publicKey) throws Exception {
        int dataLen = data.length;
        if (dataLen <= DEFAULT_BUFFERSIZE) {
            return encryptByPublicKey(data, publicKey);
        }
        List<Byte> allBytes = new ArrayList<Byte>(2048);
        int bufIndex = 0;
        int subDataLoop = 0;
        byte[] buf = new byte[DEFAULT_BUFFERSIZE];
        for (int i = 0; i < dataLen; i++) {
            buf[bufIndex] = data[i];
            if (++bufIndex == DEFAULT_BUFFERSIZE || i == dataLen - 1) {
                subDataLoop++;
                if (subDataLoop != 1) {
                    for (byte b : DEFAULT_SPLIT) {
                        allBytes.add(b);
                    }
                }
                byte[] encryptBytes = encryptByPublicKey(buf, publicKey);
                for (byte b : encryptBytes) {
                    allBytes.add(b);
                }
                bufIndex = 0;
                if (i == dataLen - 1) {
                    buf = null;
                } else {
                    buf = new byte[Math.min(DEFAULT_BUFFERSIZE, dataLen - i - 1)];
                }
            }
        }
        byte[] bytes = new byte[allBytes.size()];
        {
            int i = 0;
            for (Byte b : allBytes) {
                bytes[i++] = b.byteValue();
            }
        }
        return bytes;
    }



    /**
     * 使用私钥分段加密
     *
     * @param data       要加密的原始数据
     * @param privateKey 秘钥
     */
    public static byte[] encryptByPrivateKeyForSpilt(byte[] data, byte[] privateKey) throws Exception {
        int dataLen = data.length;
        if (dataLen <= DEFAULT_BUFFERSIZE) {
            return encryptByPrivateKey(data, privateKey);
        }
        List<Byte> allBytes = new ArrayList<Byte>(2048);
        int bufIndex = 0;
        int subDataLoop = 0;
        byte[] buf = new byte[DEFAULT_BUFFERSIZE];
        for (int i = 0; i < dataLen; i++) {
            buf[bufIndex] = data[i];
            if (++bufIndex == DEFAULT_BUFFERSIZE || i == dataLen - 1) {
                subDataLoop++;
                if (subDataLoop != 1) {
                    for (byte b : DEFAULT_SPLIT) {
                        allBytes.add(b);
                    }
                }
                byte[] encryptBytes = encryptByPrivateKey(buf, privateKey);
                for (byte b : encryptBytes) {
                    allBytes.add(b);
                }
                bufIndex = 0;
                if (i == dataLen - 1) {
                    buf = null;
                } else {
                    buf = new byte[Math.min(DEFAULT_BUFFERSIZE, dataLen - i - 1)];
                }
            }
        }
        byte[] bytes = new byte[allBytes.size()];
        {
            int i = 0;
            for (Byte b : allBytes) {
                bytes[i++] = b.byteValue();
            }
        }
        return bytes;
    }

    /**
     * 公钥分段解密
     *
     * @param encrypted 待解密数据
     * @param publicKey 密钥
     */
    public static byte[] decryptByPublicKeyForSpilt(byte[] encrypted, byte[] publicKey) throws Exception {
        int splitLen = DEFAULT_SPLIT.length;
        if (splitLen <= 0) {
            return decryptByPublicKey(encrypted, publicKey);
        }
        int dataLen = encrypted.length;
        List<Byte> allBytes = new ArrayList<Byte>(1024);
        int latestStartIndex = 0;
        for (int i = 0; i < dataLen; i++) {
            byte bt = encrypted[i];
            boolean isMatchSplit = false;
            if (i == dataLen - 1) {
                // 到data的最后了
                byte[] part = new byte[dataLen - latestStartIndex];
                System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
                byte[] decryptPart = decryptByPublicKey(part, publicKey);
                for (byte b : decryptPart) {
                    allBytes.add(b);
                }
                latestStartIndex = i + splitLen;
                i = latestStartIndex - 1;
            } else if (bt == DEFAULT_SPLIT[0]) {
                // 这个是以split[0]开头
                if (splitLen > 1) {
                    if (i + splitLen < dataLen) {
                        // 没有超出data的范围
                        for (int j = 1; j < splitLen; j++) {
                            if (DEFAULT_SPLIT[j] != encrypted[i + j]) {
                                break;
                            }
                            if (j == splitLen - 1) {
                                // 验证到split的最后一位，都没有break，则表明已经确认是split段
                                isMatchSplit = true;
                            }
                        }
                    }
                } else {
                    // split只有一位，则已经匹配了
                    isMatchSplit = true;
                }
            }
            if (isMatchSplit) {
                byte[] part = new byte[i - latestStartIndex];
                System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
                byte[] decryptPart = decryptByPublicKey(part, publicKey);
                for (byte b : decryptPart) {
                    allBytes.add(b);
                }
                latestStartIndex = i + splitLen;
                i = latestStartIndex - 1;
            }
        }
        byte[] bytes = new byte[allBytes.size()];
        {
            int i = 0;
            for (Byte b : allBytes) {
                bytes[i++] = b.byteValue();
            }
        }
        return bytes;
    }

    /**
     * 使用私钥分段解密
     */
    public static byte[] decryptByPrivateKeyForSpilt(byte[] encrypted, byte[] privateKey) throws Exception {
        int splitLen = DEFAULT_SPLIT.length;
        if (splitLen <= 0) {
            return decryptByPrivateKey(encrypted, privateKey);
        }
        int dataLen = encrypted.length;
        List<Byte> allBytes = new ArrayList<Byte>(1024);
        int latestStartIndex = 0;
        for (int i = 0; i < dataLen; i++) {
            byte bt = encrypted[i];
            boolean isMatchSplit = false;
            if (i == dataLen - 1) {
                // 到data的最后了
                byte[] part = new byte[dataLen - latestStartIndex];
                System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
                byte[] decryptPart = decryptByPrivateKey(part, privateKey);
                for (byte b : decryptPart) {
                    allBytes.add(b);
                }
                latestStartIndex = i + splitLen;
                i = latestStartIndex - 1;
            } else if (bt == DEFAULT_SPLIT[0]) {
                // 这个是以split[0]开头
                if (splitLen > 1) {
                    if (i + splitLen < dataLen) {
                        // 没有超出data的范围
                        for (int j = 1; j < splitLen; j++) {
                            if (DEFAULT_SPLIT[j] != encrypted[i + j]) {
                                break;
                            }
                            if (j == splitLen - 1) {
                                // 验证到split的最后一位，都没有break，则表明已经确认是split段
                                isMatchSplit = true;
                            }
                        }
                    }
                } else {
                    // split只有一位，则已经匹配了
                    isMatchSplit = true;
                }
            }
            if (isMatchSplit) {
                byte[] part = new byte[i - latestStartIndex];
                System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
                byte[] decryptPart = decryptByPrivateKey(part, privateKey);
                for (byte b : decryptPart) {
                    allBytes.add(b);
                }
                latestStartIndex = i + splitLen;
                i = latestStartIndex - 1;
            }
        }
        byte[] bytes = new byte[allBytes.size()];
        {
            int i = 0;
            for (Byte b : allBytes) {
                bytes[i++] = b.byteValue();
            }
        }
        return bytes;
    }

    /**
     * 用公钥对字符串进行分段加密
     */
    public static String encryptByPublicKeyForSpiltStr(String data, String publicKey) throws Exception {
        byte[] encryptBytes = encryptByPublicKeyForSpilt(data.getBytes(StandardCharsets.UTF_8), publicKey.getBytes());
        return encryptBASE64(encryptBytes);
    }

    /**
     * 使用私钥分段加密
     *
     * @param data       要加密的原始数据
     * @param privateKey 秘钥
     */
    public static String encryptByPrivateKeyForSpiltStr(String data, String privateKey) throws Exception {
        byte[] encryptBytes = encryptByPrivateKeyForSpilt(data.getBytes(StandardCharsets.UTF_8), privateKey.getBytes());
        return encryptBASE64(encryptBytes);
    }

    /**
     * 公钥分段解密
     *
     * @param encrypted 待解密数据
     * @param publicKey 密钥
     */
    public static String decryptByPublicKeyForSpiltStr(String encrypted, String publicKey) throws Exception {
        byte[] decryptBytes  = decryptByPublicKeyForSpilt(decryptBASE64(encrypted), publicKey.getBytes());
        return new String(decryptBytes,StandardCharsets.UTF_8);
    }

    /**
     * 使用私钥分段解密
     */
    public static String decryptByPrivateKeyForSpiltStr(String encrypted, String privateKey) throws Exception {
        byte[] decryptBytes = decryptByPrivateKeyForSpilt(decryptBASE64(encrypted), privateKey.getBytes());
        return new String(decryptBytes,StandardCharsets.UTF_8);
    }

    public static void testEncrypt1(String content) {
        //生成秘钥对
        KeyPair keyPair = RSAAndroid.generateRSAKeyPair(RSAAndroid.DEFAULT_KEY_SIZE);
        //公钥
        PublicKey publicKey = keyPair.getPublic();
        //私钥
        PrivateKey privateKey = keyPair.getPrivate();

        long start;
        long end;
        byte[] encryptBytes = new byte[0];
        byte[] decryptBytes = new byte[0];
        String encryStr, decryStr;

        try {

            System.out.println("公钥:" + encryptBASE64(RSAAndroid.publicRsaKey.getBytes()));
            //公钥加密
            start = System.currentTimeMillis();

            encryptBytes = RSAAndroid.encryptByPublicKeyForSpilt(content.getBytes(StandardCharsets.UTF_8), RSAAndroid.publicRsaKey.getBytes());
            System.out.println("testEncrypt: 公钥加密 encryptBytes:" + encryptBytes);

            end = System.currentTimeMillis();
            System.out.println("公钥加密耗时 cost time---->" + (end - start));
            //encryStr = BASE64Encoder.encode(encryptBytes);
            encryStr = encryptBASE64(encryptBytes);
//            Log.e(TAG, "加密后json数据 --1-->" + encryStr);
            System.out.println("加密后json数据长度 --1-->" + encryStr.length());
            System.out.println("testEncrypt: encryStr:" + encryStr);
            //私钥解密
            System.out.println("私钥:" + encryptBASE64(RSAAndroid.privateRsaKey.getBytes()));
            start = System.currentTimeMillis();
            decryptBytes = RSAAndroid.decryptByPrivateKeyForSpilt(decryptBASE64(encryStr), RSAAndroid.privateRsaKey.getBytes());
            decryStr = new String(decryptBytes);
            System.out.println("testEncrypt: 私钥解密 decryStr:" + decryStr);
            end = System.currentTimeMillis();
            System.out.println("私钥解密耗时 cost time---->" + (end - start));
            System.out.println("解密后json数据 --1-->" + decryStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void testEncrypt2(String content) {
        //生成秘钥对
        KeyPair keyPair = RSAAndroid.generateRSAKeyPair(RSAAndroid.DEFAULT_KEY_SIZE);
        //公钥
        PublicKey publicKey = keyPair.getPublic();
        //私钥
        PrivateKey privateKey = keyPair.getPrivate();

        long start;
        long end;
        byte[] encryptBytes = new byte[0];
        byte[] decryptBytes = new byte[0];
        String encryStr, decryStr;

        try {

            //私钥加密
            start = System.currentTimeMillis();
            encryptBytes = RSAAndroid.encryptByPrivateKeyForSpilt(content.getBytes(StandardCharsets.UTF_8), RSAAndroid.privateRsaKey.getBytes());
            end = System.currentTimeMillis();
            System.out.println("私钥加密密耗时 cost time---->" + (end - start));
            encryStr = encryptBASE64(encryptBytes);

//            Log.e(TAG, "加密后json数据 --2-->" + encryStr);
            System.out.println("加密后json数据长度 --2-->" + encryStr.length());
            System.out.println("testEncrypt: 私钥加密密 encryStr:" + encryStr);
            //公钥解密
            start = System.currentTimeMillis();
            decryptBytes = RSAAndroid.decryptByPublicKeyForSpilt(decryptBASE64(encryStr), RSAAndroid.publicRsaKey.getBytes());

            decryStr = new String(decryptBytes);
            System.out.println("testEncrypt: 公钥解密 decryStr:" + decryStr);
            end = System.currentTimeMillis();
            System.out.println("公钥解密耗时 cost time---->" + (end - start));
            System.out.println("解密后json数据 --2-->" + decryStr);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void testEncrypt3(String content) {
        long start;
        long end;
        String encryStr, decryStr;
        try {
            //公钥加密
            System.out.println("公钥:" + RSAAndroid.publicRsaKey);
            start = System.currentTimeMillis();
            encryStr = RSAAndroid.encryptByPublicKeyForSpiltStr(content, RSAAndroid.publicRsaKey);
            end = System.currentTimeMillis();
            System.out.println("公钥加密耗时 cost time---->" + (end - start));
            System.out.println("加密后json数据长度 --1-->" + encryStr.length());
            System.out.println("testEncrypt: 公钥加密 encryStr:" + encryStr);
            //私钥解密
            System.out.println("私钥:" + RSAAndroid.privateRsaKey);
            start = System.currentTimeMillis();
            encryStr = "X6Ad0LpiBP7ze/cEgwGkYS53zG5ozY1Nrk2HzrrGlWqFZdyrmYihc1Fo3pjxxfOJi60zkZXguPt197vdtf2NHjtBCamvGt8DYhvfGUFP0YSod11GckQQaUslUkwPjQKTGJ46Kdy7Y2gMKK7EdEabEmTPl/XIT9fDFX/nMWgAutcQ15Gq5FRwWQH7sSVNF5bjrxxBs7Q/JwsCsU+xl0LraifUf+1O6NzCHc4ll7dmWq6EGORjaSVYka8ItuI4hhVWffcrOhY1vDqjFkY+Mzac9ppgk1nc5StMMNGyPC4OYsLkqZa5h8acK8YhSi7dpj58Lo/yJWqMmewiGDSskrcFXCNQQVJUI10H9WmD3b//98jW6cq3awLqmpoZmjuN3fm8vx1KPOCEZs2GlM7/wCEUMabV7O5EsTtRf+6pqHYTPzZfxt2lXEEJviktjguG1WI71nwg+1XyIx5tn818XiYvs0L02hi3sWpQkk/NJ4QaE3LziuCMrgDU/GcFzYLybU627yAPPPLjB9wVZGefwUdWjMtY5AzPMBa9eNjsMaHAGe2zbnopafuouwsjH+uEZd2rzT3Uacb7+pNjl94XiB/0Xhwl1n5bi6HbT3aY2jC6YbKqCp5XQaLCbuefaRVCLC6pOHmNX6NKni98/bYKQ6XfhDoA56gJAEVsAaj/a6vpzVIMsej5zDI=";
            decryStr = RSAAndroid.decryptByPrivateKeyForSpiltStr(encryStr, RSAAndroid.privateRsaKey);
            end = System.currentTimeMillis();
            System.out.println("私钥解密耗时 cost time---->" + (end - start));
            System.out.println("解密后json数据长度 --1-->" + decryStr.length());
            System.out.println("testEncrypt: 私钥解密 decryStr:" + decryStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void testEncrypt4(String content) {
        long start;
        long end;
        String encryStr, decryStr;
        try {
            System.out.println("公钥:" + RSAAndroid.publicRsaKey);
            //私钥加密
            start = System.currentTimeMillis();
            encryStr = RSAAndroid.encryptByPrivateKeyForSpiltStr(content, RSAAndroid.privateRsaKey);
            end = System.currentTimeMillis();
            System.out.println("私钥加密密耗时 cost time---->" + (end - start));
            System.out.println("加密后json数据长度 --2-->" + encryStr.length());
            System.out.println("testEncrypt: 私钥加密 encryStr:" + encryStr);
            //公钥解密
            System.out.println("私钥:" + RSAAndroid.privateRsaKey);
            start = System.currentTimeMillis();
            decryStr = RSAAndroid.decryptByPublicKeyForSpiltStr(encryStr, RSAAndroid.publicRsaKey);
            end = System.currentTimeMillis();
            System.out.println("公钥解密耗时 cost time---->" + (end - start));
            System.out.println("解密后json数据 --2-->" + decryStr);
            System.out.println("testEncrypt: 公钥解密 decryStr:" + decryStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        RSAAndroid.testEncrypt3("8月8日晚，东京国立竞技场的奥运圣火熄灭，场内电子屏上打出了“ARIGATO”（日语“谢谢”的罗马字）。57年前的那一夜，东京奥运会闭幕式大屏上留下的是“SAYONARA”（日语“再见”罗马字）。从告别到感谢，本届奥运会对日本而言，原本是一场赌上国运的体育盛事，而现实却朝着与理想相反的方向一路狂奔。8月8日晚，东京国立竞技场的奥运圣火熄灭，场内电子屏上打出了“ARIGATO”（日语“谢谢”的罗马字）。57年前的那一夜，东京奥运会闭幕式大屏上留下的是“SAYONARA”（日语“再见”罗马字）。从告别到感谢，本届奥运会对日本而言，原本是一场赌上国运的体育盛事，而现实却朝着与理想相反的方向一路狂奔。8月8日晚，东京国立竞技场的奥运圣火熄灭，场内电子屏上打出了“ARIGATO”（日语“谢谢”的罗马字）。57年前的那一夜，东京奥运会闭幕式大屏上留下的是“SAYONARA”（日语“再见”罗马字）。从告别到感谢，本届奥运会对日本而言，原本是一场赌上国运的体育盛事，而现实却朝着与理想相反的方向一路狂奔。8月8日晚，东京国立竞技场的奥运圣火熄灭，场内电子屏上打出了“ARIGATO”（日语“谢谢”的罗马字）。57年前的那一夜，东京奥运会闭幕式大屏上留下的是“SAYONARA”（日语“再见”罗马字）。从告别到感谢，本届奥运会对日本而言，原本是一场赌上国运的体育盛事，而现实却朝着与理想相反的方向一路狂奔。8月8日晚，东京国立竞技场的奥运圣火熄灭，场内电子屏上打出了“ARIGATO”（日语“谢谢”的罗马字）。57年前的那一夜，东京奥运会闭幕式大屏上留下的是“SAYONARA”（日语“再见”罗马字）。从告别到感谢，本届奥运会对日本而言，原本是一场赌上国运的体育盛事，而现实却朝着与理想相反的方向一路狂奔。8月8日晚，东京国立竞技场的奥运圣火熄灭，场内电子屏上打出了“ARIGATO”（日语“谢谢”的罗马字）。57年前的那一夜，东京奥运会闭幕式大屏上留下的是“SAYONARA”（日语“再见”罗马字）。从告别到感谢，本届奥运会对日本而言，原本是一场赌上国运的体育盛事，而现实却朝着与理想相反的方向一路狂奔。8月8日晚，东京国立竞技场的奥运圣火熄灭，场内电子屏上打出了“ARIGATO”（日语“谢谢”的罗马字）。57年前的那一夜，东京奥运会闭幕式大屏上留下的是“SAYONARA”（日语“再见”罗马字）。从告别到感谢，本届奥运会对日本而言，原本是一场赌上国运的体育盛事，而现实却朝着与理想相反的方向一路狂奔。8月8日晚，东京国立竞技场的奥运圣火熄灭，场内电子屏上打出了“ARIGATO”（日语“谢谢”的罗马字）。57年前的那一夜，东京奥运会闭幕式大屏上留下的是“SAYONARA”（日语“再见”罗马字）。从告别到感谢，本届奥运会对日本而言，原本是一场赌上国运的体育盛事，而现实却朝着与理想相反的方向一路狂奔。8月8日晚，东京国立竞技场的奥运圣火熄灭，场内电子屏上打出了“ARIGATO”（日语“谢谢”的罗马字）。57年前的那一夜，东京奥运会闭幕式大屏上留下的是“SAYONARA”（日语“再见”罗马字）。从告别到感谢，本届奥运会对日本而言，原本是一场赌上国运的体育盛事，而现实却朝着与理想相反的方向一路狂奔。8月8日晚，东京国立竞技场的奥运圣火熄灭，场内电子屏上打出了“ARIGATO”（日语“谢谢”的罗马字）。57年前的那一夜，东京奥运会闭幕式大屏上留下的是“SAYONARA”（日语“再见”罗马字）。从告别到感谢，本届奥运会对日本而言，原本是一场赌上国运的体育盛事，而现实却朝着与理想相反的方向一路狂奔。8月8日晚，东京国立竞技场的奥运圣火熄灭，场内电子屏上打出了“ARIGATO”（日语“谢谢”的罗马字）。57年前的那一夜，东京奥运会闭幕式大屏上留下的是“SAYONARA”（日语“再见”罗马字）。从告别到感谢，本届奥运会对日本而言，原本是一场赌上国运的体育盛事，而现实却朝着与理想相反的方向一路狂奔。");
    }
}