package com.ea.nimble; import com.ea.nimble.Log; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.security.GeneralSecurityException; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; import javax.crypto.spec.SecretKeySpec; /* loaded from: classes2.dex */ class Encryptor { private static int ENCRYPTION_KEY_LENGTH = 256; private static int ENCRYPTION_KEY_ROUND = 997; private static final byte[] ENCRYPTOR_VERSION_1 = {78, 69, 86, 49}; private static final byte[] ENCRYPTOR_VERSION_2 = {78, 69, 86, 50}; private SecretKey getKeyV1(byte[] bArr) throws GeneralSecurityException { try { return new SecretKeySpec(SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(new PBEKeySpec(ApplicationEnvironment.getComponent().getApplicationBundleId().toCharArray(), bArr, ENCRYPTION_KEY_ROUND, ENCRYPTION_KEY_LENGTH)).getEncoded(), "AES"); } catch (GeneralSecurityException e) { Log.Helper.LOGFS(null, "Can't initialize Encryptor: " + e.toString(), new Object[0]); throw e; } } private SecretKey getKeyV2() throws GeneralSecurityException { try { IApplicationEnvironment component = ApplicationEnvironment.getComponent(); return new SecretKeySpec(SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(new PBEKeySpec(component.getAndroidId().toCharArray(), Utility.SHA256Hash(component.getApplicationBundleId()), ENCRYPTION_KEY_ROUND, ENCRYPTION_KEY_LENGTH)).getEncoded(), "AES"); } catch (GeneralSecurityException e) { Log.Helper.LOGFS(null, "Can't initialize Encryptor: " + e.toString(), new Object[0]); throw e; } } public ObjectOutputStream encryptOutputStream(OutputStream outputStream) throws IOException, GeneralSecurityException { SecretKey keyV2 = getKeyV2(); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(1, keyV2); byte[] bArr = ENCRYPTOR_VERSION_2; outputStream.write(bArr, 0, bArr.length); byte[] iv = ((IvParameterSpec) cipher.getParameters().getParameterSpec(IvParameterSpec.class)).getIV(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeInt(iv.length); objectOutputStream.write(iv, 0, iv.length); objectOutputStream.flush(); return new ObjectOutputStream(new CipherOutputStream(outputStream, cipher)); } public ObjectInputStream decryptInputStream(InputStream inputStream) throws IOException, GeneralSecurityException { if (!inputStream.markSupported()) { Log.Helper.LOGWS("Encryptor", "decryptInputStream() : InputStream doesn't support stream mark()", new Object[0]); } byte[] bArr = ENCRYPTOR_VERSION_2; int length = bArr.length; inputStream.mark(length); byte[] bArr2 = new byte[length]; if (inputStream.read(bArr2, 0, length) < length || !Arrays.equals(bArr, bArr2)) { inputStream.reset(); return decryptV1InputStream(inputStream); } ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); int readInt = objectInputStream.readInt(); byte[] bArr3 = new byte[readInt]; objectInputStream.read(bArr3, 0, readInt); SecretKey keyV2 = getKeyV2(); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(2, keyV2, new IvParameterSpec(bArr3)); return new ObjectInputStream(new CipherInputStream(inputStream, cipher)); } private ObjectInputStream decryptV1InputStream(InputStream inputStream) throws IOException, GeneralSecurityException { byte[] bArr = ENCRYPTOR_VERSION_1; int length = bArr.length; inputStream.mark(length); byte[] bArr2 = new byte[length]; if (inputStream.read(bArr2, 0, length) < length || !Arrays.equals(bArr, bArr2)) { inputStream.reset(); return decryptLegacyInputStream(inputStream); } ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); int readInt = objectInputStream.readInt(); byte[] bArr3 = new byte[readInt]; objectInputStream.read(bArr3, 0, readInt); int readInt2 = objectInputStream.readInt(); byte[] bArr4 = new byte[readInt2]; objectInputStream.read(bArr4, 0, readInt2); SecretKey keyV1 = getKeyV1(bArr3); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(2, keyV1, new IvParameterSpec(bArr4)); return new ObjectInputStream(new CipherInputStream(inputStream, cipher)); } private ObjectInputStream decryptLegacyInputStream(InputStream inputStream) throws IOException, GeneralSecurityException { String applicationBundleId = ApplicationEnvironment.getComponent().getApplicationBundleId(); byte[] bytes = "02:00:00:00:00:00".getBytes(); byte[] bArr = new byte[8]; int i = 0; int i2 = 0; while (i < 8) { int i3 = i2 + 1; if (i3 % 3 == 0) { i2 = i3; } bArr[i] = bytes[i2]; i++; i2++; } SecretKey generateSecret = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(applicationBundleId.toCharArray(), bArr, ENCRYPTION_KEY_ROUND, ENCRYPTION_KEY_LENGTH)); PBEParameterSpec pBEParameterSpec = new PBEParameterSpec(bArr, ENCRYPTION_KEY_ROUND); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(2, generateSecret, pBEParameterSpec); return new ObjectInputStream(new CipherInputStream(inputStream, cipher)); } }