1.1-Introductie-Programmeren

Encrypting messages

Difficulty: Filled Filled Outlined

Create a program that allows the user to enter any message (String) and encrypts this message using the Caesar cipher. This cipher is a very classic form of encryption, using a random value (most commonly between 1 and 26) and shifts each letter of the message with that specific value. So assume you have a cipher of value 3, the letter ‘a’ should be encoded as a ‘d’ (is it’s 3 letters ahead of the ‘a’ in the alphabet). The letter ‘b’ would become an ‘e’, etc.

To do this easily, you can ask Java to convert any character into an integer by casting it (back and forth). This looks something like:

char character = 'a';
int positionAscii = (int) character;
int newPositionAscii = positionAscii + 4; // In this case, the cipher 4 is used.
char cryptChar = (char) newPositionAscii;

Note that all characters belong to something called the “Ascii-table”. This table can be used to determine the actual int value of any character.

To allow proper encryption, the computer will random the cipher in our program.

Make sure your program works by encrypting these messages: 1) Your own name. 2) “introudction” 3) “hboit” 4) “ineverknewstringscouldbethislongreallytherearealotoflettershere”

Example

Example