hk.java 源代码


package com.inmobi.media;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Base64;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class hk {

    private static final String f15710a = "hk";

    private static String f15711b = "AES";

    private static String f15712c = "AES/CBC/PKCS7Padding";

    private static byte[] f15713d;

    public static String a(String str, byte[] bArr, byte[] bArr2, byte[] bArr3, String str2, String str3) {
        try {
            byte[] b6 = b(str.getBytes("UTF-8"), bArr, bArr2);
            byte[] b7 = b(b6, bArr3);
            byte[] a7 = a(b6);
            byte[] a8 = a(b7);
            return new String(Base64.encode(a(a(a(a(a(a(bArr), a(bArr3)), a(bArr2)), str3, str2)), a(a7, a8)), 8));
        } catch (Exception unused) {
            return null;
        }
    }

    @SuppressLint({"TrulyRandom"})
    private static byte[] b(byte[] bArr, byte[] bArr2, byte[] bArr3) {
        SecretKeySpec secretKeySpec = new SecretKeySpec(bArr2, f15711b);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(bArr3);
        try {
            Cipher cipher = Cipher.getInstance(f15712c);
            cipher.init(1, secretKeySpec, ivParameterSpec);
            cipher.init(1, secretKeySpec, ivParameterSpec);
            cipher.init(1, secretKeySpec, ivParameterSpec);
            return cipher.doFinal(bArr);
        } catch (Throwable unused) {
            return null;
        }
    }

    private static byte[] b() {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128, new SecureRandom());
            SecretKey generateKey = keyGenerator.generateKey();
            if (generateKey != null) {
                return generateKey.getEncoded();
            }
            return null;
        } catch (Exception unused) {
            return null;
        }
    }

    private static byte[] b(byte[] bArr, byte[] bArr2) {
        SecretKeySpec secretKeySpec = new SecretKeySpec(bArr2, 0, bArr2.length, "HmacSHA1");
        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(secretKeySpec);
            return mac.doFinal(bArr);
        } catch (InvalidKeyException | NoSuchAlgorithmException unused) {
            return null;
        }
    }

    public static byte[] a(byte[] bArr) {
        long length = bArr.length;
        ByteBuffer allocate = ByteBuffer.allocate(8);
        allocate.order(ByteOrder.LITTLE_ENDIAN);
        allocate.putLong(length);
        byte[] array = allocate.array();
        byte[] bArr2 = new byte[array.length + bArr.length];
        System.arraycopy(array, 0, bArr2, 0, array.length);
        System.arraycopy(bArr, 0, bArr2, array.length, bArr.length);
        return bArr2;
    }

    public static byte[] a(byte[] bArr, String str, String str2) {
        try {
            RSAPublicKey rSAPublicKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(new BigInteger(str2, 16), new BigInteger(str, 16)));
            Cipher cipher = Cipher.getInstance("RSA/ECB/nopadding");
            cipher.init(1, rSAPublicKey);
            return cipher.doFinal(bArr);
        } catch (Throwable unused) {
            return null;
        }
    }

    public static byte[] a(byte[] bArr, byte[] bArr2) {
        byte[] bArr3 = new byte[bArr.length + bArr2.length];
        System.arraycopy(bArr, 0, bArr3, 0, bArr.length);
        System.arraycopy(bArr2, 0, bArr3, bArr.length, bArr2.length);
        return bArr3;
    }

    public static synchronized byte[] a() {
        synchronized (hk.class) {
            Context c6 = gy.c();
            if (c6 == null) {
                return f15713d;
            }
            hh hhVar = new hh(c6, "aes_key_store");
            try {
                if ((System.currentTimeMillis() / 1000) - hhVar.a() > 86400) {
                    byte[] b6 = b();
                    f15713d = b6;
                    hhVar.b("aes_public_key", Base64.encodeToString(b6, 0));
                } else if (f15713d == null) {
                    try {
                        f15713d = Base64.decode(hhVar.b("aes_public_key"), 0);
                    } catch (IllegalArgumentException unused) {
                        byte[] b7 = b();
                        f15713d = b7;
                        hhVar.b("aes_public_key", Base64.encodeToString(b7, 0));
                    }
                }
            } catch (Exception unused2) {
            }
            return f15713d;
        }
    }

    public static byte[] a(byte[] bArr, byte[] bArr2, byte[] bArr3) {
        SecretKeySpec secretKeySpec = new SecretKeySpec(bArr2, f15711b);
        try {
            Cipher cipher = Cipher.getInstance(f15712c);
            cipher.init(2, secretKeySpec, new IvParameterSpec(bArr3));
            return cipher.doFinal(bArr);
        } catch (Throwable unused) {
            return null;
        }
    }

    public static byte[] a(int i6) {
        byte[] bArr = new byte[i6];
        try {
            new SecureRandom().nextBytes(bArr);
        } catch (Exception unused) {
        }
        return bArr;
    }
}