Download presentation
Presentation is loading. Please wait.
2
Encryption and Decryption
PC01 Term 2 Project Encryption and Decryption
3
Introduction We’ve focused on loops, sorting values, and finding values this term It’s now time to look at a practical example Make programs that use looping and searching Encrypting strings Decrypting strings
4
Introduction Encrypt Decrypt
Transform a string into a coded, secret message Only people “in-the-know” and read this message Decrypt Transform a coded message into plaintext Can only be done by people “in-the-know”
5
Encryption Techniques (ROT13)
Moves a character in the alphabet forward 13 spaces For example A N B O C P Z M If we go past Z, we cycle back to A and resume
6
Encryption Techniques (ROT13) Exercise
Create a new project called “Term2Project”. At the start of the main method, create a string variable and give it a value (e.g. “fox”) Make sure to only use lower case and no other symbols! Turn this string into a character array using ToCharArray() Loop through this array and move each character along 13 spaces (e.g. add 13 to it) You will need to cast this value back to a character Join the array back in to a string and output it after the loop
7
Encryption Techniques (ROT13) Exercise
You may have noticed things looking a bit odd for the latter half of the alphabet Add a check inside the loop to see if the shifted letter is greater than ‘z’ If it is, move it backwards 26 spaces As before, you will need to cast the shift back into a character Run your program and see if you get any more weird outputs All of the encrypted texts should be lower-case
8
Decryption Techniques (ROT13)
Decryption often works in reverse ROT13 encryption moves a character to the right 13 spaces Decryption reverses that Which direction do we move a character in to decrypt? How many spaces do we move a character?
9
Decryption Techniques (ROT13) Exercise
After outputting the encrypted text, try decrypting it You need to do the same as before, but reverse the process Output the text again after decrypting to see if you did it right
10
Encryption Techniques (Caesar Shift)
Very similar to the ROT13 technique In fact, ROT13 is a variant of the Caesar Shift Involves ‘shifting’ characters so many spaces Instead of just 13, we can use whatever number we want From 0 to 25
11
Encryption Techniques (Caesar Shift)
Caesar Shift of 1 (ROT1) A B X Y Caesar Shift of 3 (ROT3) A D C F
12
Encryption Techniques (Caesar Shift)
We can change our ROT13 technique into the Caesar Shift technique Create a variable for the amount to shift by Replace the 13 shift with this variable That’s it Could assign the shift amount a value from the user
13
Encryption Techniques (Caesar Shift) Exercise
Create an integer variable called shift Assign it a value from the user Validate this value so that it is An integer Greater (or equal) to 0 Less than (or equal) to 25
14
Encryption Techniques (Caesar Shift) Exercise
Replace the 13 shift with the shift variable Everything else can stay the same! Test your program using different shift amounts, and see if the encrypted string makes sense Shift 5: funtech kzsyjhm Shift 10: funtech pexdomr
15
Decryption Techniques (Caesar Shift)
Decrypting text using the Caesar Shift is exactly like decrypting using ROT13 They’re practically the same, after all We simply shift backwards the correct number of spaces However, as it’s the Caesar Shift, we need to use the correct number of spaces E.g. if text was shifted 20 spaces, we need to shift 20 spaces backwards
16
Decryption Techniques (Caesar Shift) Exercise
Edit your decryption code so that it supports different shift amounts HINT: You could use your shift variable for this Once done, make sure the input string and the decrypted string are the same
17
Encryption/Decryption Program Exercise
Change your program so that it has a ‘main menu’ that shows options for: Encrypting text using the Caesar Shift Decrypting text using the Caesar Shift Exit If the user chooses to “Encrypt text using the Caesar Shift”, ask them for: The text to encrypt The shift to use And then perform the Caesar Shift on the input text using the given shift – output the encrypted text to the console
18
Encryption/Decryption Program Exercise
If the user chooses to “Decrypt text using the Caesar Shift”, ask them for: The text to decrypt The shift to use And then perform the reverse Caesar Shift on the encrypted text using the given shift – output the decrypted text to the console If the user chooses “Exit”, close the program
19
Encryption/Decryption Program Exercise
Edit your encryption/decryption parts so that they do not alter symbols E.g. exclamation marks, full-stops, and spaces Edit your encryption/decryption parts so that they also work for capital letters An easy way to do this is to turn all letters into their lower case form first before encrypting/decrypting
20
Encryption Cracking (Caesar Shift)
All decryption we’ve done involves knowing the ‘key’ beforehand However, we may not know what this is In this case, we may not know what shift was used There are a few ways of ‘cracking’ encrypted text ‘Easiest’ way is looking at the most common character
21
Encryption Cracking (Caesar Shift)
The most common character in the English language is ‘e’ If we look through encrypted text and find the most common character, we can assume it is ‘e’ E.g. if “t” occurs the most in encrypted text, we can assume that e t If we work out the shift from “e” to the most common character, we can guess the shift that was used We can then decrypt using that shift Note that this is not perfect Doesn’t work well for small bits of text (e.g. words or sentences) Works better as more text is involved (e.g. paragraphs or pages)
22
Encryption Cracking (Caesar Shift) Exercise
Add another menu option for “Cracking Caesar Shift” If chosen, the program should first ask for the text to ‘crack’ It should then go through all the characters (skipping symbols) and find the most common character Then it should calculate the shift from “e” to this character Next it should decrypt the text using this shift and the usual Caesar Shift decryption method Finally it should output the decrypted text to the console
23
Encryption Cracking (Caesar Shift) Exercise
Add another menu option for “Brute Force Caesar Shift” If chosen, the program should ask the user for text to decrypt It should then output the decrypted text for every possible shift, as well as the shift used
24
Encryption Technique (Vigenère Cipher)
Ceasar cipher is fairly easy to brute force Simply output all possible shifts and see the result There is a more advanced technique Vigenère Cipher So secure it took over 300 years to crack after its inception Will add separate menu options for encrypting/decrypting with this technique
25
Encryption/Decryption Program Exercise
Add two menu options to your program Encrypting Text Using Vigenere Decrypting Text Using Vigenere Output dummy text for both menu options (to check that they work) Run your program and make sure the dummy text is output when these menu options are selected
26
Encryption Technique (Vigenère Cipher)
Vigenère Cipher works similarly to Caesar cipher However, there is one big difference Caesar shifts all keys the same amount Vigenère shifts all keys a varying amount Vigenère requires a “key” – a series of letters or words decided in advance Determines the shift of each letter in the plain text Plaintext: ALLISDISCOVEREDFLEEATONCE Cipher: CABLECABLECABLECABLECABLE Ciphertext: CLMTWFITNSXESPHHLFPEVOONI
27
Encryption Technique (Vigenère Cipher)
Key is repeated over and over alongside message to encrypt To encrypt message, letter of key is used as shift for same letter in plain text Essentially ROT shifts, where each letter is a number (based on alphabet) A: ROT0 C: ROT2 J: ROT9
28
Encryption Technique (Vigenère Cipher) Exercise
When “Encrypt Text Using Vigenere” is selected, ask the user for text to encrypt Make this text entirely upper case Then ask the user to enter a key Use validation to ensure that the key: Is NOT empty Contains ONLY alphabetical characters Turn both of these inputs into character arrays
29
Encryption Technique (Vigenère Cipher) Exercise
Loop through the characters in the plain text If the character is a letter (e.g. not a space or symbol or number) perform a Caesar shift on that character Base shift on character in “key” E.g. A ROT0, E ROT4 After shifting plain text character, increment to next character in key Output the encrypted text after the loop
30
Decryption Technique (Vigenère Cipher) Exercise
If user chooses “Decrypt Text Using Vigenere”, make the user input the encrypted text and then the key Make sure key only contains alphabetical characters Loop through the encrypted text (character by character) and perform a reverse shift based on the current character in the key If a character is shifted, increment to next character in key Once decryption is done, output the decrypted text to the console
31
Encryption Cracking (Vigenère Cipher)
The Vigenère Cipher is much harder to crack than Caesar Cipher However, we can sometimes make a good guess at the key and find part of the of the plain text Usually need a bit of thinking on our part to figure out rest of key
32
Encryption Cracking (Vigenère Cipher)
We will add a “Key Guess” option to program Lets user guess a possible key They will input a length Program will estimate each letter in the key Essentially performing lots of smaller letter analysis Find most common character for current letter Also check other characters in plain text that might have been shifted by same letter Assume most common character is E and work backwards to get letter of key Do this for every letter in the key and return result Highly depends on length the user inputs for the key
33
Encryption Cracking (Vigenère Cipher) Exercise
Add a “Guess Key (Vigenere)” menu option If chosen, program should ask the user for the length of the key The program should then, for each individual key character, look at characters in the plaintext affected by this key character E.g. Key is “CAMEL” Encrypted text is “DWWF DWJ A LSI DM ZXCWI WA QPW NXC C A ME L CAMELCAM EL CAME L CAM E LC Characters affected by a C: DWICQC Find most common character and assume is E Work out shift from E to current letter Assume this is key character (e.g. Shift of 4 means character is F) Repeat for all other characters in key Return the string made up of the guessed characters in key
34
Encryption Technique (XOR Cipher)
One final encryption technique XOR Cipher XOR “Exclusive OR” Used on two binary values Returns 1 if one or the other are 1, but not if both 1 or 0 Num1: 7 Num2: 21 (Num3) Num1 XOR Num2: 18
35
Encryption Technique (XOR Cipher)
Characters stored as ASCII in some encodings Integer value from 0 to 255 E.g. ‘a’ 97, ‘1’ 49 Can use XOR on two characters (as integer values are just binary numbers) Returns a different number Can be cast back to a character
36
Encryption Technique (XOR Cipher) Exercise
Add new menu option to program Encrypting Text using XOR This option asks for two strings Plaint text input Key Then turn inputs into char arrays
37
Encryption Technique (XOR Cipher) Exercise
Loop through char[] plain text For each character, perform XOR on character and current key character Can be done using ^ operator (called caret) E.g. ‘a’ ^ ‘b’ is char a XOR char b Store result back in char[] Move on to next character in key char[] If at end of key, loop back to beginning Output encrypted text at end
38
Decryption Technique (XOR Cipher)
Decrypting just as easy as encrypting We take encrypted text and key Perform XOR on them Result is plain text Num3: 10010 Num2: 10101 (Num3 XOR Num2) Num1: 00111
39
Decryption Technique (XOR Cipher) Exercise
Add another menu option to program Decrypting Text using XOR When chosen, ask user for encrypted text and key Convert these to char[] See if you can work out how to decrypt using XOR! HINT: Same process as encrypting
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.