A simple RandomAccessFile
scrambler would be this:
private static final String READ_WRITE = "rw";
private static final Random random = new Random();
public static void scramble(String filePath, int scrambledByteCount) throws IOException{
RandomAccessFile file = new RandomAccessFile(filePath, READ_WRITE);
long fileLength = file.getLength();
for(int count = 0; count < scrambledByteCount; count++) {
long nextPosition = random.nextLong(fileLength-1);
file.seek(nextPosition);
int scrambleByte = random.nextInt(255) - 128;
file.write(scrambleByte);
}
file.close();
}
RandomAccessFile
is exactly what it sounds like, a file that can be read from and written to at any position. You can choose the position by calling randomAccessFile.seek(long position)
.
Random
is also what it sounds like, a factory for random numbers, bytes, etc.
So essentially
A) open the file for read/write at any position
B) get a random position, get a random byte, write the random byte to the random position
C) repeat B) for scrambledByteCount
times
3
solved Can i porposely corrupt a file through Java programming ? Also is it possible to scramble a file contents ?