package gk;

import fk.r;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import mk.a0;

/* loaded from: classes3.dex */
public final class j implements fk.a {

    /* renamed from: c, reason: collision with root package name */
    private static final byte[] f20049c = new byte[0];

    /* renamed from: a, reason: collision with root package name */
    private final a0 f20050a;

    /* renamed from: b, reason: collision with root package name */
    private final fk.a f20051b;

    public j(a0 a0Var, fk.a aVar) {
        this.f20050a = a0Var;
        this.f20051b = aVar;
    }

    private byte[] c(byte[] bArr, byte[] bArr2) {
        return ByteBuffer.allocate(bArr.length + 4 + bArr2.length).putInt(bArr.length).put(bArr).put(bArr2).array();
    }

    @Override // fk.a
    public byte[] a(byte[] bArr, byte[] bArr2) {
        byte[] c10 = r.o(this.f20050a).c();
        return c(this.f20051b.a(c10, f20049c), ((fk.a) r.i(this.f20050a.L(), c10, fk.a.class)).a(bArr, bArr2));
    }

    @Override // fk.a
    public byte[] b(byte[] bArr, byte[] bArr2) {
        try {
            ByteBuffer wrap = ByteBuffer.wrap(bArr);
            int i10 = wrap.getInt();
            if (i10 <= 0 || i10 > bArr.length - 4) {
                throw new GeneralSecurityException("invalid ciphertext");
            }
            byte[] bArr3 = new byte[i10];
            wrap.get(bArr3, 0, i10);
            byte[] bArr4 = new byte[wrap.remaining()];
            wrap.get(bArr4, 0, wrap.remaining());
            return ((fk.a) r.i(this.f20050a.L(), this.f20051b.b(bArr3, f20049c), fk.a.class)).b(bArr4, bArr2);
        } catch (IndexOutOfBoundsException | NegativeArraySizeException | BufferUnderflowException e10) {
            throw new GeneralSecurityException("invalid ciphertext", e10);
        }
    }
}
