Reputation: 97
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
Upvotes: 0
Views: 2465
Reputation: 1794
To answer your question:
You need to pass your message
inside of Message()
into the encryption class and from there, loop through all of the characters and replace the message
character with its equivalent in the scrambled alphabet. For example character A
you would replace with scrambled[0]
.
To do this you'll need to look at an ASCII Table to look at it's numerical representation. And then do something like:
public String encryptText(String message) {
message = message.toLowerCase();
String encryptedMessage = "";
for(int i = 0; i < message.length; i++) {
encryptedText += scrambled[message[i] - 'a' + 1];
}
return encryptedMessage;
}
With that said, I would highly recommend deleting everything that you've just done. When it comes to Encryption, unless you really know what you're doing you should not be trying to create your own protocol, as cracking it will be trivially easy for anyone competent in encryption.
Here's a simple example of what you should be doing, https://stackoverflow.com/a/4487541/1327636
What you are doing is a substitution cipher, which is not very difficult to crack. The way to cracking substitution ciphers is by:
Counting the frequency of letters in your encrypted text and comparing it to a letter frequency table
Looking for repeating characters, ex. ZZ
Looking at the length of words and comparing the encrypted string character to the frequency table to find popular words such as The
Looking for repeating patterns such as ING
, TH
, ION
Using just those four things can give you a very good statistical probability in cracking substitution ciphers. If you want to read more about them, I would look over here.
Upvotes: 1