package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class PaddedBlockCipher extends org.bouncycastle.crypto.DefaultBufferedBlockCipher {
    @Override // org.bouncycastle.crypto.DefaultBufferedBlockCipher, org.bouncycastle.crypto.BufferedBlockCipher
    public int a(byte[] bArr, int i) throws org.bouncycastle.crypto.InvalidCipherTextException {
        int iC;
        int iC2;
        int iB = this.k.b();
        if (this.j) {
            if (this.i != iB) {
                iC2 = 0;
            } else {
                if ((iB * 2) + i > bArr.length) {
                    throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
                }
                iC2 = this.k.c(this.h, 0, bArr, i);
                this.i = 0;
            }
            byte b = (byte) (iB - this.i);
            while (true) {
                int i2 = this.i;
                if (i2 >= iB) {
                    break;
                }
                this.h[i2] = b;
                this.i = i2 + 1;
            }
            iC = iC2 + this.k.c(this.h, 0, bArr, i + iC2);
        } else {
            if (this.i != iB) {
                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;
            byte[] bArr3 = this.h;
            int i3 = bArr3[iB - 1] & 255;
            if (i3 > iB) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("pad block corrupted");
            }
            iC = iC3 - i3;
            java.lang.System.arraycopy(bArr3, 0, bArr, i, iC);
        }
        i();
        return iC;
    }

    @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 ? i2 - bArr.length : i2 - length;
    }

    @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;
    }
}
