Pythonfor Beginner Hack The Caesar Cipher
Pythonfor Beginner Hack The Caesar Cipher
2
Intro............................................................................................................................................. 3
Python Essental .......................................................................................................................... 4
Installation............................................................................................................................... 4
Python Install...................................................................................................................... 4
Pycharm IDE Installation.....................................................................................................5
If Statement............................................................................................................................. 6
Example check a number is even or odd............................................................................6
Hint..................................................................................................................................... 6
Solution............................................................................................................................... 7
For Statement......................................................................................................................... 7
Exmple : Count the Number of Even and Odd....................................................................7
Hint..................................................................................................................................... 7
Solution............................................................................................................................... 7
String....................................................................................................................................... 8
Function.................................................................................................................................. 8
Encrypt Message with Caesar Cipher..........................................................................................9
Manually Encrypt..................................................................................................................... 9
Automate Encrpyt.................................................................................................................. 10
Decrypt Caesar Cipher Message...............................................................................................11
Manually Decrypt................................................................................................................... 11
Automate Decrypt.................................................................................................................. 12
Hack Caesar Cipher Message...................................................................................................14
The Brute-Force Attack......................................................................................................... 14
Intro
The Caesar cipher is named after Julius Caesar, who, according to Suetonius, used it with a
shift of three to protect messages of military significance. While Caesar's was the first recorded
use of this scheme, other substitution ciphers are known to have been used earlier.[4][5]
"If he had anything confidential to say, he wrote it in cipher, that is, by so changing the order of
the letters of the alphabet, that not a word could be made out. If anyone wishes to decipher
these, and get at their meaning, he must substitute the fourth letter of the alphabet, namely D,
for A, and so with the others."
His nephew, Augustus, also used the cipher, but with a right shift of one, and it did not wrap
around to the beginning of the alphabet:
"Whenever he wrote in cipher, he wrote B for A, C for B, and the rest of the letters on the same
principle, using AA for Z."
Evidence exists that Julius Caesar also used more complicated systems,[6] and one writer, Aulus
Gellius, refers to a (now lost) treatise on his ciphers:
"There is even a rather ingeniously written treatise by the grammarian Probus concerning the
secret meaning of letters in the composition of Caesar's epistles."
It is unknown how effective the Caesar cipher was at the time, but it is likely to have been
reasonably secure, not least because most of Caesar's enemies would have been illiterate and
others would have assumed that the messages were written in an unknown foreign language.[7]
There is no record at that time of any techniques for the solution of simple substitution ciphers.
The earliest surviving records date to the 9th century works of Al-Kindi in the Arab world with the
discovery of frequency analysis.[8]
Python Essental
Installation
Python Install
We will install python with anaconda distribution, I recommand this because all the package
already tested carefully. Go to https://anaconda.org/ and click to download
Click to 64 bit and Python 3.6 version and download installer to local
Then try to run the code and you will see ‘hello world’ printout.
If Statement
In order to write useful programs, we almost always need the ability to check conditions and
change the behavior of the program accordingly. Conditional statements give us this ability. The
simplest form is the if statement, which has the genaral form:
if BOOLEAN_EXPRESSION:
STATEMENTS
Hint
● Use % operator to get remaining of division between a number and 2
● Use if statement
Solution
#number = 4
number = 5
mod = num % 2
if mod > 0:
print("This is an odd number.")
else:
print("This is an even number.")
For Statement
The for loop processes each item in a sequence, so it is used with Python’s sequence data
types - strings, lists, and tuples.
Each item in turn is (re-)assigned to the loop variable, and the body of the loop is executed.
The general form of a for loop is:
for LOOP_VARIABLE in SEQUENCE:
STATEMENTS
This is another example of a compound statement in Python, and like the branching statements,
it has a header terminated by a colon (:) and a body consisting of a sequence of one or more
statements indented the same amount from the header.
Hint
● Use % to check if a number divible for 2
● Use in to check every item inside tuple
Solution
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple
count_odd = 0
count_even = 0
for x in numbers:
if not x % 2:
count_even+=1
else:
count_odd+=1
print("Number of even numbers :",count_even)
print("Number of odd numbers :",count_odd)
String
Following are some essential string function will be used in our Caesar Cipher program
Function
In Python, a function is a named sequence of statements that belong together. Their primary
purpose is to help us organize programs into chunks that match how we think about the
problem.
The syntax for a function definition is:
def NAME( PARAMETERS ):
STATEMENTS
For example we create a funtion that return number of even inside a number list
def number_of_even(list_number):
even_number = 0
for x in list_number:
if x % 2 == 0:
even_number = even_number + 1
return even_number
numbers = [1,2,3,4,5,6,7,8,9]
print('number of even : ', number_of_even(numbers))
● To encrypt a message use Caesar Cipher, you will encrypt each alphabe character in
message.
● To encrypt a character, you will replace the original character by a new character.
● The new character is found from original one by just shift original a number of time on
alphabe table. The number of shift is called KEY. For example, if KEY = 3, “A” character
will become “D” as show up above table.
● Using above rules, message “I love programming” will become “L oryh surjudpplqj”
Automate Encrpyt
Using python programming language, we will transfer sequent of encrypt to a program with
following notes:
● Using for in loop to access every character inside a string
● Using upper() function to make all character inside string become upper case
● Using find() function to findout character position inside string
● Using len() global function to get length of string
● Access character inside string with index
# org_message = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alphabe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
key = 3
encrypt_message = ''
if org_character in alphabe:
org_position = alphabe.find(org_character)
new_position = org_position + key
new_character = alphabe[new_position]
else:
new_character = org_character
print(encrypt_message)
alphabe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
encrypt_message = ''
if org_character in alphabe:
org_position = alphabe.find(org_character)
new_position = org_position + key
new_character = alphabe[new_position]
else:
new_character = org_character
print(encrypt_message)
return encrypt_message
#######################################
org_message = 'I love programming'
key = 3
encrypt_caesar_cipher(org_message,key)
Automate Decrypt
Folowing python code will doing decrypt
alphabe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
key = 3
org_message = ''
if encrypt_character in alphabe:
encrypt_position = alphabe.find(encrypt_character)
org_position = encrypt_position - key
if org_position < 0:
org_position = encrypt_position - key + len(alphabe)
org_character = alphabe[org_position]
else:
org_character = encrypt_character
org_message = org_message + org_character
print(org_message)
Putting above code into function, We will have decrypt function as below
alphabe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
org_message = ''
if encrypt_character in alphabe:
encrypt_position = alphabe.find(encrypt_character)
org_position = encrypt_position - key
if org_position < 0:
org_position = encrypt_position - key +
len(alphabe)
org_character = alphabe[org_position]
else:
org_character = encrypt_character
print(org_message)
return org_message
################################################
encrypt_message = 'L ORYH SURJUDPPLQJ'
key = 3
decrypt_caesar_cipher(encrypt_message, key)
Hack Caesar Cipher Message
The Brute-Force Attack
Have only 26 characters in alphabe, it mean we only need to try out 25 keys in order to findout
the original message. The way of trying all possibility like this called brute-force attack.
● This code use decrypt function which already created from decrypt part.
● To try out 25 possibility, we use rang() function combine with for statement
alphabe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
org_message = ''
if encrypt_character in alphabe:
encrypt_position = alphabe.find(encrypt_character)
org_position = encrypt_position - key
if org_position < 0:
org_position = encrypt_position - key +
len(alphabe)
org_character = alphabe[org_position]
else:
org_character = encrypt_character
print(org_message)
return org_message
def brute_force_caesar_cipher(encrypt_message):
################################################
Run this program and we can see what out put make sense.
K NQXG RTQITCOOKPI
J MPWF QSPHSBNNJOH
I LOVE PROGRAMMING
H KNUD OQNFQZLLHMF
G JMTC NPMEPYKKGLE
F ILSB MOLDOXJJFKD
E HKRA LNKCNWIIEJC
D GJQZ KMJBMVHHDIB
C FIPY JLIALUGGCHA
B EHOX IKHZKTFFBGZ
A DGNW HJGYJSEEAFY
Z CFMV GIFXIRDDZEX
Y BELU FHEWHQCCYDW
X ADKT EGDVGPBBXCV
W ZCJS DFCUFOAAWBU
V YBIR CEBTENZZVAT
U XAHQ BDASDMYYUZS
T WZGP ACZRCLXXTYR
S VYFO ZBYQBKWWSXQ
R UXEN YAXPAJVVRWP
Q TWDM XZWOZIUUQVO
P SVCL WYVNYHTTPUN
O RUBK VXUMXGSSOTM
N QTAJ UWTLWFRRNSL
M PSZI TVSKVEQQMRK