EDIT2: Guess the question goes here. So my first question is, am I scanning the file correctly (probably not)?
So I am trying to build an encoder in Java using a Vigenere cipher. The key and plain text files are suppose to be passed in the command line with the execution of the program (like ">java vigenere k1.txt pt1.txt"). Then its suppose to scan the file for the text (which is one of the parts I don't think I did right) where it will remove all non-alphabetic letters and convert all of the text to lower text, for each (key and plain text), and pad the plain text with 'x' if its shorter than the key text when repeated. From there its suppose to encrypt the plain text by shifting the characters by the key characters % 26.
import java.util.Scanner;
public class vigenere {
//string length constant
public static final int MAX_LENGTH = 512;
public static void main(String[] args) {
//obtaining file names
String keyFile = args[0];
String plainTextFile = args [1];
//scanning files
Scanner keyContents = new Scanner(keyFile);
keyContents.useDelimiter("//Z");
Scanner plainTextContents = new Scanner(plainTextFile);
plainTextContents.useDelimiter("//Z");
//turn scanners into strings
String key = keyContents.next();
String plainText = plainTextContents.next();
//conver string to stringbuilder
StringBuilder key1 = new StringBuilder(key);
StringBuilder plainText1 = new StringBuilder(plainText);
//set length of strings
key1.setLength(MAX_LENGTH);
plainText1.setLength(MAX_LENGTH);
//set stringbuilder back to string
key = key1.toString();
plainText = plainText1.toString();
System.out.println("Original Key:");
System.out.println(key);
System.out.println("****************************");
System.out.println("Original PlainText:");
System.out.println(plainText);
System.out.println("****************************");
encrypt(plainText, key);
}
static void encrypt(String plainText, String key) {
//new strings
String newKey = "";
String newPlainText = "";
//convert to all lowercase
key = key.toLowerCase();
plainText = plainText.toLowerCase();
for(int i=0; i<key.length(); i++) {
char currentKey = key.charAt(i);
if(currentKey < 'a' || currentKey > 'z')
continue;
newKey += (char)(currentKey);
}
System.out.println(newKey);
for(int i=0, j=0; i<plainText.length(); i++) {
char currentPlainText = plainText.charAt(i);
if(currentPlainText < 'a' || currentPlainText > 'z')
continue;
newPlainText += (char)((currentPlainText + newKey.charAt(j) % 26) + 'A');
}
System.out.println(newPlainText);
}
}
The outputs currently are:
Original Key:
k1.txt
****************************
Original PlainText:
p1a.txt
****************************
ktxt
´¥¸¼¸