package com.google.android.gms.internal.pal;

import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/* JADX INFO: loaded from: classes2.dex */
public final class q3 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static Cipher f21155a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Object f21156b = new Object();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final Object f21157c = new Object();

    public static String a(byte[] bArr, byte[] bArr2) throws zzda {
        byte[] bArrDoFinal;
        byte[] iv;
        int length = bArr.length;
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(bArr, "AES");
            synchronized (f21156b) {
                c().init(1, secretKeySpec, (SecureRandom) null);
                bArrDoFinal = c().doFinal(bArr2);
                iv = c().getIV();
            }
            int length2 = bArrDoFinal.length + iv.length;
            ByteBuffer byteBufferAllocate = ByteBuffer.allocate(length2);
            byteBufferAllocate.put(iv).put(bArrDoFinal);
            byteBufferAllocate.flip();
            byte[] bArr3 = new byte[length2];
            byteBufferAllocate.get(bArr3);
            return q9.p(bArr3, false);
        } catch (InvalidKeyException e10) {
            throw new zzda(e10);
        } catch (NoSuchAlgorithmException e11) {
            throw new zzda(e11);
        } catch (BadPaddingException e12) {
            throw new zzda(e12);
        } catch (IllegalBlockSizeException e13) {
            throw new zzda(e13);
        } catch (NoSuchPaddingException e14) {
            throw new zzda(e14);
        }
    }

    public static byte[] b(byte[] bArr, String str) {
        byte[] bArrDoFinal;
        int length = bArr.length;
        try {
            byte[] bArrO = q9.O(str, false);
            int length2 = bArrO.length;
            if (length2 <= 16) {
                throw new zzda();
            }
            ByteBuffer byteBufferAllocate = ByteBuffer.allocate(length2);
            byteBufferAllocate.put(bArrO);
            byteBufferAllocate.flip();
            byte[] bArr2 = new byte[16];
            byte[] bArr3 = new byte[length2 - 16];
            byteBufferAllocate.get(bArr2);
            byteBufferAllocate.get(bArr3);
            SecretKeySpec secretKeySpec = new SecretKeySpec(bArr, "AES");
            synchronized (f21156b) {
                c().init(2, secretKeySpec, new IvParameterSpec(bArr2));
                bArrDoFinal = c().doFinal(bArr3);
            }
            return bArrDoFinal;
        } catch (IllegalArgumentException e10) {
            throw new zzda(e10);
        } catch (InvalidAlgorithmParameterException e11) {
            throw new zzda(e11);
        } catch (InvalidKeyException e12) {
            throw new zzda(e12);
        } catch (NoSuchAlgorithmException e13) {
            throw new zzda(e13);
        } catch (BadPaddingException e14) {
            throw new zzda(e14);
        } catch (IllegalBlockSizeException e15) {
            throw new zzda(e15);
        } catch (NoSuchPaddingException e16) {
            throw new zzda(e16);
        }
    }

    public static final Cipher c() {
        Cipher cipher;
        synchronized (f21157c) {
            try {
                if (f21155a == null) {
                    f21155a = Cipher.getInstance("AES/CBC/PKCS5Padding");
                }
                cipher = f21155a;
            } catch (Throwable th) {
                throw th;
            }
        }
        return cipher;
    }
}
