Cryptography With Python Tutorial
Cryptography With Python Tutorial
After completing this tutorial, you will be able to relate the basic techniques of
cryptography in real world scenarios.
Audience
This tutorial is meant for the end users who aspire to learn the basics of cryptography and
its implementation in real world projects. This tutorial is also useful for networking
professionals as well as hackers who want to implement new frameworks instead of
following a traditional approach.
Prerequisites
Throughout this tutorial, you will learn the basics of cryptography, algorithm description
and its implementation in Python. This tutorial is designed with an assumption that the
user has an understanding on the basics of cryptography and algorithms. If you are a
beginner to these topics, we suggest you to go through tutorials related to them, before
you start with this tutorial.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
i
Cryptography with Python
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
Drawback ................................................................................................................................................ 8
Example .................................................................................................................................................. 9
Example ................................................................................................................................................ 14
ii
Cryptography with Python
Example ................................................................................................................................................ 16
Code ...................................................................................................................................................... 16
Pyperclip ............................................................................................................................................... 18
Code ...................................................................................................................................................... 20
Code ...................................................................................................................................................... 22
Code ...................................................................................................................................................... 25
Algorithm .............................................................................................................................................. 30
Code ...................................................................................................................................................... 30
Code ...................................................................................................................................................... 34
iii
Cryptography with Python
Example ................................................................................................................................................ 39
Code ...................................................................................................................................................... 45
Code ...................................................................................................................................................... 52
Encryption ............................................................................................................................................. 54
Decryption ............................................................................................................................................ 54
24. CRYPTOGRAPHY WITH PYTHON – IMPLEMENTATION OF ONE TIME PAD CIPHER ............. 55
Installation ............................................................................................................................................ 55
Algorithm .............................................................................................................................................. 61
iv
Cryptography with Python
Authorization ........................................................................................................................................ 71
Authentication ...................................................................................................................................... 72
v
1. Cryptography with Python – Overview
Cryptography is the art of communication between two users via coded messages. The
science of cryptography emerged with the basic motive of providing security to the
confidential messages transferred from one party to another.
Cryptography is defined as the art and science of concealing the message to introduce
privacy and secrecy as recognized in information security.
Terminologies of Cryptography
The frequently used terms in cryptography are explained here:
Plain Text
The plain text message is the text which is readable and can be understood by all users.
The plain text is the message which undergoes cryptography.
Cipher Text
Cipher text is the message obtained after applying cryptography on plain text.
Encryption
The process of converting plain text to cipher text is called encryption. It is also called as
encoding.
Decryption
The process of converting cipher text to plain text is called decryption. It is also termed
as decoding.
The diagram given below shows an illustration of the complete process of cryptography:
1
Cryptography with Python
2
2. Cryptography with Python – Double Strength Encryption
Cryptography with Python
Double strength encryption, also called as multiple encryption, is the process of encrypting
an already encrypted text one or more times, either with the same or different
algorithm/pattern.
The other names for double strength encryption include cascade encryption or cascade
ciphering.
3
Cryptography with Python
Hybrid Cryptography
Hybrid cryptography is the process of using multiple ciphers of different types together by
including benefits of each of the cipher. There is one common approach which is usually
followed to generate a random secret key for a symmetric cipher and then encrypt this
key via asymmetric key cryptography.
Due to this pattern, the original message itself is encrypted using the symmetric cipher
and then using secret key. The receiver after receiving the message decrypts the message
using secret key first, using his/her own private key and then uses the specified key to
decrypt the message.
4
3. Cryptography with Python – PythonCryptography
Overview and
with Python
Installation
Interpreted
Python is processed at runtime using the interpreter. There is no need to compile a
program before execution. It is similar to PERL and PHP.
Object-Oriented
Python follows object-oriented style and design patterns. It includes class definition with
various features like encapsulation and polymorphism.
It includes high-level dynamic data types and supports various dynamic type
checking.
Python includes a feature of integration with C, C++ and languages like Java.
5
Cryptography with Python
Python Strings
The basic declaration of strings is shown below:
Python Lists
The lists of python can be declared as compound data types, separated by commas and
enclosed within square brackets ([]).
Python Tuples
A tuple is dynamic data type of Python which consists of number of values separated by
commas. Tuples are enclosed with parentheses.
Python Dictionary
Python dictionary is a type of hash table. A dictionary key can be almost any data type of
Python, which are usually numbers or strings.
Cryptography Packages
Python includes a package called cryptography which provides cryptographic recipes and
primitives. It supports Python 2.7, Python 3.4+, and PyPy 5.3+. The basic installation of
cryptography package is achieved through following command:
6
Cryptography with Python
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.
7
4. Cryptography with Python – Reverse Cipher
Cryptography with Python
The previous chapter gave you an overview of installation of Python on your local
computer. In this chapter you will learn in detail about reverse cipher and its coding.
Reverse Cipher uses a pattern of reversing the string of plain text to convert as
cipher text.
To decrypt cipher text, the user simply needs to reverse the cipher text to get the
plain text.
Drawback
The major drawback of reverse cipher is that it is very weak. A hacker 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,.
8
Cryptography with Python
Example
Consider an example where the statement This is program to explain reverse cipher
is to be implemented with reverse cipher algorithm. The following python code uses the
algorithm to obtain the output.
Output
You can see the reversed text, that is the output as shown in the following image:
Explanation
Plain text is stored in the variable message and the translated variable is used to store
the cipher text created.
The length of plain text is calculated using for loop and with help of index number.
The characters are stored in cipher text variable translated which is printed in the
last line.
9
5. Cryptography with Python – Caesar Cipher
Cryptography with Python
In the last chapter, we have dealt with reverse cipher. This chapter talks about Caesar
cipher in detail.
Caesar Cipher Technique is the simple and easy method of encryption technique.
Each letter of plain text is replaced by a letter with some fixed number of positions
down with alphabet.
The following diagram depicts the working of Caesar cipher algorithm implementation:
10
Cryptography with Python
def encrypt(text,s):
result = ""
return result
Output
You can see the Caesar cipher, that is the output as shown in the following image:
11
Cryptography with Python
Explanation
The plain text character is traversed one at a time.
For each character in the given plain text, transform the given character as per the
rule depending on the procedure of encryption and decryption of text.
After the steps is followed, a new string is generated which is referred as cipher
text.
12
Cryptography with Python
Consider the cipher text encrypted in the previous example. Then, the output with possible
hacking methods with the key and using brute force attack technique is as follows:
13
6. Cryptography with Python – ROT13 Algorithm
Cryptography with Python
Till now, you have learnt about reverse cipher and Caesar cipher algorithms. Now, let us
discuss the ROT13 algorithm and its implementation.
Example
The following diagram explains the ROT13 algorithm process pictorially:
Program Code
The program implementation of ROT13 algorithm is as follows:
rot13trans = maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm')
14
Cryptography with Python
return text.translate(rot13trans)
def main():
txt = "ROT13 Algorithm"
print rot13(txt)
if __name__ == "__main__":
main()
You can see the ROT13 output as shown in the following image:
Drawback
The ROT13 algorithm uses 13 shifts. Therefore, it is very easy to shift the characters in
the reverse manner to decrypt the cipher text.
15
7. Cryptography with Python – Transposition Cipher
Cryptography with Python
Example
A simple example for a transposition cipher is columnar transposition cipher where
each character in the plain text is written horizontally with specified alphabet width. The
cipher is written vertically, which creates an entirely different cipher text.
Consider the plain text hello world, and let us apply the simple columnar transposition
technique as shown below:
h e l l
o w o r
l d
The plain text characters are placed horizontally and the cipher text is created with vertical
format as : holewdlo lr. Now, the receiver has to use the same table to decrypt the cipher
text to plain text.
Code
The following program code demonstrates the basic implementation of columnar
transposition technique:
order = {
int(val): num for num, val in enumerate(key)
}
ciphertext = ''
for index in sorted(order.keys()):
for part in split_len(plaintext, len(key)):
try:
16
Cryptography with Python
ciphertext += part[order[index]]
except IndexError:
continue
return ciphertext
print(encode('3214', 'HELLO'))
Explanation
Using the function split_len(), we can split the plain text characters, which can be
placed in columnar or row format.
encode method helps to create cipher text with key specifying the number of
columns and prints the cipher text by reading characters through each column.
Output
The program code for the basic implementation of columnar transposition technique gives
the following output:
Note:
17
8. Cryptography with Python – Encryption of Transposition
Cryptography with Python
Cipher
In the previous chapter, we have learnt about Transposition Cipher. In this chapter, let us
discuss its encryption.
Pyperclip
The main usage of pyperclip plugin in Python programming language is to perform cross
platform module for copying and pasting text to the clipboard. You can install python
pyperclip module using the command as shown:
If the requirement already exists in the system, you can see the following output:
Code
The python code for encrypting transposition cipher in which pyperclip is the main module
is as shown below:
import pyperclip
def main():
myMessage = 'Transposition Cipher'
myKey = 10
ciphertext = encryptMessage(myKey, myMessage)
print("Cipher Text is")
print(ciphertext + '|')
pyperclip.copy(ciphertext)
def encryptMessage(key, message):
ciphertext = [''] * key
for col in range(key):
position = col
while position < len(message):
ciphertext[col] += message[position]
18
Cryptography with Python
position += key
return ''.join(ciphertext) #Cipher text
if __name__ == '__main__':
main()
Output
The program code for encrypting transposition cipher in which pyperclip is the main
module gives the following output:
Explanation
The function main() calls the encryptMessage() which includes the procedure
for splitting the characters using len function and iterating them in a columnar
format.
The main function is initialized at the end to get the appropriate output.
19
9. Cryptography with Python – Decryption of Transposition
Cryptography with Python
Cipher
In this chapter, you will learn the procedure for decrypting the transposition cipher.
Code
Observe the following code for a better understanding of decrypting a transposition cipher.
The cipher text for message Transposition Cipher with key as 6 is fetched as Toners
raiCntisippoh.
def main():
myMessage= 'Toners raiCntisippoh'
myKey = 6
plaintext = decryptMessage(myKey, myMessage)
print("The plain text is")
print('Transposition Cipher')
20
Cryptography with Python
Explanation
The cipher text and the mentioned key are the two values taken as input parameters for
decoding or decrypting the cipher text in reverse technique by placing characters in a
column format and reading them in a horizontal manner.
You can place letters in a column format and later combined or concatenate them together
using the following piece of code:
Output
The program code for decrypting transposition cipher gives the following output:
21
10. Cryptography with Python – Encryption of files
Cryptography with Python
Code
The program code for encrypting the file with password protector is mentioned below:
# =================Other Configuration================
# Usages :
usage = "usage: %prog [options] "
# Version
Version="%prog 0.0.1"
# ====================================================
# Import Modules
import optparse, sys,os
from toolkit import processor as ps
def main():
parser=optparse.OptionParser(usage=usage,version=Version)
parser.add_option('-i','--input',type='string',dest='inputfile',help="File
Input Path For Encryption", default=None)
parser.add_option('-o','--
output',type="string",dest='outputfile',help="File Output Path For Saving
Encrypter Cipher",default=".")
22
Cryptography with Python
parser.add_option('-p','--
password',type="string",dest='password',help="Provide Password For Encrypting
File",default=None)
(options, args)= parser.parse_args()
outputfile=os.path.join(options.outputfile,os.path.basename(options.inputfile).
split('.')[0]+'.ssb')
password=options.password
base=os.path.basename(inputfile).split('.')[1]
work="E"
ps.FileCipher(inputfile,outputfile,password,work)
return
if __name__ == '__main__':
main()
You can use the following command to execute the encryption process along with
password:
23
Cryptography with Python
Output
You can observe the following output when you execute the code given above:
Explanation
The passwords are generated using MD5 hash algorithm and the values are stored in
simply safe backup files in Windows system, which includes the values as displayed below:
24
11. Cryptography with Python – Decryption of files
Cryptography with Python
In this chapter, let us discuss decryption of files in cryptography using Python. Note that
for decryption process, we will follow the same procedure, but instead of specifying the
output path, we will focus on input path or the necessary file which is encrypted.
Code
The following is a sample code for decrypting files in cryptography using Python:
#!/usr/bin/python
# =================Other Configuration================
# Usages :
usage = "usage: %prog [options] "
# Version
Version="%prog 0.0.1"
# ====================================================
# Import Modules
import optparse, sys,os
from toolkit import processor as ps
def main():
25
Cryptography with Python
parser=optparse.OptionParser(usage=usage,version=Version)
parser.add_option('-i','--input',type='string',dest='inputfile',help="File
Input Path For Encryption", default=None)
parser.add_option('-o','--
output',type="string",dest='outputfile',help="File Output Path For Saving
Encrypter Cipher",default=".")
parser.add_option('-p','--
password',type="string",dest='password',help="Provide Password For Encrypting
File",default=None)
(options, args)= parser.parse_args()
You can use the following command for executing the above code:
26
Cryptography with Python
Output
You can observe the following code when you execute the command shown above:
Note:
The output specifies the hash values before encryption and after decryption, which keeps a
note that the same file is encrypted and the process was successful.
27
12. Cryptography with Python – Base64Cryptography
Encoding and
with Python
Decoding
Base64 encoding converts the binary data into text format, which is passed through
communication channel where a user can handle text safely. Base64 is also called as
Privacy enhanced Electronic mail (PEM) and is primarily used in email encryption
process.
Python includes a module called BASE64 which includes two primary functions as given
below:
import base64
encoded_data = base64.b64encode("Encode this text")
print("Encoded text with base 64 is")
print(encoded_data)
Output
The code for base64 encoding gives you the following output:
28
Cryptography with Python
import base64
decoded_data = base64.b64decode("RW5jb2RlIHRoaXMgdGV4dA==")
print("decoded text is ")
print(decoded_data)
Output
The code for base64 decoding gives you the following output:
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.
Drawback
Base64 algorithm is usually used to store passwords in database. The major drawback is
that each decoded word can be encoded easily through any online tool and intruders can
easily get the information.
29
13. Cryptography with Python – XOR Process
Cryptography with Python
In this chapter, let us understand the XOR process along with its coding in Python.
Algorithm
XOR algorithm of encryption and decryption converts the plain text in the format ASCII bytes
and uses XOR procedure to convert it to a specified byte. It offers the following advantages
to its users:
Fast computation
No difference marked in left and right side
Easy to understand and analyze
Code
You can use the following piece of code to perform XOR process:
30
Cryptography with Python
Output
The code for XOR process gives you the following output:
Explanation
The function xor_crypt_string() includes a parameter to specify mode of encode and
decode and also the string value.
The basic functions are taken with base64 modules which follows the XOR procedure/
operation to encrypt or decrypt the plain text/ cipher text.
Note:
XOR encryption is used to encrypt data and is hard to crack by brute-force method, that is
by generating random encrypting keys to match with the correct cipher text.
31
14. Cryptography with Python – Multiplicative Cipher
Cryptography with Python
While using Caesar cipher technique, encrypting and decrypting symbols involves
converting the values into numbers with a simple basic procedure of addition or
subtraction.
The numbers will be used for multiplication procedure and the associated key is 7. The
basic formula to be used in such a scenario to generate a multiplicative cipher is as follows:
32
Cryptography with Python
The number fetched through output is mapped in the table mentioned above and the
corresponding letter is taken as the encrypted letter.
Note:
The advantage with a multiplicative cipher is that it can work with very large keys like
8,953,851. It would take quite a long time for a computer to brute-force through a majority
of nine million keys.
33
15. Cryptography with Python – Affine Cipher
Cryptography with Python
Affine Cipher is the combination of Multiplicative Cipher and Caesar Cipher algorithm. The
basic implementation of affine cipher is as shown in the image below:
In this chapter, we will implement affine cipher by creating its corresponding class that
includes two basic functions for encryption and decryption.
Code
You can use the following code to implement an affine cipher:
class Affine(object):
DIE = 128
KEY = (7, 3, 55)
def __init__(self):
pass
def encryptChar(self, char):
K1, K2, kI = self.KEY
return chr((K1 * ord(char) + K2) % self.DIE)
affine = Affine()
print affine.encrypt('Affine Cipher')
print affine.decrypt('*18?FMT')
Output
You can observe the following output when you implement an affine cipher:
The output displays the encrypted message for the plain text message Affine Cipher and
decrypted message for the message sent as input abcdefg.
35
16. Cryptography with Python – HackingCryptography
Monoalphabetic
with Python
Cipher
In this chapter, you will learn about monoalphabetic cipher and its hacking using Python.
Monoalphabetic Cipher
A Monoalphabetic cipher uses a fixed substitution for encrypting the entire message. A
monoalphabetic cipher using a Python dictionary with JSON objects is shown here:
monoalpha_cipher = {
'a': 'm',
'b': 'n',
'c': 'b',
'd': 'v',
'e': 'c',
'f': 'x',
'g': 'z',
'h': 'a',
'i': 's',
'j': 'd',
'k': 'f',
'l': 'g',
'm': 'h',
'n': 'j',
'o': 'k',
'p': 'l',
'q': 'p',
'r': 'o',
's': 'i',
't': 'u',
'u': 'y',
'v': 't',
'w': 'r',
'x': 'e',
'y': 'w',
'z': 'q',
36
Cryptography with Python
With help of this dictionary, we can encrypt the letters with the associated letters as values
in JSON object. The following program creates a monoalphabetic program as a class
representation which includes all the functions of encryption and decryption.
def random_monoalpha_cipher(pool=None):
if pool is None:
pool = letters + digits
original_pool = list(pool)
shuffled_pool = list(pool)
shuffle(shuffled_pool)
return dict(zip(original_pool, shuffled_pool))
def inverse_monoalpha_cipher(monoalpha_cipher):
inverse_monoalpha = {}
for key, value in monoalpha_cipher.iteritems():
inverse_monoalpha[value] = key
return inverse_monoalpha
37
Cryptography with Python
This file is called later to implement the encryption and decryption process of
Monoalphabetic cipher which is mentioned as below:
import monoalphabeticCipher as mc
cipher = mc.random_monoalpha_cipher()
print(cipher)
encrypted = mc.encrypt_with_monoalpha('Hello all you hackers out there!',
cipher)
decrypted = mc.decrypt_with_monoalpha('sXGGt SGG Nt0 HSrLXFC t0U UHXFX!',
cipher)
print(encrypted)
print(decrypted)
Output
You can observe the following output when you implement the code given above:
Thus, you can hack a monoalphabetic cipher with specified key value pair which cracks
the cipher text to actual plain text.
38
17. Cryptography with Python – Simple Substitution Cipher
Cryptography with Python
Simple substitution cipher is the most commonly used cipher and includes an algorithm of
substituting every plain text character for every cipher text character. In this process,
alphabets are jumbled in comparison with Caesar cipher algorithm.
Example
Keys for a simple substitution cipher usually consists of 26 letters. An example key is:
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def main():
message = ''
if len(sys.argv) > 1:
with open(sys.argv[1], 'r') as f:
message = f.read()
else:
message = raw_input("Enter your message: ")
mode = raw_input("E for Encrypt, D for Decrypt: ")
key = ''
while checkKey(key) is False:
key = raw_input("Enter 26 ALPHA key (leave blank for random key): ")
if key == '':
key = getRandomKey()
if checkKey(key) is False:
39
Cryptography with Python
if len(sys.argv) > 1:
fileOut = 'enc.' + sys.argv[1]
with open(fileOut, 'w') as f:
f.write(translated)
print('Success! File written to: %s' % (fileOut))
else: print('Result: ' + translated)
# Store the key into list, sort it, convert back, compare to alphabet.
def checkKey(key):
keyString = ''.join(sorted(list(key)))
return keyString == LETTERS
translated = ''
charsA = LETTERS
charsB = key
else:
translated += symbol
return translated
def getRandomKey():
randomList = list(LETTERS)
random.shuffle(randomList)
return ''.join(randomList)
if __name__ == '__main__':
main()
Output
You can observe the following output when you implement the code given above:
41
18. Cryptography with Python – Testing of Simple
Cryptography with Python
Substitution Cipher
In this chapter, we will focus on testing substitution cipher using various methods, which
helps to generate random strings as given below:
if __name__ == '__main__':
main()
42
Cryptography with Python
Output
You can observe the output as randomly generated strings which helps in generating
random plain text messages, as shown below:
43
Cryptography with Python
After the test is successfully completed, we can observe the output message Substitution
test passed!.
44
19. Cryptography with Python – Decryption of Simple
Cryptography with Python
Substitution Cipher
In this chapter, you can learn about simple implementation of substitution cipher which
displays the encrypted and decrypted message as per the logic used in simple substitution
cipher technique. This can be considered as an alternative approach of coding.
Code
You can use the following code to perform decryption using simple substitution cipher:
import random
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + \
'abcdefghijklmnopqrstuvwxyz' + \
'0123456789' + \
':.;,?!@#$%&()+=-*/_<> []{}`~^"\'\\'
def generate_key():
"""Generate an key for our cipher"""
shuffled = sorted(chars, key=lambda k: random.random())
return dict(zip(chars, shuffled))
def show_result(plaintext):
"""Generate a resulting cipher with elements shown"""
key = generate_key()
encrypted = encrypt(key, plaintext)
decrypted = decrypt(key, encrypted)
print 'Key: %s' % key
45
Cryptography with Python
Output
The above code gives you the output as shown here:
46
20. Cryptography with Python – Python Modules of
Cryptography with Python
Cryptography
In this chapter, you will learn in detail about various modules of cryptography in Python.
Cryptography Module
It includes all the recipes and primitives, and provides a high level interface of coding in
Python. You can install cryptography module using the following command:
Code
You can use the following code to implement the cryptography module:
47
Cryptography with Python
Output
The code given above produces the following output:
The code given here is used to verify the password and creating its hash. It also includes
logic for verifying the password for authentication purpose.
import uuid
import hashlib
def hash_password(password):
# uuid is used to generate a random number of the specified password
salt = uuid.uuid4().hex
return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':'
+ salt
48
Cryptography with Python
Output
Scenario 1: If you have entered a correct password, you can find the following output:
Scenario 2: If we enter wrong password, you can find the following output:
Explanation
Hashlib package is used for storing passwords in a database. In this program, salt is
used which adds a random sequence to the password string before implementing the
hash function.
49
21. Cryptography with Python – Understanding Vignere
Cryptography with Python
Cipher
Vignere Cipher includes a twist with Caesar Cipher algorithm used for encryption and
decryption. Vignere Cipher works similar to Caesar Cipher algorithm with only one major
distinction: Caesar Cipher includes algorithm for one-character shift, whereas Vignere
Cipher includes key with multiple alphabets shift.
Mathematical Equation
For encryption the mathematical equation is as follows:
Vignere cipher uses more than one set of substitutions, and hence it is also referred as
polyalphabetic cipher. Vignere Cipher will use a letter key instead of a numeric key
representation: Letter A will be used for key 0, letter B for key 1 and so on. Numbers of
the letters before and after encryption process is shown below:
50
Cryptography with Python
The possible combination of number of possible keys based on Vignere key length is given
as follows, which gives the result of how secure is Vignere Cipher Algorithm:
1 26 = 26
2 26 × 26 = 676
3 676 × 26 = 17,576
4 17,576 × 26 = 456,976
5 456,976 × 26 = 11,881,376
6 11,881,376 × 26 = 308,915,776
7 308,915,776 × 26 = 8,031,810,176
8 8,031,810,176 × 26 = 208,827,064,576
9 208,827,064,576 × 26 = 5,429,503,678,976
10 5,429,503,678,976 × 26 = 141,167,095,653,376
11 141,167,095,653,376 × 26 = 3,670,344,486,987,776
12 3,670,344,486,987,776 × 26 = 95,428,956,661,682,176
13 95,428,956,661,682,176 × 26 = 2,481,152,873,203,736,576
14 2,481,152,873,203,736,576 × 26 = 64,509,974,703,297,150,976
Vignere Tableau
The tableau used for Vignere cipher is as shown below:
51
22. Cryptography with Python – Implementing Vignere
Cryptography with Python
Cipher
In this chapter, let us understand how to implement Vignere cipher. Consider the text This is
basic implementation of Vignere Cipher is to be encoded and the key used is PIZZA.
Code
You can use the following code to implement a Vignere cipher in Python:
import pyperclip
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def main():
myMessage = "This is basic implementation of Vignere Cipher"
myKey = 'PIZZA'
myMode = 'encrypt'
if myMode == 'encrypt':
translated = encryptMessage(myKey, myMessage)
elif myMode == 'decrypt':
translated = decryptMessage(myKey, myMessage)
print('%sed message:' % (myMode.title()))
print(translated)
print()
52
Cryptography with Python
if __name__ == '__main__':
main()
Output
You can observe the following output when you implement the code given above:
The possible combinations of hacking the Vignere cipher is next to impossible. Hence, it is
considered as a secure encryption mode.
53
23. Cryptography with Python – One Time Pad Cipher
Cryptography with Python
One-time pad cipher is a type of Vignere cipher which includes the following features:
It is an unbreakable cipher.
The key is exactly same as the length of message which is encrypted.
The key is made up of random symbols.
As the name suggests, key is used one time only and never used again for any
other message to be encrypted.
Due to this, encrypted message will be vulnerable to attack for a cryptanalyst. The key
used for a one-time pad cipher is called pad, as it is printed on pads of paper.
Why is it Unbreakable?
The key is unbreakable owing to the following features:
Encryption
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 is the plain text. It is described in the example below:
Plain text: T H I S I S S E C R E T
OTP-Key : X V H E U W N O P G D Z
---------------------------------------
Ciphertext: Q C P W C O F S R X H S
In groups : QCPWC OFSRX HS
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.
54
24. Cryptography with Python – Implementation of One Time
Cryptography with Python
Pad Cipher
Python includes a hacky implementation module for one-time pad cipher implementation.
The package name is called One-Time-Pad which includes a command line encryption
tool that uses encryption mechanism similar to the one-time pad cipher algorithm.
Installation
You can use the following command to install this module:
If you wish to use it from the command-line, run the following command:
onetimepad
Code
The following code helps to generate a one-time pad cipher:
import onetimepad
cipher = onetimepad.encrypt('One Time Cipher', 'random')
print("Cipher text is ")
print(cipher)
print("Plain text is ")
msg = onetimepad.decrypt(cipher, 'random')
print(msg)
55
Cryptography with Python
Output
You can observe the following output when you run the code given above:
Note:
The encrypted message is very easy to crack if the length of the key is less than
the length of message (plain text).
In any case, the key is not necessarily random, which makes one-time pad cipher as a
worth tool.
56
25. Cryptography with Python – Symmetric and Asymmetric
Cryptography with Python
Cryptography
In this chapter, let us discuss in detail about symmetric and asymmetric cryptography.
Symmetric Cryptography
In this type, the encryption and decryption process uses the same key. It is also called as
secret key cryptography. The main features of symmetric cryptography are as follows:
Drawback
The major drawback of symmetric cryptography is that if the key is leaked to the intruder,
the message can be easily changed and this is considered as a risk factor.
Installation
The command for installation of DES package pyDES in Python is:
57
Cryptography with Python
import pyDes
data = "DES Algorithm Implementation"
k = pyDes.des("DESCRYPT", pyDes.CBC, "\0\0\0\0\0\0\0\0", pad=None,
padmode=pyDes.PAD_PKCS5)
d = k.encrypt(data)
print "Encrypted: %r" % d
print "Decrypted: %r" % k.decrypt(d)
assert k.decrypt(d) == data
It calls for the variable padmode which fetches all the packages as per DES algorithm
implementation and follows encryption and decryption in a specified manner.
Output
You can see the following output as a result of the code given above:
Asymmetric Cryptography
It is also called as public key cryptography. It works in the reverse way of symmetric
cryptography. This implies that it requires two keys: one for encryption and other for
decryption. The public key is used for encrypting and the private key is used for decrypting.
Drawback
Due to its key length, it contributes lower encryption speed.
Key management is crucial.
58
Cryptography with Python
The following program code in Python illustrates the working of asymmetric cryptography
using RSA algorithm and its implementation:
def generate_keys():
# key length must be a multiple of 256 and >= 1024
modulus_length = 256*4
privatekey = RSA.generate(modulus_length, Random.new().read)
publickey = privatekey.publickey()
return privatekey, publickey
59
Cryptography with Python
Output
You can find the following output when you execute the code given above:
60
26. Cryptography with Python – Understanding RSA
Cryptography with Python
Algorithm
RSA algorithm is a public key encryption technique and is considered as the most secure way
of encryption. It was invented by Rivest, Shamir and Adleman in year 1978 and hence name
RSA algorithm.
Algorithm
The RSA algorithm holds the following features:
The integers used by this method are sufficiently large making it difficult to solve.
There are two sets of keys in this algorithm: private key and public key.
You will have to go through the following steps to work on RSA algorithm:
N=p*q
The above formula is the basic formula for Extended Euclidean Algorithm, which takes p
and q as the input parameters.
61
Cryptography with Python
Encryption Formula
Consider a sender who sends the plain text message to someone whose public key is
(n,e). To encrypt the plain text message in the given scenario, use the following syntax:
C = Pe mod n
Decryption Formula
The decryption process is very straightforward and includes analytics for calculation in a
systematic approach. Considering receiver C has the private key d, the result modulus will
be calculated as:
Plaintext = Cd mod n
62
27. Cryptography with Python – Creating RSA Keys
Cryptography with Python
In this chapter, we will focus on step wise implementation of RSA algorithm using Python.
Create two large prime numbers namely p and q. The product of these numbers
will be called n, where n= p*q
Generate a random number which is relatively prime with (p-1) and (q-1). Let the
number be called as e.
Cryptomath Module
The source code of cryptomath module which follows all the basic implementation of RSA
algorithm is as follows:
if gcd(a, m) != 1:
return None
u1, u2, u3 = 1, 0, a
v1, v2, v3 = 0, 1, m
while v3 != 0:
q = u3 // v3
63
Cryptography with Python
v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3),
v1, v2, v3
return u1 % m
RabinMiller Module
The source code of RabinMiller module which follows all the basic implementation of RSA
algorithm is as follows:
import random
def rabinMiller(num):
s = num - 1
t = 0
while s % 2 == 0:
s = s // 2
t += 1
def isPrime(num):
if (num < 2):
return False
lowPrimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227,
229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
64
Cryptography with Python
317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509,
521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617,
619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727,
733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947,
953, 967, 971, 977, 983, 991, 997]
if num in lowPrimes:
return True
return rabinMiller(num)
def generateLargePrime(keysize=1024):
while True:
num = random.randrange(2**(keysize-1), 2**(keysize))
if isPrime(num):
return num
def main():
makeKeyFiles('RSA_demo', 1024)
def generateKey(keySize):
# Step 1: Create two prime numbers, p and q. Calculate n = p * q.
print('Generating p prime...')
p = rabinMiller.generateLargePrime(keySize)
print('Generating q prime...')
q = rabinMiller.generateLargePrime(keySize)
n = p * q
65
Cryptography with Python
publicKey = (n, e)
privateKey = (n, d)
if os.path.exists('%s_pubkey.txt' % (name)) or
os.path.exists('%s_privkey.txt' % (name)):
sys.exit('WARNING: The file %s_pubkey.txt or %s_privkey.txt already
exists! Use a different name or delete these files and re-run this program.' %
(name, name))
print()
print('The public key is a %s and a %s digit number.' %
(len(str(publicKey[0])), len(str(publicKey[1]))))
print('Writing public key to file %s_pubkey.txt...' % (name))
fo = open('%s_pubkey.txt' % (name), 'w')
66
Cryptography with Python
print()
print('The private key is a %s and a %s digit number.' %
(len(str(publicKey[0])), len(str(publicKey[1]))))
print('Writing private key to file %s_privkey.txt...' % (name))
fo = open('%s_privkey.txt' % (name), 'w')
fo.write('%s,%s,%s' % (keySize, privateKey[0], privateKey[1]))
fo.close()
67
Cryptography with Python
Output
The public key and private keys are generated and saved in the respective files as shown
in the following output.
68
28. Cryptography with Python – RSA Cipher Encryption
Cryptography with Python
In this chapter, we will focus on different implementation of RSA cipher encryption and
the functions involved for the same. You can refer or include this python file for
implementing RSA cipher algorithm implementation.
We have initialized the hash value as SHA-256 for better security purpose. We will use a
function to generate new keys or a pair of public and private key using the following code.
def newkeys(keysize):
random_generator = Random.new().read
key = RSA.generate(keysize, random_generator)
private, public = key, key.publickey()
return public, private
def importKey(externKey):
return RSA.importKey(externKey)
For encryption, the following function is used which follows the RSA algorithm:
Two parameters are mandatory: message and pub_key which refers to Public key. A
public key is used for encryption and private key is used for decryption.
69
Cryptography with Python
def importKey(externKey):
return RSA.importKey(externKey)
def getpublickey(priv_key):
return priv_key.publickey()
70
29. Cryptography with Python – RSA Cipher Decryption
Cryptography with Python
This chapter is a continuation of the previous chapter where we followed step wise
implementation of encryption using RSA algorithm and discusses in detail about it.
Authorization
Authorization is the process to confirm that the sender is the only one who have
transmitted the message. The following code explains this:
71
Cryptography with Python
Authentication
Authentication is possible by verification method which is explained as below:
The digital signature is verified along with the details of sender and recipient. This adds
more weight age for security purposes.
def importKey(externKey):
72
Cryptography with Python
return RSA.importKey(externKey)
def getpublickey(priv_key):
return priv_key.publickey()
74
30. Cryptography with Python – Hacking RSA Cipher
Cryptography with Python
Hacking RSA cipher is possible with small prime numbers, but it is considered impossible
if it is used with large numbers. The reasons which specify why it is difficult to hack RSA
cipher are as follows:
Brute force attack would not work as there are too many possible keys to work
through. Also, this consumes a lot of time.
Dictionary attack will not work in RSA algorithm as the keys are numeric and does
not include any characters in it.
M = C^d mod n
With the help of small prime numbers, we can try hacking RSA cipher and the sample code
for the same is mentioned below:
def p_and_q(n):
data = []
for i in range(2, n):
if n % i == 0:
data.append(i)
return tuple(data)
def main():
e = int(input("input e: "))
n = int(input("input n: "))
c = int(input("input c: "))
# t = 123
# private key = (103, 143)
p_and_q_v = p_and_q(n)
# print("[p_and_q]: ", p_and_q_v)
d = private_index(e, euler_v)
plain = decipher(d, n, c)
print("plain: ", plain)
if __name__ == "__main__":
main()
76
Cryptography with Python
Output
The above code produces the following output:
77