package com.asdfg2.daili.Common.crypt;
import java.io.UnsupportedEncodingException;
import kotlin.UByte;
public final class XXTEA {
private static final int DELTA = -1640531527;
/* renamed from: MX */
private static int m12MX(int i, int i2, int i3, int i4, int i5, int[] iArr) {
return ((i ^ i2) + (iArr[(i4 & 3) ^ i5] ^ i3)) ^ (((i3 >>> 5) ^ (i2 << 2)) + ((i2 >>> 3) ^ (i3 << 4)));
}
private XXTEA() {
}
public static final byte[] encrypt(byte[] bArr, byte[] bArr2) {
if (bArr.length == 0) {
return bArr;
}
return toByteArray(encrypt(toIntArray(bArr, true), toIntArray(fixKey(bArr2), false)), false);
}
public static final byte[] encrypt(String str, byte[] bArr) {
try {
return encrypt(str.getBytes("UTF-8"), bArr);
} catch (UnsupportedEncodingException unused) {
return null;
}
}
public static final byte[] encrypt(byte[] bArr, String str) {
try {
return encrypt(bArr, str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException unused) {
return null;
}
}
public static final byte[] encrypt(String str, String str2) {
try {
return encrypt(str.getBytes("UTF-8"), str2.getBytes("UTF-8"));
} catch (UnsupportedEncodingException unused) {
return null;
}
}
public static final String encryptToBase64String(byte[] bArr, byte[] bArr2) {
byte[] encrypt = encrypt(bArr, bArr2);
if (encrypt == null) {
return null;
}
return Base64.encode(encrypt);
}
public static final String encryptToBase64String(String str, byte[] bArr) {
byte[] encrypt = encrypt(str, bArr);
if (encrypt == null) {
return null;
}
return Base64.encode(encrypt);
}
public static final String encryptToBase64String(byte[] bArr, String str) {
byte[] encrypt = encrypt(bArr, str);
if (encrypt == null) {
return null;
}
return Base64.encode(encrypt);
}
public static final String encryptToBase64String(String str, String str2) {
byte[] encrypt = encrypt(str, str2);
if (encrypt == null) {
return null;
}
return Base64.encode(encrypt);
}
public static final byte[] decrypt(byte[] bArr, byte[] bArr2) {
if (bArr.length == 0) {
return bArr;
}
return toByteArray(decrypt(toIntArray(bArr, false), toIntArray(fixKey(bArr2), false)), true);
}
public static final byte[] decrypt(byte[] bArr, String str) {
try {
return decrypt(bArr, str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException unused) {
return null;
}
}
public static final byte[] decryptBase64String(String str, byte[] bArr) {
return decrypt(Base64.decode(str), bArr);
}
public static final byte[] decryptBase64String(String str, String str2) {
return decrypt(Base64.decode(str), str2);
}
public static final String decryptToString(byte[] bArr, byte[] bArr2) {
try {
byte[] decrypt = decrypt(bArr, bArr2);
if (decrypt == null) {
return null;
}
return new String(decrypt, "UTF-8");
} catch (UnsupportedEncodingException unused) {
return null;
}
}
public static final String decryptToString(byte[] bArr, String str) {
try {
byte[] decrypt = decrypt(bArr, str);
if (decrypt == null) {
return null;
}
return new String(decrypt, "UTF-8");
} catch (UnsupportedEncodingException unused) {
return null;
}
}
public static final String decryptBase64StringToString(String str, byte[] bArr) {
try {
byte[] decrypt = decrypt(Base64.decode(str), bArr);
if (decrypt == null) {
return null;
}
return new String(decrypt, "UTF-8");
} catch (UnsupportedEncodingException unused) {
return null;
}
}
public static final String decryptBase64StringToString(String str, String str2) {
try {
byte[] decrypt = decrypt(Base64.decode(str), str2);
if (decrypt == null) {
return null;
}
return new String(decrypt, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static int[] encrypt(int[] iArr, int[] iArr2) {
int length = iArr.length - 1;
if (length < 1) {
return iArr;
}
int i = (52 / (length + 1)) + 6;
int i2 = iArr[length];
int i3 = 0;
while (true) {
int i4 = i - 1;
if (i <= 0) {
return iArr;
}
int i5 = DELTA + i3;
int i6 = (i5 >>> 2) & 3;
int i7 = i2;
int i8 = 0;
while (i8 < length) {
int i9 = i8 + 1;
i7 = iArr[i8] + m12MX(i5, iArr[i9], i7, i8, i6, iArr2);
iArr[i8] = i7;
i8 = i9;
}
i2 = iArr[length] + m12MX(i5, iArr[0], i7, i8, i6, iArr2);
iArr[length] = i2;
i3 = i5;
i = i4;
}
}
private static int[] decrypt(int[] iArr, int[] iArr2) {
int length = iArr.length - 1;
if (length < 1) {
return iArr;
}
int i = iArr[0];
for (int i2 = ((52 / (length + 1)) + 6) * DELTA; i2 != 0; i2 -= DELTA) {
int i3 = (i2 >>> 2) & 3;
int i4 = i;
int i5 = length;
while (i5 > 0) {
i4 = iArr[i5] - m12MX(i2, i4, iArr[i5 - 1], i5, i3, iArr2);
iArr[i5] = i4;
i5--;
}
i = iArr[0] - m12MX(i2, i4, iArr[length], i5, i3, iArr2);
iArr[0] = i;
}
return iArr;
}
private static byte[] fixKey(byte[] bArr) {
if (bArr.length == 16) {
return bArr;
}
byte[] bArr2 = new byte[16];
if (bArr.length < 16) {
System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
} else {
System.arraycopy(bArr, 0, bArr2, 0, 16);
}
return bArr2;
}
private static int[] toIntArray(byte[] bArr, boolean z) {
int[] iArr;
int length = (bArr.length & 3) == 0 ? bArr.length >>> 2 : (bArr.length >>> 2) + 1;
if (z) {
iArr = new int[(length + 1)];
iArr[length] = bArr.length;
} else {
iArr = new int[length];
}
int length2 = bArr.length;
for (int i = 0; i < length2; i++) {
int i2 = i >>> 2;
iArr[i2] = iArr[i2] | ((bArr[i] & UByte.MAX_VALUE) << ((i & 3) << 3));
}
return iArr;
}
private static byte[] toByteArray(int[] iArr, boolean z) {
int i;
int length = iArr.length << 2;
if (z) {
i = iArr[iArr.length - 1];
int i2 = length - 4;
if (i < i2 - 3 || i > i2) {
return null;
}
} else {
i = length;
}
byte[] bArr = new byte[i];
for (int i3 = 0; i3 < i; i3++) {
bArr[i3] = (byte) (iArr[i3 >>> 2] >>> ((i3 & 3) << 3));
}
return bArr;
}
}