package p012cn.jiguang.p024d.p035g.p036a;
import android.support.p008v4.view.ViewCompat;
import android.util.Base64;
import java.lang.reflect.InvocationTargetException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import p012cn.jiguang.p024d.p025a.C0307d;
import p012cn.jiguang.p038e.C0416d;
import p012cn.jiguang.p039f.C0418b;
import p012cn.jiguang.p040g.C0421a;
/* renamed from: cn.jiguang.d.g.a.a */
public final class C0397a {
/* renamed from: a */
private static String f531a = "";
/* renamed from: b */
private static int f532b;
/* renamed from: a */
public static String m832a() {
return f531a;
}
/* renamed from: a */
public static String m833a(long j) {
long j2;
long j3;
switch (((int) j) % 10) {
case 1:
j2 = 5 * j;
j3 = 88;
break;
case 2:
j2 = 23 * j;
j3 = 15;
break;
case 3:
j2 = 3 * j;
j3 = 73;
break;
case 4:
j2 = 13 * j;
j3 = 96;
break;
case 5:
j2 = 17 * j;
j3 = 49;
break;
case 6:
j2 = 7 * j;
j3 = 68;
break;
case 7:
j2 = 31 * j;
j3 = 39;
break;
case 8:
j2 = 29 * j;
j3 = 41;
break;
case 9:
j2 = 37 * j;
j3 = 91;
break;
default:
j2 = 8 * j;
j3 = 74;
break;
}
long j4 = j2 + (j % j3);
return C0421a.m998a("JCKP" + String.valueOf(j4));
}
/* renamed from: a */
public static String m834a(String str) {
String encodeToString;
try {
if ("DFA84B10B7ACDD25".length() != 16) {
encodeToString = null;
} else {
byte[] c = m844c("DFA84B10B7ACDD25", "ASCII");
Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
instance.init(1, new SecretKeySpec(c, "AES"), m836a(c));
encodeToString = Base64.encodeToString(instance.doFinal(str.getBytes()), 2);
}
return encodeToString;
} catch (Exception unused) {
C0416d.m970g("", "Unexpected - failed to AES encrypt.");
return "";
}
}
/* renamed from: a */
public static String m835a(String str, String str2) {
if (str2 == null) {
return null;
}
try {
if (str2.length() != 16) {
return null;
}
byte[] c = m844c(str2, "ASCII");
Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
instance.init(2, new SecretKeySpec(c, "AES"), m836a(c));
return new String(instance.doFinal(Base64.decode(str, 2)));
} catch (Exception unused) {
return null;
}
}
/* renamed from: a */
private static IvParameterSpec m836a(byte[] bArr) {
try {
return (IvParameterSpec) C0418b.m983a(IvParameterSpec.class, new Object[]{bArr}, new Class[]{byte[].class});
} catch (NoSuchMethodException e) {
e.printStackTrace();
return null;
} catch (IllegalAccessException e2) {
e2.printStackTrace();
return null;
} catch (InvocationTargetException e3) {
e3.printStackTrace();
return null;
} catch (InstantiationException e4) {
e4.printStackTrace();
return null;
}
}
/* renamed from: a */
public static void m837a(int i) {
f532b = i;
}
/* renamed from: a */
public static byte[] m838a(String str, byte[] bArr) {
SecretKeySpec secretKeySpec = new SecretKeySpec(m844c(str, "utf-8"), "AES");
Cipher instance = Cipher.getInstance("AES/ECB/PKCS7Padding");
instance.init(1, secretKeySpec);
return instance.doFinal(bArr);
}
/* renamed from: a */
public static byte[] m839a(byte[] bArr, int i) {
Exception e;
byte[] bArr2;
long j;
int length = bArr.length - 24;
byte[] bArr3 = new byte[24];
byte[] bArr4 = new byte[length];
System.arraycopy(bArr, 0, bArr3, 0, 24);
System.arraycopy(bArr, 24, bArr4, 0, length);
String str = "";
if (i == 1) {
j = C0307d.m391d(null);
} else {
if (i == 0) {
j = (long) f532b;
}
byte[] a = m838a(str, bArr4);
int length2 = a.length + 24;
bArr2 = new byte[length2];
try {
System.arraycopy(bArr3, 0, bArr2, 0, 24);
System.arraycopy(a, 0, bArr2, 24, a.length);
bArr2[0] = (byte) ((length2 >>> 8) & 255);
bArr2[1] = (byte) (length2 & 255);
bArr2[0] = (byte) (bArr2[0] | 128);
return bArr2;
} catch (Exception e2) {
e = e2;
e.printStackTrace();
return bArr2;
}
}
str = m841b(j);
try {
byte[] a2 = m838a(str, bArr4);
int length22 = a2.length + 24;
bArr2 = new byte[length22];
System.arraycopy(bArr3, 0, bArr2, 0, 24);
System.arraycopy(a2, 0, bArr2, 24, a2.length);
bArr2[0] = (byte) ((length22 >>> 8) & 255);
bArr2[1] = (byte) (length22 & 255);
bArr2[0] = (byte) (bArr2[0] | 128);
return bArr2;
} catch (Exception e3) {
e = e3;
bArr2 = null;
e.printStackTrace();
return bArr2;
}
}
/* renamed from: b */
public static int m840b() {
return Math.abs(new SecureRandom().nextInt()) & ViewCompat.MEASURED_SIZE_MASK;
}
/* renamed from: b */
private static String m841b(long j) {
long j2;
long j3;
String valueOf = String.valueOf(j);
int length = valueOf.length();
if (length >= 2) {
valueOf = valueOf.substring(length - 2, length);
}
switch (Integer.parseInt(valueOf) % 10) {
case 1:
j2 = 5 * j;
j3 = 88;
break;
case 2:
j2 = 23 * j;
j3 = 15;
break;
case 3:
j2 = 3 * j;
j3 = 73;
break;
case 4:
j2 = 13 * j;
j3 = 96;
break;
case 5:
j2 = 17 * j;
j3 = 49;
break;
case 6:
j2 = 7 * j;
j3 = 68;
break;
case 7:
j2 = 31 * j;
j3 = 39;
break;
case 8:
j2 = 29 * j;
j3 = 41;
break;
case 9:
j2 = 37 * j;
j3 = 91;
break;
default:
j2 = 8 * j;
j3 = 74;
break;
}
long j4 = j2 + (j % j3);
String a = C0421a.m998a("JCKP" + String.valueOf(j4));
f531a = a;
return a;
}
/* renamed from: b */
public static String m842b(String str, String str2) {
try {
return m835a(str, "DFA84B10B7ACDD25");
} catch (Exception unused) {
C0416d.m970g("", "Unexpected - failed to AES decrypt.");
return str2;
}
}
/* renamed from: b */
public static byte[] m843b(String str, byte[] bArr) {
SecretKeySpec secretKeySpec = new SecretKeySpec(m844c(str, "utf-8"), "AES");
Cipher instance = Cipher.getInstance("AES/ECB/NoPadding");
instance.init(2, secretKeySpec);
return instance.doFinal(bArr);
}
/* renamed from: c */
private static byte[] m844c(String str, String str2) {
byte[] bArr = new byte[str.length()];
byte[] bytes = str.substring(0, str.length() / 2).getBytes(str2);
byte[] bytes2 = str.substring(str.length() / 2).getBytes(str2);
System.arraycopy(bytes, 0, bArr, 0, bytes.length);
System.arraycopy(bytes2, 0, bArr, bytes.length, bytes2.length);
return bArr;
}
}