0% found this document useful (0 votes)
3 views5 pages

Practical NO1

This document provides an implementation of the Simplified Data Encryption Standard (S-DES) algorithm, including functions for permutation, key generation, encryption, and decryption. It defines S-boxes, permutation tables, and includes example usage for encrypting and decrypting an 8-bit plaintext using a 10-bit key. The implementation utilizes bitwise operations and transformations to achieve encryption and decryption.

Uploaded by

gaurav pawar
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)
3 views5 pages

Practical NO1

This document provides an implementation of the Simplified Data Encryption Standard (S-DES) algorithm, including functions for permutation, key generation, encryption, and decryption. It defines S-boxes, permutation tables, and includes example usage for encrypting and decrypting an 8-bit plaintext using a 10-bit key. The implementation utilizes bitwise operations and transformations to achieve encryption and decryption.

Uploaded by

gaurav pawar
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

Practical NO:1

# S-DES Implementation

# Permutation functions
def permute(bits, table):
return [bits[i - 1] for i in table]

def left_shift(bits, n):


return bits[n:] + bits[:n]

def xor(a, b):


return [i ^ j for i, j in zip(a, b)]

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

S1 = [
[0, 1, 2, 3],
[2, 0, 1, 3],
[3, 0, 1, 0],
[2, 1, 0, 3]
]

# Tables
P10 = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6]
P8 = [6, 3, 7, 4, 8, 5, 10, 9]
IP = [2, 6, 3, 1, 4, 8, 5, 7]
IP_INV = [4, 1, 3, 5, 7, 2, 8, 6]
EP = [4, 1, 2, 3, 2, 3, 4, 1]
P4 = [2, 4, 3, 1]

# Key Generation
def generate_keys(key):
key = permute(key, P10)
left, right = key[:5], key[5:]

left = left_shift(left, 1)
right = left_shift(right, 1)
K1 = permute(left + right, P8)

left = left_shift(left, 2)
right = left_shift(right, 2)
K2 = permute(left + right, P8)

return K1, K2

# S-Box lookup
def sbox_lookup(bits, sbox):
row = bits[0]*2 + bits[3]
col = bits[1]*2 + bits[2]
val = sbox[row][col]
return [val >> 1 & 1, val & 1]
# F-function
def fk(bits, key):
left, right = bits[:4], bits[4:]
temp = permute(right, EP)
temp = xor(temp, key)

left_sbox = sbox_lookup(temp[:4], S0)


right_sbox = sbox_lookup(temp[4:], S1)

temp = permute(left_sbox + right_sbox, P4)


left = xor(left, temp)

return left + right

def switch(bits):
return bits[4:] + bits[:4]

# Encryption
def encrypt(plaintext, key):
K1, K2 = generate_keys(key)

bits = permute(plaintext, IP)


bits = fk(bits, K1)
bits = switch(bits)
bits = fk(bits, K2)
ciphertext = permute(bits, IP_INV)

return ciphertext
# Decryption
def decrypt(ciphertext, key):
K1, K2 = generate_keys(key)

bits = permute(ciphertext, IP)


bits = fk(bits, K2)
bits = switch(bits)
bits = fk(bits, K1)
plaintext = permute(bits, IP_INV)

return plaintext

# Helper to convert string to bit list


def str_to_bits(s):
return [int(bit) for bit in s]

def bits_to_str(bits):
return ''.join(map(str, bits))

# Example Usage
key = str_to_bits("1010000010") # 10-bit key
plaintext = str_to_bits("11010111") # 8-bit plaintext

cipher = encrypt(plaintext, key)


print("Ciphertext:", bits_to_str(cipher))

decrypted = decrypt(cipher, key)


print("Decrypted:", bits_to_str(decrypted))
Output

You might also like