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

Lab 2 Crypto 22BCE1013

The document outlines a lab exercise on DES encryption and decryption, detailing both server and client code implementations. It explains the encryption process, including padding, initial permutation, bit reversal, and final permutation, as well as the decryption process that reverses these steps. Additionally, it describes the flow of execution between the client and server during the encryption and decryption of binary messages.

Uploaded by

bhavya sahni
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)
8 views5 pages

Lab 2 Crypto 22BCE1013

The document outlines a lab exercise on DES encryption and decryption, detailing both server and client code implementations. It explains the encryption process, including padding, initial permutation, bit reversal, and final permutation, as well as the decryption process that reverses these steps. Additionally, it describes the flow of execution between the client and server during the encryption and decryption of binary messages.

Uploaded by

bhavya sahni
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/ 5

BCSE309P - Cryptography and

Network Security Lab


Lab 2: DES Encryption and Decryption

Submitted by : Shashwat Gupta (22BCE1013)


DES Encryp on and Decryp on
Server Code –
import socket
IP_TABLE = [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]
FP_TABLE = [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]
def pad_64(data):
return data.ljust(64, '0')
def permute(data, table):
return ''.join(data[i - 1] for i in table)
def decrypt(data, key):
data = pad_64(data)
ini al_perm = permute(data, IP_TABLE)
reversed_bits = ini al_perm[::-1]
return permute(reversed_bits, FP_TABLE)
if __name__ == "__main__":
ip, port = "127.0.0.1", 1234
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_sock.bind((ip, port))
server_sock.listen(5)
print(f"Server running on {ip}:{port}")
while True:
client_sock, addr = server_sock.accept()
print(f"Connec on from {addr[0]}:{addr[1]}")
encrypted_data = client_sock.recv(1024).decode()
key = "hardkey"
decrypted_data = decrypt(encrypted_data, key)
print("Decrypted binary message:", decrypted_data)
client_sock.sendall(decrypted_data.encode())
client_sock.close()
ti
ti
ti
ti
ti
Client Code-
import socket
IP_TABLE = [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]
FP_TABLE = [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]

def pad_64(data):
return data.ljust(64, '0')

def permute(data, tbl):


return ''.join(data[i - 1] for i in tbl)

def encrypt(data, key):


data = pad_64(data)
permuted = permute(data, IP_TABLE)
reversed_data = permuted[::-1]
return permute(reversed_data, FP_TABLE)

if __name__ == "__main__":
ip, port = "127.0.0.1", 1234
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
user_data = input("Enter binary string (up to 64 bits): ")
key = "key123"
encrypted = encrypt(user_data, key)
print("Encrypted Output: ", encrypted)
sock.sendall(encrypted.encode())
response = sock.recv(1024)
print("Server Response:", response.decode())
Encryp on Technique Report :
Data Encryp on Standard (DES) : It uses basic permuta ons and bit manipula ons to mimic the
structure of a block cipher. Here are the key steps:
• Padding to 64 Bits - If the input binary string is less than 64 bits, it is padded with zeros on the right
to ensure a xed length of 64 bits.
• Ini al Permuta on (IP) - The input binary string undergoes an ini al permuta on using a
prede ned IP_TABLE, This rearranges the bits based on the table's mapping.
• Bit Reversal - A er the ini al permuta on, the resul ng binary string is reversed. This simple
opera on mimics the complexity introduced in real encryp on algorithms.
• Final Permuta on (FP) - Finally, the reversed string is permuted again using a FP_TABLE, which
rearranges the bits into the nal encrypted format.
• Decryp on - Decryp on reverses the process: apply the nal permuta on, reverse the bits, and
apply the ini al permuta on to retrieve the original plaintext.

Server Code
Purpose - Acts as a receiver and decryptor of messages sent by the client.
1. Socket Ini aliza on:
- A socket is created and bound to the IP `127.0.0.1` (localhost) and port `1234`.
- The server listens for incoming client connec ons.
2. Receiving Data:
- The server accepts a client connec on, receives the encrypted binary string, and decrypts it.
3. Decryp on Process:
- The received binary string is padded to 64 bits.
- The `IP_TABLE` and `FP_TABLE` are used for permuta on and decryp on logic.
4. Response to Client:
- The decrypted message is sent back to the client.

Client Code
Purpose: Encrypts a binary message and sends it to the server.
1. Socket Ini aliza on:
- A socket is created and connected to the server at `127.0.0.1` and port `1234`.
2. User Input:
- The client takes a binary string (up to 64 bits) from the user.
3. Encryp on Process:
- The binary string is padded, permuted using `IP_TABLE`, reversed, and then permuted again using
`FP_TABLE`.
4. Sending Data:
- The encrypted binary string is sent to the server.
5. Receiving Response:
- The client receives the decrypted binary message from the server and displays it
Flow of Execu on
1. Client:
- Encrypts the user-provided binary string.
- Sends the encrypted string to the server.
2. Server:
- Receives the encrypted string.
- Decrypts it back to the original binary message.
- Sends the decrypted message back to the client.
3. Client:
- Receives the decrypted message and displays it.
ti
ti
fi
ti
ti
ti
ti
fi
ti
ti
ti
ti
ti
ti
ti
ft
ti
ti
ti
ti
ti
fi
ti
ti
ti
ti
ti
ti
fi
ti
ti
ti
ti
ti
ti
Output-

You might also like