Member Avatar for coalman987

Hi all I'm new and I'm horrible at Java. I've written this decryption code for a Caesar cipher but now I need to decrypt a one-time pad. Is there any way to simply work with what I have and rewrite it to work with a one-time pad? Or must I start over. Here is what I have.

import java.io.*;

class Decrypt {
	public static void main(String[] args) {
				
		int rotation = Integer.parseInt(args[1]);
		
		try {
			FileInputStream input = new FileInputStream(args[0]);
			FileOutputStream output = new FileOutputStream("out.txt");
	

			while(input.available()>0) { 
				  
				  int theByte = input.read();
				  theByte = theByte - rotation;
				  output.write(theByte);
			}
				  
		}
		catch(Exception e) {
			
		}
	
	
	}
}

From Wikipedia (red emphasis added by me)...

In cryptography, the one-time pad (OTP) is a type of encryption, which has been proven to be impossible to crack if used correctly. Each bit or character from the plaintext is encrypted by a modular addition with a bit or character from a secret random key (or pad) of the same length as the plaintext, resulting in a ciphertext. If the key is truly random, as large as or greater than the plaintext, never reused in whole or part, and kept secret, the ciphertext will be impossible to decrypt or break without knowing the key.

Thus if you have a 1000 byte file, your key needs to be at least 1000 bytes. Contrast that with the Caesar Cipher, where you have a one digit key (if you can even call it a "key") and your message can be as long as desired. So it seems to me you need TWO input files, one for the input message, one for the key, and ONE output file for the output message, so I think you should start from scratch.

Member Avatar for coalman987

Ok. Thanks. Any hints or pointers on where to start? I don't expect it to be done for me but I'm at an utter loss.

Your Caesaer Cipher looks off actually. Shouldn't there be a wraparound somewhere? What's 'A' - 3 for example? If you're only using capital letters, it's supposed to be 'X', right? And 'X' + 3 would be 'A'. Change it according to whatever you want as far as allowable characters, but there needs to be some handling of that in there somewhere I think.

Anyway, fix that part first, make sure it works. I guess a one-time pad using only capital letters (it's probably better to make it any printable letter) would be this, assuming A is 0, B is 1, ..., Z is 25.

FROG plain
+ JMWC key
------
  ODKI encrypted

So you'd have a file called plain.txt that has "FROG" in it and a file called key.txt that has "JMWC". Set up a loop, read in a character at a time from each file and do your operation on them (either "add" or "subtract" or "XOR" them, whichever you're doing) and output the result to the third file. Do a few (actually more than a few) on paper to make sure that you have the algorithm down before attempting to actually code.

From Wikipedia (red emphasis added by me)...

Thus if you have a 1000 byte file, your key needs to be at least 1000 bytes. Contrast that with the Caesar Cipher, where you have a one digit key (if you can even call it a "key") and your message can be as long as desired. So it seems to me you need TWO input files, one for the input message, one for the key, and ONE output file for the output message, so I think you should start from scratch.

And you need to have the decryption algorithm needed to decrypt the text.
While often an OTP may use a simple substitution cipher, this isn't a given.
At the very least there may be an offset in the key from which to start for example, either fixed or derived from the encrypted data in some way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.