package com.example.sjkbf.util;

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

import javax.servlet.http.HttpServletRequest;

public class tool {


    public static String encryptDecrypt(String input, String key) {
        char[] output = new char[input.length()];
        for (int i = 0; i < input.length(); i++) {
            output[i] = (char) (input.charAt(i) ^ key.charAt(i % key.length())); // 使用异或运算进行加密
        }
        return new String(output);
    }


    private static final String prkey = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAMiYuNW4K1rST7ZW" +
            "YpWX6nEziXi5JveLPhDCLj0VZ/5/4dzBWrmoL/IcFZuHOBJtYHm965713kKC9gtw" +
            "2EyVgkqmXLT3105jEUqzNizfThc6C2ZL6vMmzUZlooxNyaOC5mWthPZtwlqQihYW" +
            "T2nW/wKp8fpTouXihQOCPjqdRoVFAgMBAAECgYEAg+1dcsHjUaIL5uG9iKEXAUhh" +
            "21H6PMgJE8CB5I6VjJ3Sj/tijcP9dH/f/h/aUtQ4pRixRCRz/s5ev3uB5ixWOVOz" +
            "Uhga/NwjURlkg1Katg/14tvd8iZsAzI7KyXGdUefC8+Rulc8SBx+BqYIZ1FTLJkg" +
            "obisps6esaaNZekvS0ECQQDxPnfWuNfwfpYFK038AhEFqBiOBMsMrB/QX+O0sQ/Z" +
            "31gWkNuu3Z+cz6PG0FEK0O8/jsXQ1/gVAreADptJAMzpAkEA1N3GFV1LuYYqkp3a" +
            "y4DnmdI/LgrBUAjWSvTSDKfB8Ik2Y8I2zDS20EsEQMvQYtLxMOMHIEqeAVzmnZcS" +
            "dH4L/QJAQM/elFJuuU7Y8SSUO/s2JYXmqukAwDPSDEJmw5m6P3dwjAd47b7e7dsf" +
            "Df/Tdgtx62ppHNWY8dQcBoxmDbPoWQJBANBQBBROS9fQA4Od9Usn5/5xcSDWp51y" +
            "OHv8ID8AQNvq+44ets+aWrl2YEAk9NZxRlWoJwYj34LH7muUNxxHaeUCQQCjtLPU" +
            "din4XjlMMRSTW/EOgX4JgZ+tQaAtB32wQocPDDeHqkDusUdlEpnDPbmkE9Sbxf1p" +
            "0mSxXgnIuT99niBd";
    private static final String prkey2 = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIdcnQN/YkABlIDJ" +
            "b2wqZgYEHRJReHvIWbeyNV7zJsMZdZRmQYKi2WMmNMH3DKyW7CCzdYrvHbQ5QyqP" +
            "YaQ1X9hVv4bPzXPKSCBVPh5Cr13WwKgh3lhLmbkA/hJ9giRIo9jT47fHE3RaJkic" +
            "RyCqWDSiOsnF7gNYJQf8MAarl/oTAgMBAAECgYAvZKvM57LcujDmjqCmfInQHWyY" +
            "V1IZ9OIEwS6Smgu+T5GtbcgOFzqJzny2VOtpGkZ+hJbpGCOMUGhF5IHJXq65qvei" +
            "ejBeJEH9xnTuJcXUKtHJjIGJQsKzuNKuTddFlY9an/BzoZq4/2Txyh2z6ZDZU1z0" +
            "9pVY8tJe2FVyMY2oIQJBAKhLDMV+kfWLzDfiXijJ28DHdKCozaaAtLZRTRL/XlQU" +
            "O8phcZ0wEKkzrNvn0Fn+5VIQFu1ghWvCo4iaS181FqkCQQDN5/+nbbzZ2AqQsBDB" +
            "eCbLiXPJM7Ey69O4leMyzKL1dyI7nMl5/SeOQE/xBViqzF6M/VUGFzE1YOxl+HZA" +
            "AQxbAkA3GNsFjXqbuTkS0rmhAPVBGAvGzX0GlfyT8+oX3iMzw6wPhfd8TtAllRVy" +
            "qWI9FIZfZ4UaEg+cSAziHCGw+eCBAkAlLJWDbNSDn/WIWcEsrLyUUqSM1sBPKTR4" +
            "RtdV/IwPNQsBKDVXyNSzYfkM0qJHc84+Nh57kwRXVsBXSJxQwd+xAkB9ZzCD0sp6" +
            "8ISzFmbwL+uVtUbe/W0pLC9PDN9B7Vcw6Jf8V0B3lxB493rxTzG3o5oapDDxFIv/" +
            "I6SViudhGXfP";

    private static final String publicKey  = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHXJ0Df2JAAZSAyW9sKmYGBB0S" +
            "UXh7yFm3sjVe8ybDGXWUZkGCotljJjTB9wysluwgs3WK7x20OUMqj2GkNV/YVb+G" +
            "z81zykggVT4eQq9d1sCoId5YS5m5AP4SfYIkSKPY0+O3xxN0WiZInEcgqlg0ojrJ" +
            "xe4DWCUH/DAGq5f6EwIDAQAB";



    static RSA rsa2 = new RSA(prkey, null);
    static RSA rsa22 = new RSA(prkey2, publicKey);

    public static String getIpAddress(HttpServletRequest request) {
        // 获取请求的IP地址
        String ipAddress = request.getRemoteAddr();
        // 如果应用部署在反向代理或负载均衡器后面，可以通过X-Forwarded-For头部获取真实IP地址
        String forwardedFor = request.getHeader("X-Forwarded-For");
        if (forwardedFor != null && !forwardedFor.isEmpty()) {
            ipAddress = forwardedFor.split(",")[0];
        }
        return ipAddress;
    }


    /**
     * 解密
     * @param str
     * @return
     */
    public static String  decrypt(String str){

        str= str.replaceAll(" ","+");
        byte[] decrypt2 = rsa2.decrypt(str, KeyType.PublicKey);
        String s = new String(decrypt2);
        String[] split = s.split("");
        s = split[0];
        return s;
    }

    /**
     * 加密
     * @param str
     * @return
     */
    public static String  encryption(String str){
        return Base64.encode(rsa2.encrypt(str, KeyType.PrivateKey));
    }


    /**
     * 加密2
     * @param str
     * @return
     */
    public static String  encryption2(String str){
        return Base64.encode(rsa22.encrypt(str, KeyType.PrivateKey));
    }

    /**
     * 解密
     */
    public static String  decrypt2(String str){
        //先进行base64解密
        str= Base64.decodeStr(str);
        byte[] decrypt2 = rsa22.decrypt(str, KeyType.PublicKey);
        String s = new String(decrypt2);
        return s;
    }


}
