Reputation: 53
I'm making a Random Substitution Cipher in Java. Basically, the program asks you for a sentence, you input the sentence, it takes the sentence and using a randomly generated alphabet, encrypts it. The user has the choice of encrypting or decrypting. The encrypted cipher text is then displayed on screen. If the user chooses so, the program will decrypt the cipher and show the original plain text message.
Here's what I have so far:
Random gen = new Random();
PrintWriter write = new PrintWriter(new File("CryptCode.txt"));
char[] chars = new char[] {'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'};
//'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'};
char[] cryptList = new char[26];
int tracker = 0;
while(tracker < 26)
{
int num = gen.nextInt(26);
if(cryptList[num] == '\u0000')
{
cryptList[num] = chars[tracker];
tracker++;
}
}
for(int i = 0; i < 26; i++)
{
write.println(chars[i] + " " + cryptList[i]);
}
write.close();
This just generates the random alphabet. I have no idea how I would implement the actual encrypt method though. I can handle the file IO and the prompts to the user on my own. I just can't understand how to go about creating a substitution algorithm. Any help is greatly appreciated. I can probably figure out the decryption method once I have the encryption method in front of me, but as of right now I have no idea how to proceed. Thanks!
Upvotes: 1
Views: 8276
Reputation: 861
I have done this before, and this is how I did it:
private static char[] 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'};
public static char[] shiftAlphabet(int shift)
{
char[] newAlpha = new char[26];
for (int i = 0; i < 26; i++)
{
if(((i + shift) < 26) && ((i + shift) >= 0))
{
newAlpha[i] = alphabet[i + shift];
}
else if ((i + shift) >= 26)
{
newAlpha[i] = alphabet[i + shift - 26];
}
}
return newAlpha;
}
public static String encrypt(String s, int shift)
{
String e = "";
for(int i = 0; i < s.length(); i++)
{
char letter = s.charAt(i);
if (letter != ' ')
{
int f = find(alphabet, letter);
if(((f + shift) < 26) && ((f + shift) >= 0))
{
letter = alphabet[f + shift];
}
else if ((f + shift) >= 26)
{
letter = alphabet[f + shift - 26];
}
e = e + String.valueOf(letter);
}
else
{
e = e + " ";
}
}
return e;
}
public static int find(char[] c, char c2)
{
int w = 0;
for(int i = 0; i < c.length; i++)
{
if(c[i] == (c2))
w = i;
}
return w;
}
The first method shifts the alphabet by a certain integer, and the second method encrypts the message, by using the last method, which finds the locations of the letter in the alphabet (it returns a value from 0 to 25). So using these methods, in your main method, you can generate a random number from 1 to 26 and use that as your "shift" variable, and then prompt the user for the message he/she would like to encrypt.
Upvotes: 1
Reputation: 1
Substitution algorithm means it substitutes a character with some character
you can go in multiple ways
1.substitute the character with nth character from now.
example: a is replaced by e
b is replaced by f
2.substitute the character with (n+i)th character from now. to replace abf
example: a is replaced by f(a=1,n=5)
b is replaced by g(b=2,n=5)
f is replaced by m(f=3,n=5)
generally every body uses rot13 as one of substitution cipher, which replaces with 13th character of string
Upvotes: 0