Program:
Name : Anjum Maner
Roll no :3129
# S-DES Key Generation and Encryption/Decryption Functions
# P10 and P8 Permutation tables
P10 = [2, 4, 1, 6, 3, 9, 0, 8, 7, 5]
P8 = [5, 2, 6, 3, 7, 4, 9, 8]
# Initial and Inverse Initial Permutation tables
IP = [1, 5, 2, 0, 3, 7, 4, 6]
IP_inv = [3, 0, 2, 4, 6, 1, 7, 5]
# S-Boxes
S0 = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 2, 0]]
S1 = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 2], [2, 1, 0, 3]]
# Helper functions
# Permutation P10 def
permute(p, table):
return [p[i] for i in table]
# Left shift function def
left_shift(bits, n):
return bits[n:] + bits[:n]
# XOR function def xor(bits1, bits2): return [bit1
^ bit2 for bit1, bit2 in zip(bits1, bits2)]
# S-box function def s_box(bits,
sbox): row = bits[0] * 2 +
bits[3]
col = bits[1] * 2 + bits[2]
return list(map(int, format(sbox[row][col], '02b')))
# Feistel function def feistel(bits,
key):
expanded_bits = permute(bits, [3, 0, 1, 2, 1, 2, 3, 0]) # Expansion/Permutation
xor_result = xor(expanded_bits, key) left, right = xor_result[:4],
xor_result[4:]
left_sbox = s_box(left, S0) right_sbox
= s_box(right, S1) return permute(left_sbox + right_sbox, [1, 3, 2,
0]) # P4 permutation
# Key generation from 10-bit key def
generate_keys(key): key =
permute(key, P10)
left, right = key[:5], key[5:]
# Generate K1 left = left_shift(left,
1) right = left_shift(right, 1)
k1 = permute(left + right, P8)
# Generate K2 left = left_shift(left,
2) right = left_shift(right, 2)
k2 = permute(left + right, P8)
return k1, k2
# S-DES Encryption def
s_des_encrypt(plaintext, key):
k1, k2 = generate_keys(key)
# Initial permutation ip_plaintext =
permute(plaintext, IP)
left, right = ip_plaintext[:4], ip_plaintext[4:]
# Round 1 round1 =
feistel(right, k1) left =
xor(left, round1) left,
right = right, left # Swap
# Round 2 round2 = feistel(right,
k2)
left = xor(left, round2)
# Final permutation
return permute(left + right, IP_inv)
# S-DES Decryption (reverse order of subkeys) def
s_des_decrypt(ciphertext, key): k1, k2 =
generate_keys(key)
# Initial permutation ip_ciphertext =
permute(ciphertext, IP) left, right =
ip_ciphertext[:4], ip_ciphertext[4:]
# Round 1
round1 = feistel(right, k2) # Use K2 first in decryption left
= xor(left, round1)
left, right = right, left # Swap
# Round 2
round2 = feistel(right, k1) # Use K1 second in decryption left
= xor(left, round2)
# Final permutation
return permute(left + right, IP_inv)
# Example usage if
__name__ == "__main__":
# Input: 8-bit plaintext and 10-bit key plaintext = [int(x) for x in
input("Enter 8-bit plaintext (space-separated):
").split()] key = [int(x) for x in input("Enter 10-bit key (space-
separated): ").split()]
print("\n-------------ENCRYPTION-------------")
ciphertext = s_des_encrypt(plaintext, key)
print("Ciphertext: ", ''.join(map(str, ciphertext)))
print("\n-------------DECRYPTION-------------") decrypted_text
= s_des_decrypt(ciphertext, key) print("Decrypted text: ",
''.join(map(str, decrypted_text)))
Output:
Enter 8-bit plaintext (space-separated): 1 0 0 1 0 1 1 1
Enter 10-bit key (space-separated): 1 1 0 1 0 0 1 1 1 0
-------------ENCRYPTION-------------
Ciphertext: 00110101
-------------DECRYPTION-------------
Decrypted text: 10010111