Skip to content

Instantly share code, notes, and snippets.

@h3ku

h3ku/RC4.java Secret

Created March 6, 2018 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h3ku/16772804adf0bb75a442ca26afb5c504 to your computer and use it in GitHub Desktop.
Save h3ku/16772804adf0bb75a442ca26afb5c504 to your computer and use it in GitHub Desktop.
package com.xiaomi.smarthome.library.crypto.rc4coder;
final class RC4 {
private int a;
private int b;
private final byte[] c = new byte[256];
public RC4(byte[] bArr) {
int i;
int length = bArr.length;
for (i = 0; i < 256; i++) {
this.c[i] = (byte) i;
}
i = 0;
for (int i2 = 0; i2 < 256; i2++) {
i = ((i + bArr[i2 % length]) + this.c[i2]) & 255;
byte b = this.c[i2];
this.c[i2] = this.c[i];
this.c[i] = b;
}
this.a = 0;
this.b = 0;
}
public byte a() {
this.a = (this.a + 1) & 255;
this.b = (this.b + this.c[this.a]) & 255;
byte b = this.c[this.a];
this.c[this.a] = this.c[this.b];
this.c[this.b] = b;
return this.c[(this.c[this.a] + this.c[this.b]) & 255];
}
public void a(byte[] bArr) {
for (int i = 0; i < bArr.length; i++) {
bArr[i] = (byte) (bArr[i] ^ a());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment