MODULE 3.
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
HUMINT GEOINT TECHINT MASINT
SIGINT (Signal FININT (Financial
(Human
Intelligence)
Intelligence)
(Geospatial
intelligence)
OSINT (Technical
Intelligence)
Intelligence)
(Measurement and
Signature
(Open- Source Intelligence) 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.
The Enigma machine was a symmetric encryption
device that used a combination of substitution
and permutation (transposition) techniques.
The breaking of the Enigma cipher by the Allies
had a profound impact on the outcome of the
war, as it enabled them to intercept and decrypt
sensitive German communications.
Cipher Methods
Cipher Name Characteristics Type
- Substitution cipher where each letter is shifted by a fixed number of positions in the
Caesar Cipher Symmetric
alphabet. Only has 26 possible keys (1 for each shift value).
- Reorganizes the order of letters in the plaintext, without changing the actual letters
Transposition Cipher Symmetric
themselves. Does not substitute or replace letters.
- Combines both substitution and linear transformation. - Each letter is replaced by a
Affine Cipher Symmetric
mathematical formula (ax + b) mod 26, where a and b are fixed coefficients.
- Each letter in the plaintext is replaced with another letter or symbol according to a
Substitution Cipher Symmetric
fixed substitution rule. - Simplest form is the Caesar cipher.
- Extension of the Caesar cipher where each letter is shifted by a value from a keyword,
Vigenère Cipher repeating the keyword as necessary. - Provides stronger encryption compared to the Symmetric
Caesar cipher.
- Uses a random key that is as long as the plaintext, and the key is used only once. -
One-Time Pad Cipher Symmetric
Provides perfect secrecy when used correctly.
- Public-key encryption algorithm that uses a pair of keys: public key for encryption and
RSA Cipher Asymmetric
private key for decryption. Based on the difficulty of factoring large numbers.
- Public-key encryption algorithm that uses points on an elliptic curve over a finite field
Elliptic Curve Cipher Asymmetric
for encryption and decryption. Provides similar security to RSA but with shorter keys.
ASCII Encoding / Decoding
Azrieli School of Continuing
Studies of the Technion
How Encodings Work?
I n order to create bytes, w e n e e d to e n c o d e strings.
A n e n c o d i n g is a w a y to t u r n a st r i n g into a series of bits
There are m a n y t y p e s of e n c o d i n g available…
T h e m o s t c o m m o n c h a ra c te r set is A S C I I – in w h i c h all c h a ra c te rs are e n c o d e d
into 1 by te (8 bits). T h e s e files are called plaintext files .
T h e A S C I I Ta b l e
W h a t Is E n c o d i n g ?
To e n c o d e is to translate a st r i n g into t h e by te s t h at represent e a c h letter.
ASCII is t h e easiest e n c o d i n g , useful only wi t h E n g l i s h . W e will learn other
e n c o d i n g s in t h e future.
Fo r exa m p l e , e n c o d i n g ‘A’ u s i n g A S C I I is to translate ‘A’ into t h e h exa d e c i m a l
by te 41, written in P y t h o n a s ‘\x41’.
This t u r n s a string object into a bytes object.
Creating Bytes in P y t hon
It’s a s e a sy a s that.
T h e b before t h e q u o te s s h o w s t h at t h e se are bytes, n o t a string.
This is w h a t w e c a n u s e in order to write st r i n g s to files!
W e c a n also create by te s b y ourselves, b y wr i t i n g b before t h e st r i n g definition:
Converting B y t e s into Strings
T h e s a m e c a n b e d o n e t h e other w a y around.
B y t e s c a n b e c o nve r te d into t h e st r i n g s t h at t h ey represent b y u s i n g the
.decode() st r i n g met h o d :
encode
‘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.
ord(character) returns a n i nte ge r re p re s e nt i n g t h e U n i c o d e c o d e p o i nt of the
g i ve n character. I n other words, it ta ke s a c h a ra c te r a s i n p u t a n d returns its
c o r r e s p o n d i n g A S C I I value (or U n i c o d e c o d e point) a s a n integer.
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
Azrieli School of Continuing
Studies of the Technion
Lab 2.2
Breaking the Transposition
Cipher
Azrieli School of Continuing
Studies of the Technion
Working with files in Python
Azrieli School of Continuing
Studies of the Technion
T h e os.path a n d Path m o d u l e s
B o t h os.path a n d Path m o d u l e s provide similar functionalities for c h e c k i n g
paths, file existence, a n d directory existence.
However, there are s o m e differences in t h e u s a g e a n d syntax.
Function os.path Path (from pathlib)
Check a path os.path.exists(path) Path(path).exists()
Check if file exists os.path.isfile(path) Path(path).is_file()
Check if directory exists os.path.isdir(path) Path(path).is_dir()
E x a m p l e w i t h os.path
E x a m p l e w i t h Path
W h a t Are Files?
Files are a l o n g s e q u e n c e of bytes, s ave d o n t h e h a r d disk.
o N o t c h a ra c te rs - N o t letters – By te s ( = 8 Bits )
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.
o Thankfully, this is very easy to d o in Python!
A c c e s s i n g a File
To a c c e s s a file, w e will u s e t h e o p e n function.
O p e n receives t wo inputs: t h e file path, a n d t h e m o d e .
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
o “wb” – u s e d for wri t i n g files
C l o s i n g t h e File
W h e n w e a c c e s s a file a n d o p e n it, our O p e r a t i n g S y s t e m l o c ks it only for us.
This m e a n s n o o n e else c a n a c c e s s it wh i l e t h e file is open.
This m e a n s t h at o n c e w e are d o n e u s i n g t h e file (reading /writing), w e will
a l ways w a n t to close it. If w e don’t others will n o t b e able to a c c e s s it.
W e close t h e file u s i n g t h e .close() m e t h o d .
R e a d i n g a File
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 ‘rb’
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 2 – Wr i t i n g t h e d ata – u s i n g t h e m e t h o d .write(), a n d g i v i n g t h e bytes
w e w a n t to write.
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.
o If t h e file doesn’t exist – a n e w file will b e created.
o If t h e file exists – t h e ex i st i n g file will b e e ra s e d a n d replaced.
But, if t h e directory t h e file is l o cate d in doesn’t exist – a n error will b e raised.
Reading
S p l i t t i n g Te x t i nto L i n e s
W e c a n u s e t h e .split() m e t h o d , wi t h t h e n e w l i n e c h a ra c te r a s t h e delimiter.
W e c a n also u s e t h e .splitlines() m e t h o d , w h i c h is m u c h easier!
R e a d i n g L i n e s f r o m a File
T h e .read() M e t h o d
Re a d , w h e n exe c u te d wi thout a n input, re a ds all of t h e by te s of
t h e file. W h a t h a p p e n s if w e u s e it twice?
File Pointer
J u s t like in variables, our pointer s h o w s u s w h e r e in t h e file w e
re a d from.
W h e n t h e file h a s just b e e n ope ne d , t h e file pointer points to the
b e g i n n i n g of t h e file. A s t h e file is b e i n g read, t h e pointer
a d va n c e s over t h e by te s of t h e file.
W h e n all by te s h ave b e e n read, t h e pointer is already at t h e e n d of
t h e file, so e xe c u t i n g t h e .read() m e t h o d a ga i n will re a d 0 m o r e
bytes.
File Pointer in Action
abcdefghijklmnopqrstuvwxyz
.read(num_of_bytes)
I n ste a d of u s i n g .read() to re a d t h e w h o l e file, w e c a n g i ve it t h e n u m b e r of
by te s to read:
# Read the first 100 bytes from the file
data = file.read(100)
# Read the next 50 bytes from the file
data = file.read(50)
Note: file.iter_content() re a d s t h e c o nte nt s of a file in c h u n k s a s bytes
objects, typically u s e d for p ro c e s s i n g l a rge files t h at m a y n o t fit into m e m o r y.
abcdefghijklmnopqrstuvwxyz
W h e r e is M y P o i n t e r N o w ? -
.tell()
W e c a n u s e t h e .tell() m e t h o d to a s k w h e r e t h e pointer is currently placed:
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!
T h e wi t h creates t h e open() object a n d s ave s it in t h e variable f.
It also m a k e s sure to exe c u te .close() for you, e ve n if a n error happens!
T h e w i t h K e y w o r d iteration b y l i n e s
I n P y t h o n , a file object iterates over t h e lines of a file b y default. E a c h line is
treated a s a se p a rate st r i n g e l e m e n t in t h e iteration. This is c o m m o n l y u s e d for
r e a d i n g text files line b y line.
Fo r exa m p l e , co n si d e r t h e following code:
# Open a file in text mode for reading
with open("file.txt", "r") as file:
# Iterate over the lines in the file
for line in file:
# Process the line
print(line.strip())
T h e with K e y w o r d - W r i t i n g
M o d e s W i t h o u t ‘b’
A p a r t f ro m m o d e s ‘rb’ a n d ‘wb’, there are t h e m o d e s ‘r’ a n d ‘w’.
W h e n w o r k i n g wi t h d ata stream, like f ro m t h e requests m o d u l e , it is better to
n o t u s e m o d e s ‘r’ a n d ‘w’!
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
o W e will usually write a n d re a d files that are n ot plain text, w h i c h m e a n s that th e s e
by te s s h o u l d b e ignored.
Asymmetric Encryption:
The RSA algorithm
Azrieli School of Continuing
Studies of the Technion
R S A Encryption
R S A encryption is a widely u s e d p u b l i c - ke y c r y p to g ra p h i c a l g o r i t h m t h at allows
s e c u re c o m m u n i c a t i o n over t h e internet.
N a m e d after its inventors R o n Rivest, A d i S h a mi r, a n d L e o n a r d A d l e m a n , R S A
e n c r y p t i o n is b a s e d o n t h e m a t h e m a t i c a l c o n c e p t s of p r i m e n u m b e r s a n d
m o d u l a r arithmetic.
It is widely u s e d for s e c u re d ata transmission, digital signatures, a n d key
e x c h a n g e in various applications, i n c l u d i n g online b a n k i n g , s e c u re m e s s a g i n g ,
a n d e-c o m m e rc e .
How R S A Encryption Works
R S A e n c r y pt i o n u s e s a pair of keys: a p u b l ic key a n d a private key.
T h e p u b l i c ke y is u s e d for encryption, a n d it is freely s h a re d wi t h others.
T h e private ke y is ke p t secret a n d is u s e d for decryption.
T h e p ro c e s s of R S A e n c r y pt i o n involves t h e following steps:
o K e y Generation: T h e user g e n e ra te s a pair of ke y s - a p u b l i c ke y a n d a private key.
o Encryption: T h e s e n d e r u s e s th e recipient's p u b l i c ke y to e n c r y p t th e plaintext
message.
o Decryption: T h e recipient u s e s their private ke y to d e cr y pt t h e ciphertext m e s s a g e
a n d obta in t h e original plaintext .
How R S A Encryption Works
Vi s u a l
One-way Functions
O n e - w a y f u n c t i o n s are a f u n d a m e n t a l c o n c e p t in m o d e r n cr yptography.
O n e - w a y f u n c t i o n s are m a t h e m a t i c a l f u n c t i o n s t h at are easy to c o m p u t e in
o n e direction, b u t c o m p u tat i o n a l l y difficult to reverse.
Also k n o w n a s trapdoor functions, a s t h ey allow for efficient c o m p u t a t i o n in
o n e direction, b u t are practically infeasible to reverse w i t h o u t a special
"trapdoor" information.
The R S A Algorithm
S e l e c t i n g t wo l a rge p r i m e n u m b e rs , p a n d q.
C a l c u l at i n g n = p * q, w h i c h is u s e d a s t h e m o d u l u s for b o t h p u b l i c a n d private
keys.
C a l c u l at i n g Euler's totient function, φ(n) = (p - 1)(q - 1).
C h o o s i n g a n i nte ge r e s u c h t h at 1 < e < φ(n) a n d gcd(e, φ(n)) = 1, w h i c h b e c o m e s
t h e p u b l i c ke y exponent.
C a l c u l at i n g d, t h e m o d u l a r multiplicative inverse of e m o d u l o φ(n), w h i c h
b e c o m e s t h e private ke y exponent.
P u b l i c ke y is (n, e) a n d private ke y is (n, d).
S e c u r i t y of R S A E n c r y p t i o n
R S A e n c r y pt i o n is c o n s i d e re d s e c u re b e c a u s e it is b a s e d o n t h e difficulty of
fa c to r i n g l a rge c o m p o s i t e n u m b e r s into their p r i m e factors.
T h e security of R S A e n c r y pt i o n relies o n t h e l e n g t h of t h e ke ys used. L o n g e r
ke ys provide h i g h e r security b u t also require m o r e p ro c e s s i n g p o w e r for
e n c r y p t i o n a n d decryption.
However, wi t h t h e a d ve nt of q u a n t u m c o m p u te rs , w h i c h c a n efficiently factor
l a rge n u m b e rs , R S A e n c r y pt i o n m a y b e c o m e vulnerable to atta c ks in the
future.
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
Azrieli School of Continuing
Studies of the Technion
Python Library For Encryption
Library Main Characteristics
- Provides functions for generating RSA key pairs, encrypting and decrypting
rsa messages using RSA algorithm. Widely used for secure communication and
digital signatures.
- Provides a wide range of cryptographic recipes and primitives in a high-level,
cryptography easy-to-use interface. Actively maintained and widely used for cryptography in
Python applications.
- Deprecated library for cryptography in Python, no longer actively maintained
pycrypto or recommended for new projects. Not recommended for use in new projects
due to lack of active maintenance and security updates.
Example…
>>> import rsa
>>> (public_key, private_key) = rsa.newkeys(2048)
>>> type(public_key)
<class 'rsa.key.PublicKey'>
>>> type(private_key)
<class 'rsa.key.PrivateKey'>
>>> data = b"Hello, red team!"
>>> encrypted_data = rsa.encrypt(data, public_key)
>>> print(encrypted_data)
…snip…
>>> decrypted_data = rsa.decrypt(encrypted_data,
private_key)
>>> print(decrypted_data)
b'Hello, red team!'
Security Concerns
https://pypi.org/project/rsa/
What are timing attacks?
Ti m i n g Attacks in P y t h o n
T i m i n g atta c ks exploit t h e variation in exe c u t i o n t i m e of c o d e to infer sensitive
information.
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.
T i m i n g atta c ks c a n b e u s e d to l e a k information s u c h a s passwords, e n c r y pt i on
keys, or other confidential 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