what-the-bank/sources/org/bouncycastle/crypto/engines/RC4Engine.java

98 lines
3.2 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.engines;
import com.google.common.primitives.UnsignedBytes;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.OutputLengthException;
import org.bouncycastle.crypto.StreamCipher;
import org.bouncycastle.crypto.params.KeyParameter;
/* loaded from: classes6.dex */
public class RC4Engine implements StreamCipher {
private static final int STATE_LENGTH = 256;
private byte[] engineState = null;
private int x = 0;
private int y = 0;
private byte[] workingKey = null;
@Override // org.bouncycastle.crypto.StreamCipher
public byte returnByte(byte b) {
int i = (this.x + 1) & 255;
this.x = i;
byte[] bArr = this.engineState;
byte b2 = bArr[i];
int i2 = (this.y + b2) & 255;
this.y = i2;
bArr[i] = bArr[i2];
bArr[i2] = b2;
return (byte) (b ^ bArr[(bArr[i] + b2) & 255]);
}
@Override // org.bouncycastle.crypto.StreamCipher
public void reset() {
setKey(this.workingKey);
}
@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 DataLengthException("input buffer too short");
}
if (i3 + i2 > bArr2.length) {
throw new OutputLengthException("output buffer too short");
}
for (int i4 = 0; i4 < i2; i4++) {
int i5 = (this.x + 1) & 255;
this.x = i5;
byte[] bArr3 = this.engineState;
byte b = bArr3[i5];
int i6 = (this.y + b) & 255;
this.y = 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 init(boolean z, CipherParameters cipherParameters) {
if (!(cipherParameters instanceof KeyParameter)) {
StringBuilder sb = new StringBuilder("invalid parameter passed to RC4 init - ");
sb.append(cipherParameters.getClass().getName());
throw new IllegalArgumentException(sb.toString());
}
byte[] key = ((KeyParameter) cipherParameters).getKey();
this.workingKey = key;
setKey(key);
}
@Override // org.bouncycastle.crypto.StreamCipher
public String getAlgorithmName() {
return "RC4";
}
private void setKey(byte[] bArr) {
this.workingKey = bArr;
this.x = 0;
this.y = 0;
if (this.engineState == null) {
this.engineState = new byte[256];
}
for (int i = 0; i < 256; i++) {
this.engineState[i] = (byte) i;
}
int i2 = 0;
int i3 = 0;
for (int i4 = 0; i4 < 256; i4++) {
byte b = bArr[i2];
byte[] bArr2 = this.engineState;
byte b2 = bArr2[i4];
i3 = ((b & UnsignedBytes.MAX_VALUE) + b2 + i3) & 255;
bArr2[i4] = bArr2[i3];
bArr2[i3] = b2;
i2 = (i2 + 1) % bArr.length;
}
}
}