Instantly share code, notes, and snippets.

@h3ku h3ku/RC4DropCoder.java Secret
Created Mar 6, 2018

Embed
What would you like to do?
package com.xiaomi.smarthome.library.crypto.rc4coder;
import com.xiaomi.smarthome.library.crypto.Base64Coder;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import javax.crypto.IllegalBlockSizeException;
public class RC4DropCoder {
private static final byte[] b = new byte[1024];
RC4 a;
static {
Arrays.fill(b, (byte) 0);
}
private static boolean c(byte[] bArr) {
return bArr == null || bArr.length == 0;
}
public RC4DropCoder(byte[] bArr) throws SecurityException {
if (c(bArr)) {
throw new SecurityException("rc4 key is null");
} else if (bArr.length != 32) {
throw new IllegalArgumentException("rc4Key length is invalid");
} else {
this.a = new RC4(bArr);
a(b);
}
}
public RC4DropCoder(String str) throws SecurityException {
this(Base64Coder.a(str));
}
public byte[] a(byte[] bArr) throws SecurityException {
if (bArr == null) {
try {
throw new IllegalBlockSizeException("no block data");
} catch (Throwable e) {
throw new SecurityException(e);
}
}
this.a.a(bArr);
return bArr;
}
public String a(String str) {
try {
return new String(a(Base64Coder.a(str)), "UTF-8");
} catch (Throwable e) {
throw new SecurityException(e);
}
}
public byte[] b(byte[] bArr) throws SecurityException {
if (bArr == null) {
try {
throw new IllegalBlockSizeException("no block data");
} catch (Throwable e) {
throw new SecurityException(e);
}
}
this.a.a(bArr);
return bArr;
}
public String b(String str) {
byte[] bArr = null;
try {
bArr = str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
}
return String.valueOf(Base64Coder.a(b(bArr)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment