Skip to content

Instantly share code, notes, and snippets.

@h3ku
Created March 6, 2018 07:55
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/12e1a934239f4df9c15664508f5208d1 to your computer and use it in GitHub Desktop.
Save h3ku/12e1a934239f4df9c15664508f5208d1 to your computer and use it in GitHub Desktop.
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