Hi, just starting Computer Science at Uni' and like to play around making different programs! Haha.
I made a Viginere Cipher, and just want some feedback. I'm sure my lecturer would say it's not 'elegant' code etc, so just be honest :P. Just want to improve =).
import javax.swing.JOptionPane;
public class VigenereCipherComplete
{
public static void main(String[] args)
{
String key, message;
String encrMessage = "";
String[] options = new String[] {"Encrypter", "Decrypter", "Cancel"};
int progChoice, keyLength, length, keyCharValue, messageCharValue, encrCharValue;
int index = 0;
int keyIndex = 0;
char keyChar;
char messageChar;
char encrChar;
boolean progType = true;
// Choice of whether to use the encrypter of decrypter
progChoice = JOptionPane.showOptionDialog(null, "Which would you like to run?", "Program choice",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(progChoice == 0)
progType = true;
else if(progChoice == 1)
progType = false;
else
System.exit(0);
// Input of key and converting it to upper case
key = JOptionPane.showInputDialog(null, "Please input the key you wish to use",
"Key input", JOptionPane.QUESTION_MESSAGE);
key = key.toUpperCase();
// Input of message and converting it to upper case
message = JOptionPane.showInputDialog(null, "Please input the plaintext you wish to use",
"Plaintext input", JOptionPane.QUESTION_MESSAGE);
message = message.toUpperCase();
keyLength = key.length();
length = message.length();
// Encryption/decryption
while(index < length)
{
keyChar = key.charAt(keyIndex);
keyCharValue = (int)keyChar;
messageChar = message.charAt(index);
messageCharValue = (int)messageChar;
if(messageCharValue >= 65 && messageCharValue <= 90)
{
// Encryption
if(progType)
encrCharValue = (keyCharValue - 65) + messageCharValue;
// Decryption
else
{
if(messageCharValue >= keyCharValue)
encrCharValue = (messageCharValue + 65) - keyCharValue;
else
encrCharValue = (((90 - keyCharValue) + messageCharValue) + 1);
}
// Loops ascii values > 90 back from to 65
if(encrCharValue > 90)
{
encrCharValue = (encrCharValue - 90);
encrCharValue = (encrCharValue + 65) - 1;
}
// Converts the char value to a char and adds it to the converted string
encrChar = (char)encrCharValue;
encrMessage = encrMessage + encrChar;
keyIndex++;
}
// Leaves the char as it is and adds it to the converted string if it is not a letter
else
{
encrCharValue = messageCharValue;
encrChar = (char)encrCharValue;
encrMessage = encrMessage + encrChar;
}
index++;
// Resets key so it will loop for the full string wanting to be converted
if (keyIndex >= keyLength)
keyIndex = 0;
}
// Output for encryption
if(progType)
System.out.println("Your key was:\t\t\t\t" + key + "\nYour encrypted message is:\t" + encrMessage);
// Outfor for decryption
else
System.out.println("Your key was:\t\t\t\t" + key + "\nYour decrypted message is:\t" + encrMessage);
}
}
I included everything in case people want to copy and paste to see how it works etc, and after previewing the post, copy and pasting might be the best way as it doesn't display too well.
Thanks :)