Cryptomoodle 2
Cryptomoodle 2
Name: C. Rohith
Register No: 21BCE0810
Slot: L13 + L 14
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)
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
# Final permutation
plain_text = final_permutation(right_half + left_half)
return plain_text
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
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)
# 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
# F-function
def f_function(input_bits, sub_key):
# Expansion permutation (EP)
expanded_bits = [input_bits[i - 1] for i in EP]
# 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
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)
# 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))]
return encrypted_text
# Main function
def main():
plain_text = input("Enter 8-bit plaintext: ")
key = generate_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()