Added XOR Scrambler for Brew data

Very fast obfuscator for brew data so that a client cant easily read it
This commit is contained in:
Sn0wStorm
2019-10-11 18:05:49 +02:00
parent cdb36b1736
commit 9bf3268fb4
6 changed files with 430 additions and 11 deletions
@@ -0,0 +1,67 @@
package com.dre.brewery.lore;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
public class XORScrambleStream extends FilterOutputStream {
private final long seed;
private SeedInputStream xorStream;
private boolean running;
public XORScrambleStream(OutputStream out, long seed) {
super(out);
this.seed = seed;
}
public void start() throws IOException {
running = true;
if (xorStream == null) {
short id = 0;
while (id == 0) {
id = (short) new Random().nextInt();
}
xorStream = new SeedInputStream(seed ^ id);
out.write((byte) (id >> 8));
out.write((byte) id);
}
}
public void stop() {
running = false;
}
@Override
public void write(int b) throws IOException {
if (!running) {
out.write(b);
return;
}
out.write(b ^ xorStream.read());
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
if (!running) {
out.write(b, off, len);
return;
}
byte[] xored = new byte[len];
xorStream.read(xored);
int j = off;
for (int i = 0; i < len; i++) {
xored[i] ^= b[j++];
}
out.write(xored);
}
@Override
public void close() throws IOException {
running = false;
xorStream = null;
super.close();
}
}