1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| import secrets from Crypto.Util.Padding import pad, unpad
FLAG = "fake_flag"
IP = [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 = [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]
E = [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]
P = [16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25]
PC1 = [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]
PC2 = [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]
shift_table = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
S_BOXES = [ [0, 1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7, 8], [2, 3, 4, 5, 6, 7, 8, 9], [3, 4, 5, 6, 7, 8, 9, 10], [4, 5, 6, 7, 8, 9, 10, 11], [5, 6, 7, 8, 9, 10, 11, 12], [6, 7, 8, 9, 10, 11, 12, 13], [7, 8, 9, 10, 11, 12, 13, 14]
]
def int_to_64b_bitstring(number): b = bin(number)[2:] if len(b) != 64: b = '0'*(64-len(b)) + b return b
def permute(block, table): res = '' for i in table: res = res + block[i - 1] return res
def rotate_left(key, bits): return key[bits:] + key[:bits]
def generate_round_keys_enc(key): round_keys = [] key = permute(key, PC1) left_half = key[:28] right_half = key[28:]
for round_num, shift_bits in enumerate(shift_table): left_half = rotate_left(left_half, shift_bits) right_half = rotate_left(right_half, shift_bits) round_key = permute(left_half + right_half, PC2) round_keys.append(round_key) return round_keys
def xor_strings(s1, s2): return ''.join(str(int(a) ^ int(b)) for a, b in zip(s1, s2))
def substitute(expanded_half): output = '' for i in range(0, len(expanded_half), 6): chunk = expanded_half[i:i + 6] row = int(chunk[:3], 2) col = int(chunk[3:], 2) val = S_BOXES[row][col] output += format(val, '04b') return output
def feistel_network(right_half, round_key): states = [] expanded_half = permute(right_half, E) states.append(expanded_half) xored_half = xor_strings(expanded_half, round_key) substituted_half = substitute(xored_half) permuted_half = permute(substituted_half, P) states.append(permuted_half) return states
def des_encrypt(plain_text, key): cipher_text = '' round_keys = generate_round_keys_enc(key)
plain_text = permute(plain_text, IP) left_half = plain_text[:32] right_half = plain_text[32:]
for round_key in round_keys[:-1]: feistel_output = feistel_network(right_half, round_key) new_right_half = xor_strings(left_half, feistel_output[-1]) left_half = right_half right_half = new_right_half feistel_output = feistel_network(right_half, round_keys[-1]) new_right_half = xor_strings(left_half, feistel_output[-1]) left_half = right_half right_half = new_right_half
cipher_text = permute(right_half + left_half, FP)
return cipher_text, feistel_output
def des_ecb_encrypt(plain_text, key): plain_text = pad(plain_text.encode(), 8) cipher_text = '' feistel_output = [] for i in range(0, len(plain_text), 8): block = int_to_64b_bitstring(int.from_bytes(plain_text[i:i + 8], 'big')) curr_cipher_text, curr_feistel_output = des_encrypt(block, key) cipher_text += curr_cipher_text feistel_output.append(curr_feistel_output) return cipher_text, feistel_output
def main(): key = int_to_64b_bitstring(secrets.randbits(64)) counter = 16 cipher_text_flag, feistel_output_flag = des_ecb_encrypt(FLAG, key) print('Welcome to your homemade supersafe encryption service made by Jack the Intern') print('Your credit for encrypted messages is at: ' + str(counter)) while True: command = input('Would you like to get the flag [1], encrypt a message yourself [2] or exit [3] \n>').strip() try: if command == "1": print("Flag = ", cipher_text_flag) print("Feistel output = ", feistel_output_flag) elif command == "2": if counter > 0: counter -= 1 data = input('Enter the plaintext you want to encrypt \n>') cipher_text, feistel_output = des_ecb_encrypt(data, key) print("Ciphertext = ", cipher_text) print("Feistel output = ", feistel_output) print("Your credit for encrypted messages is at: " + str(counter)) else: print("You have no credits for encrypted messages left.") elif command == "3": print("Exiting...") break else: print("Please enter a valid input") except: print("Something went wrong.")
if __name__ == "__main__": main()
|