package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class PGPCFBBlockCipher implements org.bouncycastle.crypto.BlockCipher {
    private byte[] a;
    private byte[] b;
    private byte[] c;
    private byte[] d;
    private org.bouncycastle.crypto.BlockCipher e;
    private int f;
    private int g;
    private boolean h;
    private boolean i;

    public PGPCFBBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher, boolean z) {
        this.e = blockCipher;
        this.i = z;
        int iB = blockCipher.b();
        this.g = iB;
        this.a = new byte[iB];
        this.b = new byte[iB];
        this.c = new byte[iB];
        this.d = new byte[iB];
    }

    private int e(byte[] bArr, int i, byte[] bArr2, int i2) {
        int i3 = this.g;
        if (i + i3 > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        if (i3 + i2 > bArr2.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        int i4 = 0;
        this.e.c(this.b, 0, this.c, 0);
        for (int i5 = 0; i5 < this.g; i5++) {
            bArr2[i2 + i5] = i(bArr[i + i5], i5);
        }
        while (true) {
            int i6 = this.g;
            if (i4 >= i6) {
                return i6;
            }
            this.b[i4] = bArr[i + i4];
            i4++;
        }
    }

    private int f(byte[] bArr, int i, byte[] bArr2, int i2) {
        int i3;
        int i4 = this.g;
        if (i + i4 > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        if (i2 + i4 > bArr2.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        int i5 = this.f;
        if (i5 == 0) {
            for (int i6 = 0; i6 < this.g; i6++) {
                this.b[i6] = bArr[i + i6];
            }
            this.e.c(this.b, 0, this.c, 0);
            this.f += this.g;
            return 0;
        }
        if (i5 != i4) {
            if (i5 >= i4 + 2) {
                java.lang.System.arraycopy(bArr, i, this.d, 0, i4);
                bArr2[i2] = i(this.d[0], this.g - 2);
                bArr2[i2 + 1] = i(this.d[1], this.g - 1);
                java.lang.System.arraycopy(this.d, 0, this.b, this.g - 2, 2);
                this.e.c(this.b, 0, this.c, 0);
                int i7 = 0;
                while (true) {
                    i3 = this.g;
                    if (i7 >= i3 - 2) {
                        break;
                    }
                    bArr2[i2 + i7 + 2] = i(this.d[i7 + 2], i7);
                    i7++;
                }
                java.lang.System.arraycopy(this.d, 2, this.b, 0, i3 - 2);
            }
            return this.g;
        }
        java.lang.System.arraycopy(bArr, i, this.d, 0, i4);
        byte[] bArr3 = this.b;
        java.lang.System.arraycopy(bArr3, 2, bArr3, 0, this.g - 2);
        byte[] bArr4 = this.b;
        int i8 = this.g;
        byte[] bArr5 = this.d;
        bArr4[i8 - 2] = bArr5[0];
        bArr4[i8 - 1] = bArr5[1];
        this.e.c(bArr4, 0, this.c, 0);
        int i9 = 0;
        while (true) {
            int i10 = this.g;
            if (i9 >= i10 - 2) {
                java.lang.System.arraycopy(this.d, 2, this.b, 0, i10 - 2);
                this.f += 2;
                return this.g - 2;
            }
            bArr2[i2 + i9] = i(this.d[i9 + 2], i9);
            i9++;
        }
    }

    private int g(byte[] bArr, int i, byte[] bArr2, int i2) {
        int i3 = this.g;
        if (i + i3 > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        if (i3 + i2 > bArr2.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        int i4 = 0;
        this.e.c(this.b, 0, this.c, 0);
        for (int i5 = 0; i5 < this.g; i5++) {
            bArr2[i2 + i5] = i(bArr[i + i5], i5);
        }
        while (true) {
            int i6 = this.g;
            if (i4 >= i6) {
                return i6;
            }
            this.b[i4] = bArr2[i2 + i4];
            i4++;
        }
    }

    private int h(byte[] bArr, int i, byte[] bArr2, int i2) {
        int i3;
        int i4;
        int i5 = this.g;
        if (i + i5 > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        int i6 = this.f;
        if (i6 != 0) {
            if (i6 >= i5 + 2) {
                if (i5 + i2 > bArr2.length) {
                    throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
                }
                this.e.c(this.b, 0, this.c, 0);
                int i7 = 0;
                while (true) {
                    i3 = this.g;
                    if (i7 >= i3) {
                        break;
                    }
                    bArr2[i2 + i7] = i(bArr[i + i7], i7);
                    i7++;
                }
                java.lang.System.arraycopy(bArr2, i2, this.b, 0, i3);
            }
            return this.g;
        }
        if ((i5 * 2) + i2 + 2 > bArr2.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        this.e.c(this.b, 0, this.c, 0);
        int i8 = 0;
        while (true) {
            i4 = this.g;
            if (i8 >= i4) {
                break;
            }
            bArr2[i2 + i8] = i(this.a[i8], i8);
            i8++;
        }
        java.lang.System.arraycopy(bArr2, i2, this.b, 0, i4);
        this.e.c(this.b, 0, this.c, 0);
        int i9 = this.g;
        bArr2[i2 + i9] = i(this.a[i9 - 2], 0);
        int i10 = this.g;
        bArr2[i2 + i10 + 1] = i(this.a[i10 - 1], 1);
        java.lang.System.arraycopy(bArr2, i2 + 2, this.b, 0, this.g);
        this.e.c(this.b, 0, this.c, 0);
        int i11 = 0;
        while (true) {
            int i12 = this.g;
            if (i11 >= i12) {
                java.lang.System.arraycopy(bArr2, i2 + i12 + 2, this.b, 0, i12);
                int i13 = this.f;
                int i14 = this.g;
                this.f = i13 + (i14 * 2) + 2;
                return (i14 * 2) + 2;
            }
            bArr2[i12 + i2 + 2 + i11] = i(bArr[i + i11], i11);
            i11++;
        }
    }

    private byte i(byte b, int i) {
        return (byte) (this.c[i] ^ b);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int b() {
        return this.e.b();
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int c(byte[] bArr, int i, byte[] bArr2, int i2) {
        if (this.i) {
            return this.h ? h(bArr, i, bArr2, i2) : f(bArr, i, bArr2, i2);
        }
        return this.h ? g(bArr, i, bArr2, i2) : e(bArr, i, bArr2, i2);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public java.lang.String getAlgorithmName() {
        java.lang.StringBuilder sb;
        java.lang.String str;
        if (this.i) {
            sb = new java.lang.StringBuilder();
            sb.append(this.e.getAlgorithmName());
            str = "/PGPCFBwithIV";
        } else {
            sb = new java.lang.StringBuilder();
            sb.append(this.e.getAlgorithmName());
            str = "/PGPCFB";
        }
        sb.append(str);
        return sb.toString();
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        this.h = z;
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
            reset();
            this.e.init(true, cipherParameters);
            return;
        }
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
        byte[] bArrA = parametersWithIV.a();
        int length = bArrA.length;
        byte[] bArr = this.a;
        if (length < bArr.length) {
            java.lang.System.arraycopy(bArrA, 0, bArr, bArr.length - bArrA.length, bArrA.length);
            int i = 0;
            while (true) {
                byte[] bArr2 = this.a;
                if (i >= bArr2.length - bArrA.length) {
                    break;
                }
                bArr2[i] = 0;
                i++;
            }
        } else {
            java.lang.System.arraycopy(bArrA, 0, bArr, 0, bArr.length);
        }
        reset();
        this.e.init(true, parametersWithIV.b());
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        this.f = 0;
        int i = 0;
        while (true) {
            byte[] bArr = this.b;
            if (i == bArr.length) {
                this.e.reset();
                return;
            }
            if (this.i) {
                bArr[i] = 0;
            } else {
                bArr[i] = this.a[i];
            }
            i++;
        }
    }
}
