package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class RC4Engine implements org.bouncycastle.crypto.StreamCipher {
    private byte[] a = null;
    private int b = 0;
    private int c = 0;
    private byte[] d = null;
    private boolean e;

    public RC4Engine() {
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(new org.bouncycastle.crypto.constraints.DefaultServiceProperties(getAlgorithmName(), 20));
    }

    private void a(byte[] bArr) {
        this.d = bArr;
        this.b = 0;
        this.c = 0;
        if (this.a == null) {
            this.a = new byte[256];
        }
        for (int i = 0; i < 256; i++) {
            this.a[i] = (byte) i;
        }
        int length = 0;
        int i2 = 0;
        for (int i3 = 0; i3 < 256; i3++) {
            int i4 = bArr[length] & 255;
            byte[] bArr2 = this.a;
            byte b = bArr2[i3];
            i2 = (i4 + b + i2) & 255;
            bArr2[i3] = bArr2[i2];
            bArr2[i2] = b;
            length = (length + 1) % bArr.length;
        }
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public byte e(byte b) {
        int i = (this.b + 1) & 255;
        this.b = i;
        byte[] bArr = this.a;
        byte b2 = bArr[i];
        int i2 = (this.c + b2) & 255;
        this.c = i2;
        bArr[i] = bArr[i2];
        bArr[i2] = b2;
        return (byte) (bArr[(bArr[i] + b2) & 255] ^ b);
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public java.lang.String getAlgorithmName() {
        return "RC4";
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.KeyParameter)) {
            throw new java.lang.IllegalArgumentException("invalid parameter passed to RC4 init - " + cipherParameters.getClass().getName());
        }
        byte[] bArrB = ((org.bouncycastle.crypto.params.KeyParameter) cipherParameters).b();
        this.d = bArrB;
        this.e = z;
        a(bArrB);
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(new org.bouncycastle.crypto.constraints.DefaultServiceProperties(getAlgorithmName(), 20, cipherParameters, org.bouncycastle.crypto.engines.Utils.a(z)));
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        if (i + i2 > 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");
        }
        for (int i4 = 0; i4 < i2; i4++) {
            int i5 = (this.b + 1) & 255;
            this.b = i5;
            byte[] bArr3 = this.a;
            byte b = bArr3[i5];
            int i6 = (this.c + b) & 255;
            this.c = i6;
            bArr3[i5] = bArr3[i6];
            bArr3[i6] = b;
            bArr2[i4 + i3] = (byte) (bArr3[(bArr3[i5] + b) & 255] ^ bArr[i4 + i]);
        }
        return i2;
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public void reset() {
        a(this.d);
    }
}
