package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class CFBBlockCipher extends org.bouncycastle.crypto.StreamBlockCipher implements org.bouncycastle.crypto.modes.CFBModeCipher {
    private byte[] b;
    private byte[] c;
    private byte[] d;
    private byte[] e;
    private int f;
    private org.bouncycastle.crypto.BlockCipher g;
    private boolean h;
    private int i;

    public CFBBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher, int i) {
        super(blockCipher);
        this.g = null;
        if (i > blockCipher.b() * 8 || i < 8 || i % 8 != 0) {
            throw new java.lang.IllegalArgumentException("CFB" + i + " not supported");
        }
        this.g = blockCipher;
        this.f = i / 8;
        this.b = new byte[blockCipher.b()];
        this.c = new byte[blockCipher.b()];
        this.d = new byte[blockCipher.b()];
        this.e = new byte[this.f];
    }

    private byte g(byte b) {
        if (this.i == 0) {
            this.g.c(this.c, 0, this.d, 0);
        }
        byte[] bArr = this.e;
        int i = this.i;
        bArr[i] = b;
        byte[] bArr2 = this.d;
        int i2 = i + 1;
        this.i = i2;
        byte b2 = (byte) (b ^ bArr2[i]);
        int i3 = this.f;
        if (i2 == i3) {
            this.i = 0;
            byte[] bArr3 = this.c;
            java.lang.System.arraycopy(bArr3, i3, bArr3, 0, bArr3.length - i3);
            byte[] bArr4 = this.e;
            byte[] bArr5 = this.c;
            int length = bArr5.length;
            int i4 = this.f;
            java.lang.System.arraycopy(bArr4, 0, bArr5, length - i4, i4);
        }
        return b2;
    }

    private byte h(byte b) {
        if (this.i == 0) {
            this.g.c(this.c, 0, this.d, 0);
        }
        byte[] bArr = this.d;
        int i = this.i;
        byte b2 = (byte) (b ^ bArr[i]);
        byte[] bArr2 = this.e;
        int i2 = i + 1;
        this.i = i2;
        bArr2[i] = b2;
        int i3 = this.f;
        if (i2 == i3) {
            this.i = 0;
            byte[] bArr3 = this.c;
            java.lang.System.arraycopy(bArr3, i3, bArr3, 0, bArr3.length - i3);
            byte[] bArr4 = this.e;
            byte[] bArr5 = this.c;
            int length = bArr5.length;
            int i4 = this.f;
            java.lang.System.arraycopy(bArr4, 0, bArr5, length - i4, i4);
        }
        return b2;
    }

    public static org.bouncycastle.crypto.modes.CFBModeCipher j(org.bouncycastle.crypto.BlockCipher blockCipher, int i) {
        return new org.bouncycastle.crypto.modes.CFBBlockCipher(blockCipher, i);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int b() {
        return this.f;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int c(byte[] bArr, int i, byte[] bArr2, int i2) {
        processBytes(bArr, i, this.f, bArr2, i2);
        return this.f;
    }

    @Override // org.bouncycastle.crypto.StreamBlockCipher
    protected byte f(byte b) {
        return this.h ? h(b) : g(b);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public java.lang.String getAlgorithmName() {
        return this.g.getAlgorithmName() + "/CFB" + (this.f * 8);
    }

    public byte[] i() {
        return org.bouncycastle.util.Arrays.j(this.c);
    }

    @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();
            if (cipherParameters != null) {
                this.g.init(true, cipherParameters);
                return;
            }
            return;
        }
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
        byte[] bArrA = parametersWithIV.a();
        int length = bArrA.length;
        byte[] bArr = this.b;
        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.b;
                if (i >= bArr2.length - bArrA.length) {
                    break;
                }
                bArr2[i] = 0;
                i++;
            }
        } else {
            java.lang.System.arraycopy(bArrA, 0, bArr, 0, bArr.length);
        }
        reset();
        if (parametersWithIV.b() != null) {
            this.g.init(true, parametersWithIV.b());
        }
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        byte[] bArr = this.b;
        java.lang.System.arraycopy(bArr, 0, this.c, 0, bArr.length);
        org.bouncycastle.util.Arrays.G(this.e, (byte) 0);
        this.i = 0;
        this.g.reset();
    }
}
