Module 3.1 - Encryption
Module 3.1 - Encryption
1
Encryption / Decryption with Python
Learning Objectives
• You will be able to program simple cipher algorithms, like the Caesar cipher and
the transposition cipher
• You will be able to read and to write files to the file system with Python
• You will be able to break substitution ciphers by frequency analysis
• You will be able to use the RSA cryptography library in Python
• You will be able to understand timing attacks against python login inputs
Introduction to Encryption
/ Decryption
Intelligence Gathering
Disciplines
Intelligence
Intelligence-gathering by
interception of signals
SIGINT
The Enigma machine was a cipher machine used
during World War II for encrypting and decrypting
secret messages.
It’s a s e a sy a s that.
‘string’ b’bytes’
decode
T h e chr() a n d ord()functions
chr(ascii_value) returns a st r i n g re p re s e nt i n g a c h a ra c te r w h o s e U n i c o d e
c o d e p o i nt is t h e i nte ge r ascii_value. I n other words, it ta ke s a n A S C I I value (or
U n i c o d e c o d e point) a s i n p u t a n d returns t h e c o r r e s p o n d i n g c h a ra c te r a s a
string.
o Fo r exa m p le , chr(65) returns t h e strin g 'A', chr(97) returns t h e strin g 'a', a n d so on.
o Fo r exa m p le , ord('A') returns th e inte ge r 65, ord('a') returns t h e inte ge r 97, a n d so on.
Lab 2.1
Breaking the Caesar Cipher
o S o m e t i m e s w e will w a n t to re a d t h e m , a n d s o m e t i m e s to write
them.
W e will u s e o n e of t wo m o d e s :
o “rb” – u s e d for r e a d i n g files
S t e p 2 – R e a d i n g t h e d ata – u s i n g t h e m e t h o d .read()
S t e p 3 – C l o s i n g t h e file – u s i n g t h e m e t h o d .close()
R e a d i n g a F i l e (cont.)
Writing a File (process)
S t e p 1 – O p e n i n g t h e file – u s i n g t h e open() function, a n d g i v i n g t h e path, a n d
t h e m o d e ‘wb’
S t e p 3 – C l o s i n g t h e file – u s i n g t h e m e t h o d .close()
W r i t i n g a F i l e (cont.)
T h e P a t h of t h e F i l e to Wr i t e
T h e p a t h yo u g i ve t h e open() f u n c t i o n w h e n wr i t i n g c a n b e a n existent file, or
a n inexistent file.
data = file.read(100)
data = file.read(50)
W h e r e is t h e
p o i nte r n o w ?
8 b y t e s in!
C h a n g i n g the Pointer Position
I c a n c h a n g e t h e pointer ’s position b y u s i n g t h e m e t h o d .seek() a n d g i v i n g the
position to c h a n g e to.
R e a d i n g t h e F i l e Tw i c e
So, in order for u s to reread t h e file (use t h e .read() m e t h o d twice), all w e n e e d
to d o is to reset t h e pointer to t h e b e g i n n i n g of t h e file!
Summary
U p until now, o p e n i n g a file to o k a few lines, a n d yo u h a d to m a k e sure you
r e m e m b e r to close t h e file.
The with Keyword - Reading
U s i n g with, w e c a n h a n d l e files easier!
Explanation:
o W e w a n t to m a i n ta i n t h e Unicode en co d i n g of th e text
o W h e n e v e r w e re a d or write plain text files, there are certain by te s that m a k e the
writing /reading stop w h e n t h e pointer re a c h e s t h e m
It is i m p o r ta nt to u s e appropriate ke y l e n g t h s a n d regularly u p d a t e ke ys to
m a i n t a i n t h e security of R S A encryption.
Cryptography libraries in
Python
https://pypi.org/project/rsa/
I n P y t h o n , t i m i n g atta c ks c a n o c c u r w h e n c o m p a r i n g st r i n g s or p e r fo r m i n g
other operations t h at ta ke different a m o u n t s of t i m e d e p e n d i n g o n t h e i n p u t
data.
Example…
Learning Objectives
• You will be able to program simple cipher algorithms, like the Caesar cipher and
the transposition cipher
• You will be able to read and to write files to the file system with Python
• You will be able to break substitution ciphers by frequency analysis
• You will be able to use the RSA cryptography library in Python
• You will be able to understand timing attacks against python login inputs