/****************************************************************************** * This program implements the XOR stream cipher. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.pureprogrammer.Utils; public class XORCipher { static void processFile(String fromFilespec, String toFilespec, String password) throws IOException { BufferedOutputStream ofh = new BufferedOutputStream(new FileOutputStream(toFilespec)); BufferedInputStream ifh = new BufferedInputStream(new FileInputStream(fromFilespec)); int c; final int PLEN = Utils.cpLength(password); int count = 0; while ((c = Utils.getbyte(ifh)) != -1) { int x = c ^ Utils.cpAt(password, count % PLEN); ofh.write(x); ++count; } try {ofh.close();} catch (IOException ex) {}; try {ifh.close();} catch (IOException ex) {}; } public static void main(String[] args) { if (args.length != 3) { System.out.println(Utils.join("", "Syntax: ", "XORCipher", " {fromFilespec} {toFilespec} {password}")); System.exit(1); } String fromFilespec = args[0]; String toFilespec = args[1]; String password = args[2]; try { processFile(fromFilespec, toFilespec, password); } catch (IOException ex) { System.out.println(Utils.join("", "Error: ", Utils.exceptionMessage(ex))); } } }