/*** Encrypt
*************************************************************
This program ENCRYPTS a message by changing each letter
to a different letter. For example:
Secret --> gvixvS
*************************************************************************/
public class Encryption
{
public Encryption()
{
String source,cipher;
char oldChar, newChar;
output("Type in a message, and it will be
encrypted.");
source = input("");
cipher = "";
for (int c=0; c<source.length(); c=c+1
)
{
oldChar =
source.charAt(c);
newChar = ' ';
if ( (oldChar >=
'a') && (oldChar <= 'z') )
{
int
ascii = (int)oldChar;
int
pos = ascii - (int)'a';
int
code = ((int)'z') - pos;
newChar = (char)code;
}
else
{
newChar = oldChar;
}
cipher = newChar +
cipher;
}
output(cipher);
}
public static void main(String[] args)
{ new Encryption(); }
public void output(String message)
{
javax.swing.JOptionPane.showMessageDialog(null,message); }
public String input(String prompt)
{ return
javax.swing.JOptionPane.showInputDialog(null,prompt);
}
}
/*************************************************************************
(1) Try encrypting various messages.
Which letters do not get encrypted?
What happens if you type in numbers, like 12345 ?
(2) Find the part of the program that changes 12345 to 54321.
Explain how that works.
(3) Find the part of the program that changes "c" to "x".
Explain how that works.
(4) Type in a message and write down the encrypted version.
Then run the program and type in the encrypted
version -
the program should produce the original message.
This means the same program can DECRYPT its own
ENCRYPTION.
Explain why that works.
(5) Change the program to perform a simpler encryption, like this:
MESSAGE --> NFTTBHF
(6) Invent another encryption concept and program
it.
*************************************************************************/