0% found this document useful (0 votes)
22 views9 pages

Cryptomoodle 2

Uploaded by

Rohith Chanda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views9 pages

Cryptomoodle 2

Uploaded by

Rohith Chanda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name: C.

Rohith Register No: 21BCE0810

Cryptography and Network Security


Lab assignment – 1

Name: C. Rohith
Register No: 21BCE0810
Slot: L13 + L 14
Name: C. Rohith Register No: 21BCE0810

1. Implement the following substitution cipher techniques without using standard


cryptographic library Functions • Data Encryption Standard (DES) • Simplified
Data Encryption Standard (SDES).
A.
Aim: To implement the above algorithm without using standard cryptographic
library.
1. Key Generation:
• Input: 64-bit binary key.
• Output: Sixteen 48-bit round keys (K1 to K16).
• Procedure:
• Perform a permutation on the 64-bit key using the initial
permutation (IP) table.
• Split the permuted key into two 32-bit halves, C0 and D0.
• Perform sixteen rounds of key generation:
• Generate sixteen 48-bit round keys using a combination of
shifting, permutation, and compression operations.
2. Encryption:
• Input: 64-bit plaintext and sixteen 48-bit round keys (K1 to K16).
• Output: 64-bit ciphertext.
• Procedure:
• Perform an initial permutation (IP) on the 64-bit plaintext.
• Split the permuted plaintext into two 32-bit halves, L0 and R0.
• Perform sixteen rounds of encryption:
• Expand Rn-1 to 48 bits using the expansion permutation (E)
table.
• XOR the expanded Rn-1 with the round key Kn.
• Apply S-box substitution to the result.
• Permute the result using the permutation (P) table.
• XOR the permuted result with Ln-1 to obtain Rn.
• Set Ln = Rn-1.
• Concatenate Ln and R16 and perform the final permutation (IP^-1)
to obtain the ciphertext.
3. Decryption:
• Input: 64-bit ciphertext and sixteen 48-bit round keys (K1 to K16).
• Output: 64-bit plaintext.
• Procedure:
• Perform the same steps as encryption, but use the round keys K16
to K1 in reverse order during decryption.
Name: C. Rohith Register No: 21BCE0810

4. Permutation Tables:
• IP: Initial permutation table.
• E: Expansion permutation table.
• P: Permutation table used in the function F.
• IP^-1: Final permutation table.
5. S-boxes:
• Eight 4x16 substitution boxes (S1 to S8) are used in the function F for
substituting 6-bit inputs to 4-bit outputs.
6. Initial and Final Permutations:
• The initial and final permutations (IP and IP^-1) are fixed permutations
applied at the beginning and end of the encryption and decryption
processes, respectively.

Code:
def generate_round_keys(key):
# Perform initial permutation on the key
key = initial_permutation(key)
# Split the key into two halves
left_half, right_half = key[:28], key[28:]
round_keys = []
for i in range(16):
# Perform circular left shift on both halves
left_half = left_shift(left_half, SHIFTS[i])
right_half = left_shift(right_half, SHIFTS[i])
# Combine and permute the halves to generate the round key
round_key = final_permutation(left_half + right_half)
round_keys.append(round_key)
return round_keys

def initial_permutation(key):
# Perform initial permutation
return permute(key, INITIAL_PERMUTATION)

def final_permutation(key):
# Perform final permutation
return permute(key, FINAL_PERMUTATION)

def permute(data, table):


# Perform permutation using the given table
return [data[i - 1] for i in table if 1 <= i <= len(data)]

def left_shift(data, n):


Name: C. Rohith Register No: 21BCE0810

# Perform circular left shift


return data[n:] + data[:n]

# Define permutation tables


INITIAL_PERMUTATION = [58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7]

FINAL_PERMUTATION = [40, 8, 48, 16, 56, 24, 64, 32,


39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25]

SHIFTS = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
def des_encrypt(plain_text, round_keys):
# Initial permutation
plain_text = initial_permutation(plain_text)
# Split plaintext into left and right halves
left_half, right_half = plain_text[:32], plain_text[32:]
# Perform 16 rounds of DES
for i in range(16):
left_half, right_half = right_half, xor(left_half, feistel(right_half,
round_keys[i]))
# Final permutation
cipher_text = final_permutation(right_half + left_half)
return cipher_text

def des_decrypt(cipher_text, round_keys):


# Initial permutation
cipher_text = initial_permutation(cipher_text)
# Split ciphertext into left and right halves
left_half, right_half = cipher_text[:32], cipher_text[32:]
# Perform 16 rounds of DES in reverse order
for i in range(15, -1, -1):
left_half, right_half = right_half, xor(left_half, feistel(right_half,
round_keys[i]))
Name: C. Rohith Register No: 21BCE0810

# Final permutation
plain_text = final_permutation(right_half + left_half)
return plain_text

def feistel(data, key):


# Expand 32-bit data to 48 bits
expanded_data = permute(data, EXPANSION_PERMUTATION)
# XOR with round key
xor_result = xor(expanded_data, key)
# Apply S-box substitution
s_box_output = s_box_substitution(xor_result)
# Permute using the P-box
return permute(s_box_output, P_BOX)

def s_box_substitution(data):
s_box_output = []
for i in range(8):
if i * 6 + 5 < len(data): # Check if the index is within range
row = 2 * data[i * 6] + data[i * 6 + 5]
col = 8 * data[i * 6 + 1] + 4 * data[i * 6 + 2] + 2 * data[i * 6 + 3]
+ data[i * 6 + 4]
val = S_BOX[i][row][col]
s_box_output += [int(bit) for bit in bin(val)[2:].zfill(4)]
else:
# Handle the case where the index is out of range
# For example, you could set row and col to some default values
row = 0
col = 0
# or you could raise an exception to indicate the error
raise IndexError("Index out of range")
return s_box_output

def xor(data1, data2):


return [(int(bit1) ^ int(bit2)) for bit1, bit2 in zip(data1, data2)]

# Define permutation tables and S-boxes


EXPANSION_PERMUTATION = [32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1]
Name: C. Rohith Register No: 21BCE0810

P_BOX = [16, 7, 20, 21, 29, 12, 28, 17,


1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9,
19, 13, 30, 6, 22, 11, 4, 25]

S_BOX = [
[
[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
[0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
[4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
[15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]
],
...
# Remaining S-boxes omitted for brevity
]

# Usage example
key = [1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1,
1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1]

plain_text = [1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0]
round_keys = generate_round_keys(key)
cipher_text = des_encrypt(plain_text, round_keys)
print("Encrypted:", cipher_text)
decrypted_text = des_decrypt(cipher_text, round_keys)
print("Decrypted:", decrypted_text)

# Key generation for SDES


def generate_key():
key = input("Enter 10-bit key: ")
while len(key) != 10 or not all(bit.isdigit() for bit in key):
print("Key must be a 10-bit binary number.")
key = input("Enter 10-bit key: ")
return key

# Initial Permutation (IP) table


IP = [2, 6, 3, 1, 4, 8, 5, 7]

# Inverse Initial Permutation (IP^-1) table


IP_inverse = [4, 1, 3, 5, 7, 2, 8, 6]

# Expansion permutation (EP) table


EP = [4, 1, 2, 3, 2, 3, 4, 1]
Name: C. Rohith Register No: 21BCE0810

# Permutation (P4) table


P4 = [2, 4, 3, 1]

# S-boxes
S0 = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]
S1 = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]

# Key generation
def key_generation(key):
key1 = [key[0], key[1], key[2], key[3], key[4], key[5]]
key2 = [key[5], key[6], key[7], key[8], key[9], key[0]]
return key1, key2

# Initial permutation (IP)


def initial_permutation(plain_text):
permuted_text = [plain_text[i - 1] for i in IP]
return permuted_text

# Inverse initial permutation (IP^-1)


def inverse_initial_permutation(cipher_text):
permuted_text = [cipher_text[i - 1] for i in IP_inverse]
return permuted_text

# F-function
def f_function(input_bits, sub_key):
# Expansion permutation (EP)
expanded_bits = [input_bits[i - 1] for i in EP]

# XOR with subkey


xor_result = [int(expanded_bits[i]) ^ int(sub_key[i]) for i in
range(len(expanded_bits))]

# S-box substitution
s0_input = [xor_result[0], xor_result[3]]
s1_input = [xor_result[1], xor_result[2]]
s0_row = int(''.join(map(str, s0_input)), 2)
s0_col = int(''.join(map(str, xor_result[4:6])), 2)
s1_row = int(''.join(map(str, s1_input)), 2)
s1_col = int(''.join(map(str, xor_result[6:])), 2)
s0_output = S0[s0_row][s0_col]
s1_output = S1[s1_row][s1_col]

# Permutation (P4)
output = [s0_output, s1_output]
Name: C. Rohith Register No: 21BCE0810

permuted_output = [output[i - 1] for i in P4]

return permuted_output

# Encryption function
def encrypt(plain_text, key):
# Generate subkeys
key1, key2 = key_generation(key)

# Initial permutation
permuted_text = initial_permutation(plain_text)

# Split text into two parts


left_half = permuted_text[:4]
right_half = permuted_text[4:]

# Round 1
f_output = f_function(right_half, key1)
temp = [left_half[i] ^ f_output[i] for i in range(len(left_half))]
left_half = right_half
right_half = temp

# Round 2
f_output = f_function(right_half, key2)
cipher_text = left_half + [right_half[i] ^ f_output[i] for i in
range(len(right_half))]

# Inverse initial permutation


encrypted_text = inverse_initial_permutation(cipher_text)

return encrypted_text

# Decryption function (same as encryption for SDES)


def decrypt(cipher_text, key):
return encrypt(cipher_text, key)

# Main function
def main():
plain_text = input("Enter 8-bit plaintext: ")
key = generate_key()

encrypted_text = encrypt(plain_text, key)


decrypted_text = decrypt(encrypted_text, key)

print("Plaintext:", plain_text)
Name: C. Rohith Register No: 21BCE0810

print("Key:", key)
print("Encrypted Text:", ''.join(map(str, encrypted_text)))
print("Decrypted Text:", ''.join(map(str, decrypted_text)))

if __name__ == "__main__":
main()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy