-- coding: utf-8 -- """ Created on Fri Dec 1 03:26:35 2023 @author: ML """ def…
- User
-- coding: utf-8 --
""" Created on Fri Dec 1 03:26:35 2023
@author: ML """
def pad_plaintext(plaintext): while len(plaintext) < 8: plaintext += “@” return plaintext[:8]
def text_to_binary(text): binary_text = “” for char in text: binary_char = bin(ord(char))[2:].zfill(8) binary_text += binary_char return binary_text
def split_blocks(plaintext): blocks = [] while len(plaintext) > 0: blocks.append(plaintext[:8]) plaintext = plaintext[8:] return blocks
def ascii_to_binary_key(key): binary_key = “” for char in key: binary_key += format(ord(char), ‘08b’) return binary_key
def apply_key_permutation(binary_key, permutation_table): permuted_key = “” for index in permutation_table: permuted_key += binary_key[index - 1] return permuted_key
def generate_subkeys(key): pc_1_table = [57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4]
pc_2_table = [14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32] if len(key) != 8: raise ValueError(“Key size must be 8 characters.”) binary_key = ascii_to_binary_key(key) permuted_key = apply_key_permutation(binary_key, pc_1_table) # Convert key to numeric value permuted_key = int(permuted_key, 2) subkeys = [] # Generate subkeys for each round for round in range(1, 17): # Left shifts for left half and right half of the key left_shifts = 1 if round in [1, 2, 9, 16] else 2 permuted_key = ((permuted_key << left_shifts) & 0xFFFFFFFFFFFF) | (permuted_key >> (28 - left_shifts)) # Use PC-2 table to convert the key to a 48-bit subkey subkey = apply_key_permutation(bin(permuted_key)[2:].zfill(56), pc_2_table) subkeys.append(subkey) return subkeysdef initial_permutation(block): # Perform initial permutation on the block initial_permutation_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]
permuted_block = “” for index in initial_permutation_table: permuted_block += block[index - 1] return permuted_blockdef expand_permutation(block): # Perform expansion permutation on the block expansion_table = [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]
expanded_block = “” for index in expansion_table: expanded_block += block[index - 1] return expanded_block - Assistant
- User
لايطبع output
- Assistant
The code you provided is not complete, as it seems to be missing the main logic where the functions are called and the output is printed. Below is an example of how you can use the functions and print the output:
plaintext = "Hello!" key = "SecretKey" # Pad the plaintext padded_plaintext = pad_plaintext(plaintext) # Convert the padded plaintext to binary binary_text = text_to_binary(padded_plaintext) # Split the binary text into blocks of 8 bits blocks = split_blocks(binary_text) # Generate the subkeys from the key subkeys = generate_subkeys(key) # Perform encryption for each block encrypted_blocks = [] for block in blocks: # Perform initial permutation permuted_block = initial_permutation(block) # Perform expansion permutation expanded_block = expand_permutation(permuted_block) # Apply round keys and substitute using S-boxes substituted_block = "" for i in range(8): s_box_input = expanded_block[i*6 : (i+1)*6] s_box_index = i // 2 substituted_block += substitute(s_box_input, s_box_index) # Perform final permutation final_block = substituted_block # Add the encrypted block to the list
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.