package org.bouncycastle.crypto.paddings;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class PaddedBufferedBlockCipher extends org.bouncycastle.crypto.DefaultBufferedBlockCipher {
    org.bouncycastle.crypto.paddings.BlockCipherPadding o;

    public PaddedBufferedBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this(blockCipher, new org.bouncycastle.crypto.paddings.PKCS7Padding());
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int a(byte[] bArr, int i) {
        int iC;
        int iB = this.k.b();
        if (this.j) {
            if (this.i != iB) {
                iC = 0;
            } else {
                if ((iB * 2) + i > bArr.length) {
                    i();
                    throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
                }
                iC = this.k.c(this.h, 0, bArr, i);
                this.i = 0;
            }
            this.o.a(this.h, this.i);
            int iC2 = iC + this.k.c(this.h, 0, bArr, i + iC);
            i();
            return iC2;
        }
        if (this.i != iB) {
            i();
            throw new org.bouncycastle.crypto.DataLengthException("last block incomplete in decryption");
        }
        org.bouncycastle.crypto.BlockCipher blockCipher = this.k;
        byte[] bArr2 = this.h;
        int iC3 = blockCipher.c(bArr2, 0, bArr2, 0);
        this.i = 0;
        try {
            int iB2 = iC3 - this.o.b(this.h);
            java.lang.System.arraycopy(this.h, 0, bArr, i, iB2);
            return iB2;
        } finally {
            i();
        }
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int c(int i) {
        int i2 = i + this.i;
        byte[] bArr = this.h;
        int length = i2 % bArr.length;
        if (length != 0) {
            i2 -= length;
        } else if (!this.j) {
            return i2;
        }
        return i2 + bArr.length;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int e(int i) {
        int i2 = i + this.i;
        byte[] bArr = this.h;
        int length = i2 % bArr.length;
        return length == 0 ? java.lang.Math.max(0, i2 - bArr.length) : i2 - length;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public void f(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.BlockCipher blockCipher;
        this.j = z;
        i();
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            org.bouncycastle.crypto.params.ParametersWithRandom parametersWithRandom = (org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters;
            this.o.c(parametersWithRandom.b());
            blockCipher = this.k;
            cipherParameters = parametersWithRandom.a();
        } else {
            this.o.c(null);
            blockCipher = this.k;
        }
        blockCipher.init(z, cipherParameters);
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int g(byte b, byte[] bArr, int i) {
        int i2 = this.i;
        byte[] bArr2 = this.h;
        int i3 = 0;
        if (i2 == bArr2.length) {
            int iC = this.k.c(bArr2, 0, bArr, i);
            this.i = 0;
            i3 = iC;
        }
        byte[] bArr3 = this.h;
        int i4 = this.i;
        this.i = i4 + 1;
        bArr3[i4] = b;
        return i3;
    }

    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int h(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        if (i2 < 0) {
            throw new java.lang.IllegalArgumentException("Can't have a negative input length!");
        }
        int iB = b();
        int iE = e(i2);
        if (iE > 0 && iE + i3 > bArr2.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        byte[] bArr3 = this.h;
        int length = bArr3.length;
        int i4 = this.i;
        int i5 = length - i4;
        int iC = 0;
        if (i2 > i5) {
            java.lang.System.arraycopy(bArr, i, bArr3, i4, i5);
            int iC2 = this.k.c(this.h, 0, bArr2, i3);
            this.i = 0;
            i2 -= i5;
            i += i5;
            iC = iC2;
            while (i2 > this.h.length) {
                iC += this.k.c(bArr, i, bArr2, i3 + iC);
                i2 -= iB;
                i += iB;
            }
        }
        java.lang.System.arraycopy(bArr, i, this.h, this.i, i2);
        this.i += i2;
        return iC;
    }

    public PaddedBufferedBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher, org.bouncycastle.crypto.paddings.BlockCipherPadding blockCipherPadding) {
        this.k = blockCipher;
        this.o = blockCipherPadding;
        this.h = new byte[blockCipher.b()];
        this.i = 0;
    }
}
