I'm making an encryption program that encrypts words the user puts in, I have everything working except outputting the encrypted word that was entered. Here's what my code looks like now:
Class 1
public class Encryption {
String encrypt = "test";
String key;
String message;
String alpha;
Encryption(){
encrypt = Encrypt();
}
private String Encrypt(){
String[] alphabet = {
"a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" ,
"n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z"
};
StringBuilder sbAlphabet = new StringBuilder();
for (int i = 0; i <= 25; i++) {
sbAlphabet.append(alphabet[i]);
}
alpha = sbAlphabet.toString();
return encrypt;
}
}
Class 2
package encryption;
import java.util.Random;
public class Key {
String key;
Key() {
key = genKey();
}
private String genKey() {
String[] scrambled = {
"a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" ,
"n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z"
};
int r;
String temp;
String key;
StringBuilder sbScrambled = new StringBuilder();
for (int i = 0; i <= 25; i++) {
r = (int)(25*Math.random() + 1);
temp = scrambled[i];
scrambled[i] = scrambled[r];
scrambled[r] = temp;
}
for (int i = 0; i <= 25; i++) {
sbScrambled.append(scrambled[i]);
}
key = sbScrambled.toString();
return key;
}
}
Class 3
package encryption;
import java.util.Scanner;
public class Message {
Key key = new Key();
Encryption encrypt;
String message;
Message() {
System.out.println("Please enter an input:");
Scanner user_input = new Scanner( System.in);
message = user_input.next();
encrypt = new Encryption();
}
public static void main(String[] args) {
Message message = new Message();
System.out.println(message.key.key);
System.out.println(message.encrypt.alpha);
}
}
The Encryption class returns the alphabet, the Key class encrypts the alphabet randomly and then displays it, and the Message class displays the encrypted key and alphabet to the user. Heres an example of what it looks like when you run it:
Please enter an input:
wew lad
mbptwuklhanjrsedzqfcyvxogi
abcdefghijklmnopqrstuvwxyz
What i'm trying to make the code do is display the users input in the encrypted alphabet, so it would look like this:
Please enter an input:
wew lad
xwx jmt
mbptwuklhanjrsedzqfcyvxogi
abcdefghijklmnopqrstuvwxyz