SlideShare a Scribd company logo
CRYPTOGRAPHY USING
PYTHON
BY: KAUSHIK RAMABHOTLA(DR3ADL0CKS)
$WHOAMI
• I am Kaushik Ramabhotla
• I am an undergrad(sophomore) at Amrita University,Amritapuri,Kerala.
• Electronics and Communications guy.
• I was a part of India’s best CTF – TEAM BIOS.(July ‘18 to April ‘19).
What are u gonna gain through this session??
• Python Overview and Installation
• Reverse cipher
• Caesar cipher
• Rot13 algorithm
• Base64 encryption-decryption
• Simple Substitution Cipher
• Python modules of cryptography
• Substitution cipher
• One Time Pad Cipher (theory)
• Symmetric and asymmetric crypto
• The RSA algorithm
Cryptography using python
What the heck is cryptography?
Cryptography is the science developed soulfully for providing security while exchanging
data or a message between two users
I WOULD SAY IT AS AN ART OF COMMUNICATION VIA CODED
MESSAGES!
BASIC TERMINOLOGY
PLAINTEXT
CIPHER TEXT
ENCRYPTION
DECRYPTI
ON
CRYPTOGRA
PHY
Why use python?
• Python is an open source high level language. It is designed as such that it can be
easily understood by most of the readers. It uses English key words frequently
• To conclude, Python is neither better or worse than any other programming
language as long as you use a good crypto library. However, if you're thinking about
implementing something like RSA yourself or some other crypto algorithm you found
in a book/paper, then most probably the code will be vulnerable unless you're an
expert and you really know what you're doing. This applies to any language.
Cryptography Packagescryptography,
Have a look at them
 Python includes a package called cryptography which provides cryptographic
primitives.
package is achieved through following command:
“pip install cryptography ”
 There are various packages with both high level recipes and low level interfaces
to common cryptographic algorithms such as symmetric ciphers, message
digests and key derivation functions.
 Throughout this tutorial, we will be using various packages of Python for
implementation of cryptographic algorithms.
Reverse cipher
As the name suggests , It is easily understood that it is
an encryption that is just the reverse of the plain text or
for an instance , it will be a water image of our original
message
message = 'kaushik'
translated = ''
i = len(message) - 1
while i >= 0:
translated = translated + message[i]
i = i - 1
print(translated)
Cryptography using python
DISADVANTAGES?
ANYONE CAN EASILY BREAK THE
CIPHER TEXT TO GET THE ORIGINAL
MESSAGE. HENCE, REVERSE CIPHER
IS NOT CONSIDERED AS GOOD
OPTION TO MAINTAIN SECURE
COMMUNICATION CHANNEL.
This cipher is a child’s play!
OH….WAIT, I’VE SEEN YOU!
CAESAR CIPHER
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. It is the most basic encryption known to the world.
IMPLEMENTATION
plain = input('enter the string : ')
shift = int(input('enter the shift : '))
result = ''
for i in range(len(plain)):
char = plain[i]
if char.isupper():
result += chr((ord(char) + shift - 65) % 26 + 65)
else:
result += chr((ord(char) + shift - 97) % 26 + 97)
print(result)
BRUTE FORCE TECHNIQUE(REV-
ENGINEERING)
message = input('enter the cipher text')
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for key in range(len(letters)):
translated = ''
for x in message:
if x in letters:
num = letters.find(x)
num = num - key
if num < 0:
num = num + len(letters)
translated = translated + letters[num]
else:
translated = translated + x
print('key %s: %s' % (key, translated))
ROT -13 ALGORITHM
• It is simply the abbreviated form of “Rotate by 13 pieces”.
• It is a special case of Caesar cipher where the shift is fixed to 13.
• Every letter is shifted by 13 places to encrypt or decrypt the message.
Cryptography using python
DRAWBACK
• The rot13 cipher uses a shift of 13 and also is a special case of
Caesar cipher
• Therefore, the algorithm is very easy to break in by brute-force
method or,
frequency analysis method. Therefore, it does not include any
practical use.
BASE64 ENCRYPTION-DECRYPTION
• BASE64 encryption converts binary data to text format
• When you encode text in ASCII, you start with a text string and
convert it to a sequence of bytes.
• When you encode data in Base64, you start with a sequence of bytes
and convert it to a text string.
CODE PART
• There is a module for base64 in python(in-built)
• For decoding…
import base64
x = base64.b64encode(bytes(input(''), 'utf-8'))
print(x)
import base64
x = base64.b64decode(bytes(input(''), 'utf-8'))
print(x)
DRAWBACKS…
• BASE64 algorithm is basically used to store passwords in databases
• Any online decoder tool can be used to decipher the information you
want
• Or you can just decipher them by yourself!
ONE TIME PAD CIPHER(O.T.P)
• It is similar to vienere cipher.
• It is an unbreakable cipher.
• As the name suggests, key is used one time only and never used
again for any other message to be encrypted
• The key is as long as the given message
• The key is auto generated
ENCRYPTION/DECRYPTION WITH ONE
TIME PAD
• To encrypt a letter, a user needs to write a key underneath the
plaintext. The plaintext letter is placed on the top and the key letter on
the left. The cross section achieved between two letters in the plain
text.
DECRYPTION
• To decrypt a letter, user takes the key letter on the left and finds cipher
text letter in that row. The plain text letter is placed at the top of the
column where the user can find the cipher text letter.
• Python has a handy module named-’onetimepad’ which has a
command line encryption tool that uses encryption mechanism similar
to the algorithm.
FACTS:
• So, if you want to encrypt the message "If you want to survive out
here, you've got to know where your towel is.”, its length is 55
excluding the spaces and punctuation. To encrypt with this O.T.P, we
need a key of length =55(same as plaintext)
• Let’s use the key
“kcqyzhepxautiqekxejmoretzhztrwwqdylbttvejmedbsanybpxqik”.
• Now imagine if a cryptanalyst got hold of the ciphertext, brute force
would not work, because the no. of possibilities are (26^55)=i.e.
666,091,878,431,395,624,153,823,182,526,730,590, 376,250,379,52
8,249,805,353,030,484,209,594,192,101,376 possible keys.
Cryptography using python
VIGENERE CIPHER :
• This cipher is almost as of Caesar cipher but with a little twist.
• Caesar cipher is one-character shift, whereas vigenere cipher involves
multiple alphabets shift
• For encryption the mathematical equation is as follows:
• For decryption the mathematical equation is as follows:
FACTS :
• In this cipher, the key length is directly proportional to security of the
plaintext
CODE PART
plaintext = input()
key = input('enter')
key_length = len(key)
key_as_int = [ord(i) for i in key]
plaintext_int = [ord(i) for i in plaintext]
ciphertext = ''
for i in range(len(plaintext_int)):
value = (plaintext_int[i] + key_as_int[i % key_length]) % 26
ciphertext += chr(value + 65)
print(ciphertext)
ciphertext = input('enter')
key = input()
key_length = len(key)
key_as_int = [ord(i) for i in key]
ciphertext_int = [ord(i) for i in ciphertext]
plaintext = ''
for i in range(len(ciphertext_int)):
value = (ciphertext_int[i] - key_as_int[i % key_length]) % 26
plaintext += chr(value + 65)
print(plaintext)
Encryption part
Decryption
part
MERITS AND DE-MERITS
• The strength of vignere cipher is that it is not susceptible to frequency
analysis due to the fact that the shifts are more. A codebreaker using
frequency analysis method/graph cannot help us in decrypting the
original message. Though it was strong cipher after its invention in
16th century. In 1863, ”Fredrich kakiski” was the first to publish a
successful general attack on the Vigenère cipher
• The primary weakness of the Vigenère cipher is the repeating nature
of its key. If a cryptanalyst correctly guesses the length of the key,
then the ciphertext can be treated as interwoven Caesar ciphers,
which, individually, can be easily broken
CRYPTOGRAPHY WITH PYTHON –
SYMMETRIC AND ASYMMETRIC
CRYPTOGRAPHY
• First we will look into Symmetric part
• Symmetric encryption is a type of encryption where the key is same
for encryption and decryption
TYPES:
• There are two types of symmetric encryption, they are:
1. Block ciphers
2. Stream ciphers
• Block ciphers : In a block cipher, the plaintext is broken into blocks of
a set length and the bits in each block are encrypted together.
• A few of the most popular block ciphers are DES/3DES, AES,
Blowfish, and Twofish.
DATA ENCRYPTION CIPHER(DES)
• DES is a block cipher with a block size of 64bit and a 56bit key since 8
of the 64 bits of the key are not used by the encryption algorithm
(function as check bits only)
BLOCK CIPHER MODES OF OPERATION
• One of the main issue with block ciphers is that it allows only to
encrypt message the same size as their block length. If you use an
algorithm in which a block has a size of 64bit to encrypt a message of
65bit, you need a way to define how the second block should be
encrypted. To issue this problem, different modes of operations were
discovered. We shall discuss the most used operations they are: 1.
ECB 2. ECB modes respectively
ECB MODE(ELECTRONIC CODEBOOK
MODE)
ECB is the simplest mode of operation. In this mode, each part of
plaintext is encrypted separately. The main disadvantage to this mode is
that if there are identical plaintext parts, when encrypted with a given
key, produce same ciphertexts, which allows an attacker to learn some
info about the plaintext.
CIPHER BLOCK CHAINING (CBC) MODE
• In the cipher block chaining, an initialisation vector(I.V) is exclusively
added to the encryption process with the key. For the first round of
encryption, it is a random public value. For the next subsequent
rounds, the I.V becomes the ciphertext of the previous round’s
encryption
IMPLEMENTATION:
import pyDes
data = "DES Algorithm Implementation"
k = pyDes.des("DESCRYPT", pyDes.CBC, "00000000", pad=None, padmode=pyDes.PAD_PKCS5)
d = k.encrypt(data)
print("Encrypted: %r" % d)
print("Decrypted: %r" % k.decrypt(d))
THINGS THAT ARE NEW IN THE PREVIOUS
CODE ARE::
• 1. initialisation vector:
• 2.padding:
ASYMMETRIC CRYPTOGRAPHY
• As discussed earlier, it is quite opposite to symmetric cryptography and
it requires two keys(one for encryption and one for decryption)
• The public key is used for encryption
• The private key is used for decryption
HOW TO GET THE KEYS?
The cryptographic strength is primarily linked to the length of the RSA modulus n. In
2017, a sufficient length is deemed to be 2048 bits.
Cryptography using python
UNDERSTANDING RSA ALGORITHM
• RSA is considered as the most secure way of encryption
• Invented by Rivest, Shamir, Adleman in 1978. and hence named RSA.
• It includes working with prime numbers
• The integers used by this method are sufficiently large to make it
difficult to solve.
• Here also, there are two sets of keys:private and public.
How it works?
STEPS IN RSA ALGORITHM
Step1:generating the RSA modulus
Step 2: generating derived
number(e),public key, private key
Step 3: finally encrypting or decrypting
LET’S DEFINE VARIABLES THAT WILL BE
USED:
• P,q two very large prime numbers.
• N = p*q
• E = public key exponent
• D = private key exponent
• M = unpadded message
• C = cipher text
KEY GENERATION:
• Calculation of phi(Euler's totient function) = (p-1)*(q-1)
• Choose an integer e such that (1<e<phi) and GCD(e,phi)=1
• Normally, e=22i+1(i>0). Small values of e can make RSA vulnerable to
some attacks under certain conditions
• Compute d as d = (1/e) * (mod(phi))
• The pair(e,n) is known as public key
• The pair (d,n) is known as private key
ENCRYPTION AND DECRYPTION
• Computes ciphertext c = me mod n
• Transmits the ciphertext c using a reliable channel
• While decrypting one can compute m = cd mod n (Since c = me mod n,
cd mod n = me*d mod n = m).
PADDING AND PADDING SCHEMES
• RSA without padding is referred as “Textbook RSA”. There are a
number of attacks possible if the message is not padded before RSA-
encryption.
• The padding bytes follow a specific standard, for
example, PKCS,OAEP,etc. padding ensures that
the plaintext does not become vulnerable to
these attacks.
Direct square
root
Hastad’s
broadcast
attack
Chosen
ciphertext
attacks
Cryptography using python
Cryptography using python
Cryptography using python
Cryptography using python
Cryptography using python
Cryptography using python
Cryptography using python
Cryptography using python
Cryptography using python
Ad

Recommended

Cryptography in Python
Cryptography in Python
Amirali Sanatinia
 
Email security
Email security
Baliram Yadav
 
Data Encryption Standard (DES)
Data Encryption Standard (DES)
Haris Ahmed
 
Cryptography
Cryptography
IGZ Software house
 
Caesar cipher
Caesar cipher
Hossain Md Shakhawat
 
Email security
Email security
Ahmed EL-KOSAIRY
 
Cryptography full report
Cryptography full report
harpoo123143
 
Authentication Application in Network Security NS4
Authentication Application in Network Security NS4
koolkampus
 
The-Hacker-Playbook-Practical-Guide-To-Penetration-Testing-2014.pdf
The-Hacker-Playbook-Practical-Guide-To-Penetration-Testing-2014.pdf
prasunkagrawal
 
Key management.ppt
Key management.ppt
Sou Jana
 
Software Security (Vulnerabilities) And Physical Security
Software Security (Vulnerabilities) And Physical Security
Nicholas Davis
 
CS6701 CRYPTOGRAPHY AND NETWORK SECURITY
CS6701 CRYPTOGRAPHY AND NETWORK SECURITY
Kathirvel Ayyaswamy
 
Unit 2
Unit 2
tamil arasan
 
Public Key Cryptography
Public Key Cryptography
Gopal Sakarkar
 
HSM (Hardware Security Module)
HSM (Hardware Security Module)
Umesh Kolhe
 
DES.ppt
DES.ppt
RizwanBasha12
 
Hybrid encryption ppt
Hybrid encryption ppt
prashantdahake
 
Advanced Cryptography for Cloud Security
Advanced Cryptography for Cloud Security
Neel Chakraborty
 
PGP S/MIME
PGP S/MIME
Sou Jana
 
certified-ethical-hacker-cehv12_course_content.pdf
certified-ethical-hacker-cehv12_course_content.pdf
infosec train
 
2. Stream Ciphers
2. Stream Ciphers
Sam Bowne
 
Diffie-hellman algorithm
Diffie-hellman algorithm
Computer_ at_home
 
Key Management and Distribution
Key Management and Distribution
Syed Bahadur Shah
 
Wireless Network Security
Wireless Network Security
kentquirk
 
Cs8792 cns - unit v
Cs8792 cns - unit v
ArthyR3
 
Rmi presentation
Rmi presentation
Azad public school
 
WLAN Attacks and Protection
WLAN Attacks and Protection
Chandrak Trivedi
 
SHA 1 Algorithm
SHA 1 Algorithm
Shiva RamDam
 
Classic Cryptography
Classic Cryptography
UTD Computer Security Group
 
Caesar Cipher Method Design and Implementation Based on Java, C++, and Python...
Caesar Cipher Method Design and Implementation Based on Java, C++, and Python...
IJCSIS Research Publications
 

More Related Content

What's hot (20)

The-Hacker-Playbook-Practical-Guide-To-Penetration-Testing-2014.pdf
The-Hacker-Playbook-Practical-Guide-To-Penetration-Testing-2014.pdf
prasunkagrawal
 
Key management.ppt
Key management.ppt
Sou Jana
 
Software Security (Vulnerabilities) And Physical Security
Software Security (Vulnerabilities) And Physical Security
Nicholas Davis
 
CS6701 CRYPTOGRAPHY AND NETWORK SECURITY
CS6701 CRYPTOGRAPHY AND NETWORK SECURITY
Kathirvel Ayyaswamy
 
Unit 2
Unit 2
tamil arasan
 
Public Key Cryptography
Public Key Cryptography
Gopal Sakarkar
 
HSM (Hardware Security Module)
HSM (Hardware Security Module)
Umesh Kolhe
 
DES.ppt
DES.ppt
RizwanBasha12
 
Hybrid encryption ppt
Hybrid encryption ppt
prashantdahake
 
Advanced Cryptography for Cloud Security
Advanced Cryptography for Cloud Security
Neel Chakraborty
 
PGP S/MIME
PGP S/MIME
Sou Jana
 
certified-ethical-hacker-cehv12_course_content.pdf
certified-ethical-hacker-cehv12_course_content.pdf
infosec train
 
2. Stream Ciphers
2. Stream Ciphers
Sam Bowne
 
Diffie-hellman algorithm
Diffie-hellman algorithm
Computer_ at_home
 
Key Management and Distribution
Key Management and Distribution
Syed Bahadur Shah
 
Wireless Network Security
Wireless Network Security
kentquirk
 
Cs8792 cns - unit v
Cs8792 cns - unit v
ArthyR3
 
Rmi presentation
Rmi presentation
Azad public school
 
WLAN Attacks and Protection
WLAN Attacks and Protection
Chandrak Trivedi
 
SHA 1 Algorithm
SHA 1 Algorithm
Shiva RamDam
 
The-Hacker-Playbook-Practical-Guide-To-Penetration-Testing-2014.pdf
The-Hacker-Playbook-Practical-Guide-To-Penetration-Testing-2014.pdf
prasunkagrawal
 
Key management.ppt
Key management.ppt
Sou Jana
 
Software Security (Vulnerabilities) And Physical Security
Software Security (Vulnerabilities) And Physical Security
Nicholas Davis
 
CS6701 CRYPTOGRAPHY AND NETWORK SECURITY
CS6701 CRYPTOGRAPHY AND NETWORK SECURITY
Kathirvel Ayyaswamy
 
Public Key Cryptography
Public Key Cryptography
Gopal Sakarkar
 
HSM (Hardware Security Module)
HSM (Hardware Security Module)
Umesh Kolhe
 
Advanced Cryptography for Cloud Security
Advanced Cryptography for Cloud Security
Neel Chakraborty
 
PGP S/MIME
PGP S/MIME
Sou Jana
 
certified-ethical-hacker-cehv12_course_content.pdf
certified-ethical-hacker-cehv12_course_content.pdf
infosec train
 
2. Stream Ciphers
2. Stream Ciphers
Sam Bowne
 
Key Management and Distribution
Key Management and Distribution
Syed Bahadur Shah
 
Wireless Network Security
Wireless Network Security
kentquirk
 
Cs8792 cns - unit v
Cs8792 cns - unit v
ArthyR3
 
WLAN Attacks and Protection
WLAN Attacks and Protection
Chandrak Trivedi
 

Similar to Cryptography using python (20)

Classic Cryptography
Classic Cryptography
UTD Computer Security Group
 
Caesar Cipher Method Design and Implementation Based on Java, C++, and Python...
Caesar Cipher Method Design and Implementation Based on Java, C++, and Python...
IJCSIS Research Publications
 
Cyber Security Part-2.pptx
Cyber Security Part-2.pptx
RavikumarVadana
 
Analysing space complexity of various encryption algorithms 2
Analysing space complexity of various encryption algorithms 2
IAEME Publication
 
Comparative Analysis of Cryptographic Algorithms and Advanced Cryptographic A...
Comparative Analysis of Cryptographic Algorithms and Advanced Cryptographic A...
editor1knowledgecuddle
 
Classical cyphers python programming
Classical cyphers python programming
ShaishavShah8
 
Cryptography 101
Cryptography 101
Aditya Kamat
 
CISSP EXAM PREPARATION FOR A PASSED SCORE
CISSP EXAM PREPARATION FOR A PASSED SCORE
rinelaam
 
ServerDecwweddgccgccfgvxgxcvfxvhfxvr.pptx
ServerDecwweddgccgccfgvxgxcvfxvhfxvr.pptx
t01151009418
 
Implementation of aes and blowfish algorithm
Implementation of aes and blowfish algorithm
eSAT Publishing House
 
Encryption is a process of converting a message, image, or any other .pdf
Encryption is a process of converting a message, image, or any other .pdf
rachanaprade
 
Cryptography in discrete structure .pptx
Cryptography in discrete structure .pptx
ayeshaimtiaz067
 
NS UNIT 1 Advanced Encryption Standard& RSA
NS UNIT 1 Advanced Encryption Standard& RSA
AntonySuresh13
 
CISSP Week 18
CISSP Week 18
jemtallon
 
Cryptography .pptx
Cryptography .pptx
abhinandpk2405
 
Network security CS2
Network security CS2
Infinity Tech Solutions
 
Encryption basics
Encryption basics
Kevin OBrien
 
FormacaoCrypto
FormacaoCrypto
Igor Antunes
 
Cryptography - Overview
Cryptography - Overview
Mohammed Adam
 
crypto slide for students, check out the good article
crypto slide for students, check out the good article
superman85hackerone
 
Caesar Cipher Method Design and Implementation Based on Java, C++, and Python...
Caesar Cipher Method Design and Implementation Based on Java, C++, and Python...
IJCSIS Research Publications
 
Cyber Security Part-2.pptx
Cyber Security Part-2.pptx
RavikumarVadana
 
Analysing space complexity of various encryption algorithms 2
Analysing space complexity of various encryption algorithms 2
IAEME Publication
 
Comparative Analysis of Cryptographic Algorithms and Advanced Cryptographic A...
Comparative Analysis of Cryptographic Algorithms and Advanced Cryptographic A...
editor1knowledgecuddle
 
Classical cyphers python programming
Classical cyphers python programming
ShaishavShah8
 
CISSP EXAM PREPARATION FOR A PASSED SCORE
CISSP EXAM PREPARATION FOR A PASSED SCORE
rinelaam
 
ServerDecwweddgccgccfgvxgxcvfxvhfxvr.pptx
ServerDecwweddgccgccfgvxgxcvfxvhfxvr.pptx
t01151009418
 
Implementation of aes and blowfish algorithm
Implementation of aes and blowfish algorithm
eSAT Publishing House
 
Encryption is a process of converting a message, image, or any other .pdf
Encryption is a process of converting a message, image, or any other .pdf
rachanaprade
 
Cryptography in discrete structure .pptx
Cryptography in discrete structure .pptx
ayeshaimtiaz067
 
NS UNIT 1 Advanced Encryption Standard& RSA
NS UNIT 1 Advanced Encryption Standard& RSA
AntonySuresh13
 
CISSP Week 18
CISSP Week 18
jemtallon
 
Cryptography - Overview
Cryptography - Overview
Mohammed Adam
 
crypto slide for students, check out the good article
crypto slide for students, check out the good article
superman85hackerone
 
Ad

Recently uploaded (20)

F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
June 2025 Progress Update With Board Call_In process.pptx
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
jutaydeonne
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
VCE Literature Section A Exam Response Guide
VCE Literature Section A Exam Response Guide
jpinnuck
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
HistoPathology Ppt. Arshita Gupta for Diploma
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
jutaydeonne
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
VCE Literature Section A Exam Response Guide
VCE Literature Section A Exam Response Guide
jpinnuck
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
HistoPathology Ppt. Arshita Gupta for Diploma
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
Ad

Cryptography using python

  • 1. CRYPTOGRAPHY USING PYTHON BY: KAUSHIK RAMABHOTLA(DR3ADL0CKS)
  • 2. $WHOAMI • I am Kaushik Ramabhotla • I am an undergrad(sophomore) at Amrita University,Amritapuri,Kerala. • Electronics and Communications guy. • I was a part of India’s best CTF – TEAM BIOS.(July ‘18 to April ‘19).
  • 3. What are u gonna gain through this session?? • Python Overview and Installation • Reverse cipher • Caesar cipher • Rot13 algorithm • Base64 encryption-decryption • Simple Substitution Cipher • Python modules of cryptography • Substitution cipher • One Time Pad Cipher (theory) • Symmetric and asymmetric crypto • The RSA algorithm
  • 5. What the heck is cryptography? Cryptography is the science developed soulfully for providing security while exchanging data or a message between two users I WOULD SAY IT AS AN ART OF COMMUNICATION VIA CODED MESSAGES!
  • 7. Why use python? • Python is an open source high level language. It is designed as such that it can be easily understood by most of the readers. It uses English key words frequently • To conclude, Python is neither better or worse than any other programming language as long as you use a good crypto library. However, if you're thinking about implementing something like RSA yourself or some other crypto algorithm you found in a book/paper, then most probably the code will be vulnerable unless you're an expert and you really know what you're doing. This applies to any language.
  • 8. Cryptography Packagescryptography, Have a look at them  Python includes a package called cryptography which provides cryptographic primitives. package is achieved through following command: “pip install cryptography ”  There are various packages with both high level recipes and low level interfaces to common cryptographic algorithms such as symmetric ciphers, message digests and key derivation functions.  Throughout this tutorial, we will be using various packages of Python for implementation of cryptographic algorithms.
  • 9. Reverse cipher As the name suggests , It is easily understood that it is an encryption that is just the reverse of the plain text or for an instance , it will be a water image of our original message message = 'kaushik' translated = '' i = len(message) - 1 while i >= 0: translated = translated + message[i] i = i - 1 print(translated)
  • 11. DISADVANTAGES? ANYONE CAN EASILY BREAK THE CIPHER TEXT TO GET THE ORIGINAL MESSAGE. HENCE, REVERSE CIPHER IS NOT CONSIDERED AS GOOD OPTION TO MAINTAIN SECURE COMMUNICATION CHANNEL. This cipher is a child’s play!
  • 13. CAESAR CIPHER 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. It is the most basic encryption known to the world.
  • 14. IMPLEMENTATION plain = input('enter the string : ') shift = int(input('enter the shift : ')) result = '' for i in range(len(plain)): char = plain[i] if char.isupper(): result += chr((ord(char) + shift - 65) % 26 + 65) else: result += chr((ord(char) + shift - 97) % 26 + 97) print(result)
  • 15. BRUTE FORCE TECHNIQUE(REV- ENGINEERING) message = input('enter the cipher text') letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' for key in range(len(letters)): translated = '' for x in message: if x in letters: num = letters.find(x) num = num - key if num < 0: num = num + len(letters) translated = translated + letters[num] else: translated = translated + x print('key %s: %s' % (key, translated))
  • 16. ROT -13 ALGORITHM • It is simply the abbreviated form of “Rotate by 13 pieces”. • It is a special case of Caesar cipher where the shift is fixed to 13. • Every letter is shifted by 13 places to encrypt or decrypt the message.
  • 18. DRAWBACK • The rot13 cipher uses a shift of 13 and also is a special case of Caesar cipher • Therefore, the algorithm is very easy to break in by brute-force method or, frequency analysis method. Therefore, it does not include any practical use.
  • 19. BASE64 ENCRYPTION-DECRYPTION • BASE64 encryption converts binary data to text format • When you encode text in ASCII, you start with a text string and convert it to a sequence of bytes. • When you encode data in Base64, you start with a sequence of bytes and convert it to a text string.
  • 20. CODE PART • There is a module for base64 in python(in-built) • For decoding… import base64 x = base64.b64encode(bytes(input(''), 'utf-8')) print(x) import base64 x = base64.b64decode(bytes(input(''), 'utf-8')) print(x)
  • 21. DRAWBACKS… • BASE64 algorithm is basically used to store passwords in databases • Any online decoder tool can be used to decipher the information you want • Or you can just decipher them by yourself!
  • 22. ONE TIME PAD CIPHER(O.T.P) • It is similar to vienere cipher. • It is an unbreakable cipher. • As the name suggests, key is used one time only and never used again for any other message to be encrypted • The key is as long as the given message • The key is auto generated
  • 23. ENCRYPTION/DECRYPTION WITH ONE TIME PAD • To encrypt a letter, a user needs to write a key underneath the plaintext. The plaintext letter is placed on the top and the key letter on the left. The cross section achieved between two letters in the plain text.
  • 24. DECRYPTION • To decrypt a letter, user takes the key letter on the left and finds cipher text letter in that row. The plain text letter is placed at the top of the column where the user can find the cipher text letter. • Python has a handy module named-’onetimepad’ which has a command line encryption tool that uses encryption mechanism similar to the algorithm.
  • 25. FACTS: • So, if you want to encrypt the message "If you want to survive out here, you've got to know where your towel is.”, its length is 55 excluding the spaces and punctuation. To encrypt with this O.T.P, we need a key of length =55(same as plaintext) • Let’s use the key “kcqyzhepxautiqekxejmoretzhztrwwqdylbttvejmedbsanybpxqik”. • Now imagine if a cryptanalyst got hold of the ciphertext, brute force would not work, because the no. of possibilities are (26^55)=i.e. 666,091,878,431,395,624,153,823,182,526,730,590, 376,250,379,52 8,249,805,353,030,484,209,594,192,101,376 possible keys.
  • 27. VIGENERE CIPHER : • This cipher is almost as of Caesar cipher but with a little twist. • Caesar cipher is one-character shift, whereas vigenere cipher involves multiple alphabets shift • For encryption the mathematical equation is as follows: • For decryption the mathematical equation is as follows:
  • 28. FACTS : • In this cipher, the key length is directly proportional to security of the plaintext
  • 29. CODE PART plaintext = input() key = input('enter') key_length = len(key) key_as_int = [ord(i) for i in key] plaintext_int = [ord(i) for i in plaintext] ciphertext = '' for i in range(len(plaintext_int)): value = (plaintext_int[i] + key_as_int[i % key_length]) % 26 ciphertext += chr(value + 65) print(ciphertext) ciphertext = input('enter') key = input() key_length = len(key) key_as_int = [ord(i) for i in key] ciphertext_int = [ord(i) for i in ciphertext] plaintext = '' for i in range(len(ciphertext_int)): value = (ciphertext_int[i] - key_as_int[i % key_length]) % 26 plaintext += chr(value + 65) print(plaintext) Encryption part Decryption part
  • 30. MERITS AND DE-MERITS • The strength of vignere cipher is that it is not susceptible to frequency analysis due to the fact that the shifts are more. A codebreaker using frequency analysis method/graph cannot help us in decrypting the original message. Though it was strong cipher after its invention in 16th century. In 1863, ”Fredrich kakiski” was the first to publish a successful general attack on the Vigenère cipher • The primary weakness of the Vigenère cipher is the repeating nature of its key. If a cryptanalyst correctly guesses the length of the key, then the ciphertext can be treated as interwoven Caesar ciphers, which, individually, can be easily broken
  • 31. CRYPTOGRAPHY WITH PYTHON – SYMMETRIC AND ASYMMETRIC CRYPTOGRAPHY • First we will look into Symmetric part • Symmetric encryption is a type of encryption where the key is same for encryption and decryption
  • 32. TYPES: • There are two types of symmetric encryption, they are: 1. Block ciphers 2. Stream ciphers • Block ciphers : In a block cipher, the plaintext is broken into blocks of a set length and the bits in each block are encrypted together. • A few of the most popular block ciphers are DES/3DES, AES, Blowfish, and Twofish.
  • 33. DATA ENCRYPTION CIPHER(DES) • DES is a block cipher with a block size of 64bit and a 56bit key since 8 of the 64 bits of the key are not used by the encryption algorithm (function as check bits only)
  • 34. BLOCK CIPHER MODES OF OPERATION • One of the main issue with block ciphers is that it allows only to encrypt message the same size as their block length. If you use an algorithm in which a block has a size of 64bit to encrypt a message of 65bit, you need a way to define how the second block should be encrypted. To issue this problem, different modes of operations were discovered. We shall discuss the most used operations they are: 1. ECB 2. ECB modes respectively
  • 35. ECB MODE(ELECTRONIC CODEBOOK MODE) ECB is the simplest mode of operation. In this mode, each part of plaintext is encrypted separately. The main disadvantage to this mode is that if there are identical plaintext parts, when encrypted with a given key, produce same ciphertexts, which allows an attacker to learn some info about the plaintext.
  • 36. CIPHER BLOCK CHAINING (CBC) MODE • In the cipher block chaining, an initialisation vector(I.V) is exclusively added to the encryption process with the key. For the first round of encryption, it is a random public value. For the next subsequent rounds, the I.V becomes the ciphertext of the previous round’s encryption
  • 37. IMPLEMENTATION: import pyDes data = "DES Algorithm Implementation" k = pyDes.des("DESCRYPT", pyDes.CBC, "00000000", pad=None, padmode=pyDes.PAD_PKCS5) d = k.encrypt(data) print("Encrypted: %r" % d) print("Decrypted: %r" % k.decrypt(d))
  • 38. THINGS THAT ARE NEW IN THE PREVIOUS CODE ARE:: • 1. initialisation vector: • 2.padding:
  • 39. ASYMMETRIC CRYPTOGRAPHY • As discussed earlier, it is quite opposite to symmetric cryptography and it requires two keys(one for encryption and one for decryption) • The public key is used for encryption • The private key is used for decryption
  • 40. HOW TO GET THE KEYS? The cryptographic strength is primarily linked to the length of the RSA modulus n. In 2017, a sufficient length is deemed to be 2048 bits.
  • 42. UNDERSTANDING RSA ALGORITHM • RSA is considered as the most secure way of encryption • Invented by Rivest, Shamir, Adleman in 1978. and hence named RSA. • It includes working with prime numbers • The integers used by this method are sufficiently large to make it difficult to solve. • Here also, there are two sets of keys:private and public. How it works?
  • 43. STEPS IN RSA ALGORITHM Step1:generating the RSA modulus Step 2: generating derived number(e),public key, private key Step 3: finally encrypting or decrypting
  • 44. LET’S DEFINE VARIABLES THAT WILL BE USED: • P,q two very large prime numbers. • N = p*q • E = public key exponent • D = private key exponent • M = unpadded message • C = cipher text
  • 45. KEY GENERATION: • Calculation of phi(Euler's totient function) = (p-1)*(q-1) • Choose an integer e such that (1<e<phi) and GCD(e,phi)=1 • Normally, e=22i+1(i>0). Small values of e can make RSA vulnerable to some attacks under certain conditions • Compute d as d = (1/e) * (mod(phi)) • The pair(e,n) is known as public key • The pair (d,n) is known as private key
  • 46. ENCRYPTION AND DECRYPTION • Computes ciphertext c = me mod n • Transmits the ciphertext c using a reliable channel • While decrypting one can compute m = cd mod n (Since c = me mod n, cd mod n = me*d mod n = m).
  • 47. PADDING AND PADDING SCHEMES • RSA without padding is referred as “Textbook RSA”. There are a number of attacks possible if the message is not padded before RSA- encryption. • The padding bytes follow a specific standard, for example, PKCS,OAEP,etc. padding ensures that the plaintext does not become vulnerable to these attacks. Direct square root Hastad’s broadcast attack Chosen ciphertext attacks