android GZIP加密

import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.RequiresApi;



import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Random;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class decry {

    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String Decrypt(String paramString){
        String replac = paramString.replace("whalev2://", "").substring(10);
        String rot13= rot13(replac);
        String str="";
        ByteArrayOutputStream byteArrayOutputStream = null;
        ByteArrayInputStream byteArrayInputStream = null;
        GZIPInputStream gZIPInputStream = null;
        try {
             byteArrayOutputStream = new ByteArrayOutputStream();
             byteArrayInputStream = new ByteArrayInputStream(Base64.getDecoder().decode(rot13.getBytes("utf-8")));
             gZIPInputStream = new GZIPInputStream(byteArrayInputStream);
            byte[] bArr = new byte[1024];
            while (true) {
                int read = gZIPInputStream.read(bArr);
                if (read == -1) {
                    break;
                }
                byteArrayOutputStream.write(bArr, 0, read);
            }



            String byteArray = byteArrayOutputStream.toString();
//            str=byteArray;
//            Log.e("byteArray::::",byteArray);
//
//            String rot13s= rot13(byteArray.replace("whale://", ""));
//            Log.e("rot13s::::",rot13s);
            String replace = byteArray.replace("whale://", "");
            if (!TextUtils.isEmpty(replace)) {
                replace = replace.substring(2, replace.length() - 1);
            Log.e("rot13s:222:::",replace);
                replace = URLDecoder.decode(replace, "UTF-8");
                replace = rot13(replace);
            Log.e("rot13s:333:::",replace);
                byte[] var9 = Base64.getDecoder().decode(replace);
                str = new String(var9, "utf-8");

                if (str.startsWith("codes=")) {
                    str = str.substring(6);

                    try {
                        str = URLDecoder.decode(str, "UTF-8");
                    } catch (UnsupportedEncodingException var4) {
                        str = str.substring(6);
                    }
//                    Log.e("replace::::",str);
                }
            }




            gZIPInputStream.close();

            byteArrayInputStream.close();
            byteArrayOutputStream.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                byteArrayOutputStream.close();
                byteArrayInputStream.close();
                gZIPInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return str;

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String Encrypt(String paramString){
        String str="";
        try {
            String codes = "codes=" + URLEncoder.encode(paramString, "UTF-8");
            String Base64Data = Base64.getEncoder().encodeToString(codes.getBytes(StandardCharsets.UTF_8));
            String rot13= rot13(Base64Data);
            String MD5 = getMD5(paramString);
           String whale = "whale://" + MD5.substring(4, 6) + URLEncoder.encode(rot13, "UTF-8") + MD5.substring(0, 1);
            str=whale;
           ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);
            gZIPOutputStream.write(whale.getBytes());
            gZIPOutputStream.close();
            String Base64ByteArray = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
            byteArrayOutputStream.close();

            Base64ByteArray = rot13(Base64ByteArray);
            str= "whalev2://" + generateRandomString(10) + Base64ByteArray;

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return str;
    }





    /**
     *  rot13
     * @param input
     * @return
     */
    public static String rot13(String input) {
        byte[] bytes = input.getBytes();
        char[] output = new char[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            char base;
            int result;
            if (b >= '0' && b <= '9') {
                base = '0';
                result = (b - '0' + 5) % 10;
            } else {
                base = 'A';
                if (b >= 'A' && b <= 'Z') {
                    result = b - 'A';
                } else {
                    byte lowerCase = 'a';
                    if (b < 'a' || b > 'z') {
                        output[i] = (char) b;
                        continue;
                    }
                    result = b - 'a';
                    base = (char)lowerCase;
                }
                result = (result + 13) % 26;
            }
            output[i] = (char) (result + base);
        }
        return String.valueOf(output);
    }


        private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

        public static String getMD5(String s) {
            try {
                // Create MD5 Hash
                 MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
                 digest.update(s.getBytes());
                 byte messageDigest[] = digest.digest();

                StringBuilder sb = new StringBuilder(messageDigest.length * 2);
                for (int i = 0; i < messageDigest.length; i++) {
                    sb.append(HEX_DIGITS[(messageDigest[i] & 0xf0) >>> 4]);
                    sb.append(HEX_DIGITS[messageDigest[i] & 0x0f]);
                }
                return sb.toString().toLowerCase();
//                 return toHexString(messageDigest);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            return "";
        }






    public static String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        StringBuilder randomString = new StringBuilder(length);

        SecureRandom random = new SecureRandom();
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(characters.length());
            char randomChar = characters.charAt(randomIndex);
            randomString.append(randomChar);
        }

        return randomString.toString();
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值