Amey B-50 System Security Lab Experiment-1
Amey B-50 System Security Lab Experiment-1
Class: TE Sem.: VI
PART A
(PART A: TO BE REFERRED BY STUDENTS)
Experiment No.01
A.1 Aim:
Design and Implementation of a product cipher using Substitution and
Transposition ciphers.
A.2 Prerequisite:
1. Basic Knowledge of Cryptography.
2. Knowledge of Substitution Cipher techniques.
A.3 Outcome:
After the successful completion of this experiment, students will be able to
apply the knowledge of symmetric cryptography to implement simple ciphers.
A.4 Theory:
them non-readable.
There are two types of cryptographic algorithms:
● Substitution and
● Transposition.
1
The process of converting from plaintext to ciphertext is known as enciphering
or encryption; restoring the plaintext from the ciphertext is deciphering or
decryption.
The many schemes used for encryption constitute the area of study known as
cryptography. Such a scheme is known as a cryptographic system or a cipher.
• Plaintext: This is the original intelligible message or data that is fed into
the algorithm as input.
• Secret key: The secret key is also input into the encryption algorithm. The
key is a value independent of the plaintext and the algorithm. The algorithm
will produce a different output depending on the specific key being used at
the time. The exact substitutions and transformations performed by the
algorithm depend on the key.
2
will produce two different ciphertexts. The ciphertext is a random stream of
data and, as it stands, is unintelligible.
2. The sender and receiver must have obtained copies of the secret key securely
and must keep the key secure. If someone can discover the key and knows
the algorithm, all communication using this key is readable.
3
These chips are widely available and incorporated into several products. With
the use of symmetric encryption, the principal security problem is
maintaining the secrecy of the key.
For encryption, a key of the form K=[K1, K2, K3,……, Kj] is generated. If the key
is generated at the message source, then it must also be provided to the
destination employing some secure channel.
Alternatively, a third party could generate the key and securely deliver it to
both source and destination.
With the message and the encryption key K as input, the encryption
algorithm forms the ciphertext Y=[Y1, Y2, Y3,……….., Yn]. We can write this as;
Y=E(K, X)
This notation indicates that Y is produced by using encryption algorithm E as
a function of the plaintext X, with the specific function determined by the
value of the key K.
4
The intended receiver, in possession of the key, can invert the
transformation:
X=D(K,Y)
An opponent, observing Y but not having access to K or X, may attempt to
recover X or K or both X and K.It is assumed that the opponent knows the
encryption (E) and decryption (D) algorithms. If the opponent is interested in
only this particular message, then the focus of the effort is to recover X by
generating a plaintext estimate X. Often, however, the opponent is
interested in being able to read future messages as well, in which case an
attempt is made to recover K by generating an estimate K.
A.5 EXAMPLE:
1. Plaintext: CryPto(Sender’s input)
Key: 5(agreement key, here input)
Cipher text: (C+5)(r+5)(y+5)(P+5)(t+5)(o+5)
: HwdUyt(output)
2. Cipher Text: XdkcZm(Receiver’s Input)
Key: 5(agreement key, here input)
Plaintext: CiphEr (output)
A.6 ALGORITHM:
@Encryption.....
A - Substitution Cipher
1. Take the Value of Plaintext.
2. Take Value of Key.
3. Run the loop n times here n will be the length of the text.
4. Convert the string into characters and that same time add with Key.
5. After Addition, mod 26 or 125 can be used depending on where you want to
limit that entry of plain text.
6. Then save and convert the calculated part (character) into a string or
StringBuffer.
7. Now the Final output will be the actual Cipher Text.
B - Transposition Cipher
1. The input of this Cipher will be the output from the Substitution Cipher.
2. Convert into a matrix with the help of the key (the key is different these
times).
3. And with the help of the matrix, just lay down the content of the matrix or
save the content into a simple string.
4. This String will be the Cipher Text.
5
@Decryption.....
B - Substitution Cipher
1. After taking the input.
2. Use the same key.
3. Follow the Same producer as encryption, here the only difference is that
instead of addition we will perform subtraction with the help of the key.
4. The result will be the Plain Text. And the code is finally decrypted.
6
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per the following segments within two hours
of the practical. The soft copy must be uploaded on the Blackboard or emailed to the
concerned lab in charge faculties at the end of the practical in case there is no
Blackboard access available)
Product_Cipher.py
7
key = 'abcdefghijklmnopqrstuvwxyz'
for l in plaintext.lower():
try:
i = (key.index(l) + n) % 26
result += key[i]
except ValueError:
result += l
return result.lower()
for l in ciphertext:
try:
i = (key.index(l) - n) % 26
result += key[i]
except ValueError:
result += l
return result
8
➔ Cipher is a method to implement encryption and decryption of messages
travelling in a network. It is used to increase the confidentiality of the
messages.
➔ In cryptology, the discipline concerned with studying cryptographic
algorithms, a cipher is an algorithm for encrypting and decrypting data.
Symmetric key encryption, also called secret key encryption, depends on
ciphers' use, which operates symmetrically. With symmetric algorithms, the
same cipher and encryption keys are applied to data in the same way,
whether the objective is to convert plaintext to ciphertext or ciphertext to
plaintext. A cipher transforms information by processing the original
plaintext characters (or other data) into ciphertext, which should be random.
➔ Traditionally, ciphers used two main types of transformation: transposition
ciphers, which keep all the original bits of data in a byte but mix their order,
and substitution ciphers, which replace specific data sequences with other
specific data sequences. For example, one type of substitution would be to
transform all bits with a value of 1 to a value of 0, and vice versa. The data
output by either method is called the ciphertext.
➔ Modern ciphers enable private communication in many different networking
protocols, including the Transport Layer Security (TLS) protocol and others
that offer network traffic encryption. Many communication technologies,
including phones, digital television, and ATMs, rely on ciphers to maintain
security and privacy.
Types of Cipher
Several types of cipher are given as follows:
1. Caesar Cipher
➔ In Caesar cipher, the set of characters of plain text is replaced by any other
character, symbols, or numbers. It is a very weak technique of hiding text. In
Caesar’s cipher, each alphabet in the message is replaced by three places
9
down. Let’s see one example. The plain text is EDUCBA. As a Caesar cipher,
each alphabet is replaced by three-place down, so E will replace by H, D will
replace by G, U will replace by X, C will replace by F, B will replace by E and A
will replace by D. So here the plain text is EDUCBA and ciphertext Is HGXFED.
➔ Caesar cipher algorithm is as follows:
1. Read each alphabet of plain text.
2. Replace each alphabet with 3 places down.
3. Repeat the process for all alphabet in plain text.
➔ A Modified Version of Caesar Cipher: This cipher works the same as Caesar
cipher, the only difference is – in Caesar cipher, each alphabet is replaced by
three-place down wherein a modified version of Caesar cipher, the number is
decided by a user to replace the alphabet and this number will be constant.
For example, EDUCBA and number for the replacement are 1, so E will replace
by F, D will replace by E, U will replace by V, C will replace by D, B will replace
by C and A will replace by B. So here the plain text is EDUCBA and ciphertext
Is FEVDCB.
➔ A modified version of the Caesar cipher algorithm is as follows
1. Read each alphabet of plain text.
2. Take the number for replacement.
3. Replace each alphabet with a specified number down.
4. Repeat the process for all alphabet in plain text.
2. Monoalphabetic Cipher
➔ As Caesar cipher and a modified version of Caesar cipher is easy to break, a
monoalphabetic cipher comes into the picture. In monoalphabetic, each
alphabet in plain text can be replaced by any other alphabet except the
original alphabet. That is A can be replaced by any other alphabet from B to
Z. B can be replaced by A or C to Z. C can be replaced by A, B, and D to z, etc.
➔ Mono alphabetic cipher causes difficulty to crack the message as there are
random substitutions and a large number of permutations and combinations
are available.
10
➔ In polygram substitution cipher, rather than replacing each alphabet with
another, the Block of alphabets is replaced with another block of alphabets.
Replace EDUCBA with XYQLAB. In this case, EDUCBA will be replaced with
XYQLAB, but EDU can be replaced with another set of the block, let’s assume
EDU will replace LOD. In this type of ciphers, the replacement of plain text is
done through the block by block rather than character by character.
6. Playfair Cipher
➔ Playfair cipher is also called Playfair square. It is a cryptographic technique
that is used to encrypt the data.
➔ The Playfair cipher process is as follows:
1. Creation and population of the matrix.
2. Encryption process.
➔ Let’s discuss the above-mentioned steps in detail: manners, creation and
population of the matrix. It uses a 5 * 5 matrix to store the keyword or the
key which is used for the encryption and decryption process.
➔ This step is working as follows:
1. Enter the keyword in the matrix in a row-wise manner i.e., from left to
right and top to bottom.
2. Skip the duplicate words in the keyword.
3. Fill the remaining spaces with the rest of the alphabets (A – Z) that
were not a part of the keyword.
Note: while doing so, combine I and J in the same cell of the table. i.e. If I or J is
present in the keyword, discard both I and J while filling the remaining space
encryption process.
11
➔ The encryption process works as follows:
1. Break the alphabets into groups (each group must contain two
values). The encryption processes will be performed on these groups.
2. If both alphabets in the group are the same, add x after the first
alphabet.
3. If both the alphabet in the group are present in the same row of the
matrix, replace them with the alphabets to their immediate right,
respectively. If the original group is on the right side of the row, then
wrapping around to the row’s left side happens.
4. If both the alphabet in the group are present in the same column,
replace them with the alphabet immediately below. If the original
group is on the bottom side of the row, then wrapping around to the
row’s top side happens.
5. If both the alphabet in the group are not in the same row or column,
replace them with the alphabets in the same row immediately but at
the other pair of corners of the rectangle, defined by the original
group.
7. Hill Cipher
➔ Hill cipher works on multiple alphabets at the same time. Hill cipher works as
follows:
1. Assign the number to each alphabet in the plain text. A = 0, B= 1….z =
25.
2. Organize the plain text message as a matrix of numbers based on the
above step that is in number format. The resultant matrix is called a
plain text matrix.
3. Multiply the plain text matrix with a randomly chosen key. Note that
the key matrix must be the size of n*n, where n stands for the number
of rows in a plain text matrix.
4. Multiply both the matrix, i.e., step 2 and step 3.
5. Calculate the mod 26 value of the above matrix, i.e., matrix results in
step 4.
6. Now translate the numbers to alphabets i.e. 0 =A, 1 =B, etc.
7. The result of step 6 becomes our ciphertext.
12
depending on a key. For example, with a shift of 1, A would be replaced by B,
B would become C, and so on.
Note: Special case of Substitution cipher is known as Caesar cipher where the key is
taken as 3.
Mathematical representation
➔ The encryption can be represented using modular arithmetic by first
transforming the letters into numbers, according to the scheme, A = 0, B = 1,
…, Z = 25.
➔ Encryption of a letter by a shift n can be described mathematically as.
En (x )=( x+ n)mod 26❑
(Encryption Phase with shift n)
Dn (x )=(x −n)mod 26❑
(Decryption Phase with shift n)
Original: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Shifted: DEFGHIJKLMNOPQRSTUVWXYZABC
Examples:
13
1. Create a list of all the characters.
2. Create a dictionary to store the substitution for all characters.
3. For each character, transform the given character as per the rule,
depending on whether we’re encrypting or decrypting the text.
4. Print the new string generated.
Example
➔ A simple example for a transposition cipher is a 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 ciphertext.
➔ Consider the plain text Amey Thakur, and let us apply the simple columnar
transposition technique shown below.
ENCRYPTION
M E G A
4 2 3 1
A m e y
_ T h a
k u r _
Output:
14
DECRYPTION
1. The recipient has to work out the column lengths by dividing the message
length by the key size to decipher it.
2. Then, write the message out in columns again, then re-order the columns by
reforming the keyword.
Encryption
➔ The encryption process’s first step is to transform each of the letters in the
plaintext alphabet to the corresponding integer in the range 0 to m-1. With
this done, the encryption process for each letter is given by
A and b are the key for the cipher; this means that we multiply our integer
value for the plaintext letter by adding b to the result. Finally, we take this
modulus m (that is, we take the remainder when the solution is divided by m
or take away the alphabet’s length until we get a number less than this
length).
➔ For example, let us encrypt the plaintext "affine cipher,” using the key a = 5, b
= 8. Firstly we must find the integer value of each of the letters in the
plaintext alphabet (the standard alphabet of 26 letters in this case). The
table below gives these values.
The typical values for the alphabet of 26 letters. Notice we start at 0, not 1.
15
➔ With the integer values of the plaintext letters found, the next step is to
perform the calculations on those values. In this instance, the math needed
is (5x+8). Finally, we must ensure that all our answers are calculated mod 26
and convert the integers back to ciphertext letters. All this information is
shown in the table below.
The affine cipher with a = 5, b = 8. We work out values of letters, then do the
calculations before converting numbers back to letters.
Decryption
➔ In deciphering the ciphertext, we must perform the opposite (or inverse)
functions on the ciphertext to retrieve the plaintext. Once again, the first
step is to convert each of the ciphertext letters into their integer values. We
must now perform the following calculation on each integer.
The decryption process for a key of a = 5, b = 8. We had to find the inverse of a first,
which is 21.
16
➔ We retrieve our plaintext of "affine cipher.”
B.4 Conclusion:
(Write an appropriate conclusion.)
17