Aa
public
Apr 07, 2025
Never
63
1 1) LFSR: 2 import numpy as np 3 from pylfsr import LFSR 4 5 state = [0,0,0,1,0] 6 fpoly = [5,3] 7 L = LFSR(fpoly=fpoly,initstate =state) 8 9 k=10 10 seq_k = L.runKCycle(k) 11 12 print('10 bits') 13 print(L.arr2str(seq_k)) 14 print('') 15 16 seq_full = L.runFullPeriod() 17 18 print('Full period of LFSR = 31-cycles') 19 print(L.arr2str(seq_full)) 20 21 L.Viz() 22 23 2) RC4 One 24 import codecs 25 26 MOD = 256 27 28 def KSA(key): 29 key_length = len(key) 30 S = list(range(MOD)) 31 j = 0 32 for i in range(MOD): 33 j = (j + S[i] + key[i % key_length]) % MOD 34 S[i], S[j] = S[j], S[i] 35 return S 36 37 def PRGA(S): 38 i = 0 39 j = 0 40 while True: 41 i = (i + 1) % MOD 42 j = (j + S[i]) % MOD 43 S[i], S[j] = S[j], S[i] 44 K = S[(S[i] + S[j]) % MOD] 45 yield K 46 47 def get_keystream(key): 48 S = KSA(key) 49 return PRGA(S) 50 51 def encrypt_logic(key, text): 52 key = [ord(c) for c in key] 53 keystream = get_keystream(key) 54 res = [] 55 for c in text: 56 val = ("%02X" % (c ^ next(keystream))) 57 res.append(val) 58 return ''.join(res) 59 60 def encrypt(key, plaintext): 61 plaintext = [ord(c) for c in plaintext] 62 return encrypt_logic(key, plaintext) 63 64 def decrypt(key, ciphertext): 65 ciphertext = codecs.decode(ciphertext, 'hex_codec') 66 res = encrypt_logic(key, ciphertext) 67 return codecs.decode(res, 'hex_codec').decode('utf-8') 68 69 def main(): 70 key = 'not-so-random-key' 71 plaintext = 'Good work! Your implementation is correct' 72 ciphertext = encrypt(key, plaintext) 73 print('plaintext:', plaintext) 74 print('ciphertext:', ciphertext) 75 ciphertext = '2D7FEE79FFCE80B7DDB7BDA5A7F878CE298615476F86F3B890FD4746BE2D8F741395F884B4A35CE979' 76 decrypted = decrypt(key, ciphertext) 77 print('decrypted:', decrypted) 78 if plaintext == decrypted: 79 print('\nCongrats ! You made it.') 80 else: 81 print('Shit! You pooped your pants ! .-.') 82 83 def test(): 84 assert(encrypt('Key', 'Plaintext')) == 'BBF316E8D940AF0AD3' 85 assert(decrypt('Key', 'BBF316E8D940AF0AD3')) == 'Plaintext' 86 assert(encrypt('Wiki', 'pedia')) == '1021BF0420' 87 assert(decrypt('Wiki', '1021BF0420')) == 'pedia' 88 assert(encrypt('Secret', 'Attack at dawn')) == '45A01F645FC35B383552544B9BF5' 89 assert(decrypt('Secret', '45A01F645FC35B383552544B9BF5')) == 'Attack at dawn' 90 91 if __name__ == '__main__': 92 main() 93 94 3) RC4 Two 95 def key_scheduling(key): 96 sched = [i for i in range(256)] 97 i = 0 98 for j in range(256): 99 i = (i + sched[j] + key[j % len(key)]) % 256 100 sched[j], sched[i] = sched[i], sched[j] 101 return sched 102 103 def stream_generation(sched): 104 i = 0 105 j = 0 106 while True: 107 i = (1 + i) % 256 108 j = (sched[i] + j) % 256 109 sched[j], sched[i] = sched[i], sched[j] 110 yield sched[(sched[i] + sched[j]) % 256] 111 112 def encrypt(text, key): 113 text = [ord(char) for char in text] 114 key = [ord(char) for char in key] 115 sched = key_scheduling(key) 116 key_stream = stream_generation(sched) 117 ciphertext = '' 118 for char in text: 119 enc = str(hex(char ^ next(key_stream))).upper() 120 ciphertext += enc 121 return ciphertext 122 123 def decrypt(ciphertext, key): 124 ciphertext = ciphertext.split('0X')[1:] 125 ciphertext = [int('0x' + c.lower(), 0) for c in ciphertext] 126 key = [ord(char) for char in key] 127 sched = key_scheduling(key) 128 key_stream = stream_generation(sched) 129 plaintext = '' 130 for char in ciphertext: 131 dec = chr(char ^ next(key_stream)) 132 plaintext += dec 133 return plaintext 134 135 if __name__ == '__main__': 136 ed = input('Enter E for Encrypt, or D for Decrypt: ').upper() 137 if ed == 'E': 138 plaintext = input('Enter your plaintext: ') 139 key = input('Enter your secret key: ') 140 result = encrypt(plaintext, key) 141 print('Result:') 142 print(result) 143 elif ed == 'D': 144 ciphertext = input('Enter your ciphertext: ') 145 key = input('Enter your secret key: ') 146 result = decrypt(ciphertext, key) 147 print('Result:') 148 print(result) 149 else: 150 print('Error in input - try again.') 151 152 4) A5 One 153 import numpy as np 154 155 reg_1 = np.empty 156 reg_2 = np.empty 157 reg_3 = np.empty 158 159 def user_input(): 160 usrinput = np.array(list(map(int, input("Enter the 64 bit key with space between the elements").strip().split())), dtype=bool) 161 print(usrinput) 162 print(type(usrinput)) 163 if len(usrinput) == 64: 164 return usrinput 165 else: 166 while len(usrinput) != 64: 167 usrinput = np.array(list(map(int, input("Enter the 64 bit key with space between the elements").strip().split())), dtype=bool) 168 return usrinput 169 170 def load_key(key): 171 global reg_1, reg_2, reg_3 172 reg_1 = key[0:19] 173 reg_2 = key[19:41] 174 reg_3 = key[41:64] 175 print("registers loaded successfully") 176 177 def get_majority(a, b, c): 178 return int(a) + int(b) + int(c) > 1 179 180 def clock_a5(clocking): 181 global reg_1, reg_2, reg_3 182 c = clocking 183 while c != 0: 184 majority = get_majority(reg_1[8], reg_2[10], reg_3[10]) 185 if reg_1[8] == majority: 186 first_bit = int(reg_1[18]) ^ int(reg_1[17]) ^ int(reg_1[16]) ^ int(reg_1[13]) 187 temp_arr1 = np.empty_like(reg_1) 188 temp_arr1[0] = first_bit 189 temp_arr1[1:] = reg_1[:18] 190 reg_1 = temp_arr1 191 if reg_2[10] == majority: 192 first_bit = int(reg_2[20]) ^ int(reg_2[21]) 193 temp_arr2 = np.empty_like(reg_2) 194 temp_arr2[0] = first_bit 195 temp_arr2[1:] = reg_2[:21] 196 reg_2 = temp_arr2 197 if reg_3[10] == majority: 198 first_bit = int(reg_3[20]) ^ int(reg_3[21]) ^ int(reg_3[22]) 199 temp_arr3 = np.empty_like(reg_3) 200 temp_arr3[0] = first_bit 201 temp_arr3[1:] = reg_3[:22] 202 reg_3 = temp_arr3 203 output = int(reg_1[18]) ^ int(reg_2[21]) ^ int(reg_3[22]) 204 print(int(output), end='') 205 c -= 1 206 207 def main(): 208 key = user_input() 209 load_key(key) 210 clocking = int(input("Please enter the number of times to clock the Stream Generator")) 211 clock_a5(clocking) 212 213 main() 214 215 # 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 216 217 5) A5 Two 218 import re 219 import copy 220 import sys 221 222 x_length = 19 223 y_length = 22 224 z_length = 23 225 key_one = "" 226 x = [] 227 y = [] 228 z = [] 229 230 def load_registers(key): 231 i = 0 232 while i < x_length: 233 x.insert(i, int(key[i])) 234 i += 1 235 j = 0 236 p = x_length 237 while j < y_length: 238 y.insert(j, int(key[p])) 239 p += 1 240 j += 1 241 k = y_length + x_length 242 r = 0 243 while r < z_length: 244 z.insert(r, int(key[k])) 245 k += 1 246 r += 1 247 248 def set_key(key): 249 if len(key) == 64 and re.match("^([01])+", key): 250 key_one = key 251 load_registers(key) 252 return True 253 return False 254 255 def get_key(): 256 return key_one 257 258 def to_binary(plain): 259 s = "" 260 for i in plain: 261 binary = str(' '.join(format(ord(x), 'b') for x in i)) 262 j = len(binary) 263 while j < 8: 264 binary = "0" + binary 265 s += binary 266 j += 1 267 binary_values = [] 268 for k in range(len(s)): 269 binary_values.insert(k, int(s[k])) 270 return binary_values 271 272 def get_majority(x, y, z): 273 return 1 if (x + y + z > 1) else 0 274 275 def get_keystream(length): 276 x_temp = copy.deepcopy(x) 277 y_temp = copy.deepcopy(y) 278 z_temp = copy.deepcopy(z) 279 keystream = [] 280 for i in range(length): 281 majority = get_majority(x_temp[8], y_temp[10], z_temp[10]) 282 if x_temp[8] == majority: 283 new = x_temp[13] ^ x_temp[16] ^ x_temp[17] ^ x_temp[18] 284 x_temp = [new] + x_temp[:-1] 285 if y_temp[10] == majority: 286 new_one = y_temp[20] ^ y_temp[21] 287 y_temp = [new_one] + y_temp[:-1] 288 if z_temp[10] == majority: 289 new_two = z_temp[7] ^ z_temp[20] ^ z_temp[21] ^ z_temp[22] 290 z_temp = [new_two] + z_temp[:-1] 291 keystream.append(x_temp[18] ^ y_temp[21] ^ z_temp[22]) 292 return keystream 293 294 def to_str(binary): 295 s = "" 296 for i in range(0, len(binary) - 8, 8): 297 s += chr(int(binary[i:i + 8], 2)) 298 return str(s) 299 300 def encrypt(plain): 301 s = "" 302 binary = to_binary(plain) 303 keystream = get_keystream(len(binary)) 304 for i in range(len(binary)): 305 s += str(binary[i] ^ keystream[i]) 306 return s 307 308 def decrypt(cipher): 309 s = "" 310 binary = [] 311 keystream = get_keystream(len(cipher)) 312 for i in range(len(cipher)): 313 binary.append(int(cipher[i])) 314 s += str(binary[i] ^ keystream[i]) 315 return to_str(s) 316 317 def inp_key(): 318 tha_key = str(input('Enter a 64-bit key: ')) 319 while len(tha_key) != 64 or not re.match("^([01])+", tha_key): 320 tha_key = str(input('Enter a valid 64-bit key: ')) 321 return tha_key 322 323 def choice(): 324 inp = str(input('\n[1]: Implement A51 Cipher\n[2]: Exit\nOption: ')) 325 while inp not in ['1', '2']: 326 inp = str(input('\n[1]: Implement A51 Cipher\n[2]: Exit\nOption: ')) 327 return inp 328 329 def text(): 330 try: 331 inp = str(input('Enter the plaintext: ')) 332 except: 333 inp = str(input('Try again: ')) 334 return inp 335 336 def main(): 337 key = inp_key() 338 set_key(key) 339 first_choice = choice() 340 if first_choice == '1': 341 plaintext = text() 342 print("\nPlainText:", plaintext) 343 a = encrypt(plaintext) 344 print("\nEncrypted Text:", a) 345 print("\nDecrypted Text:", decrypt(a)) 346 elif first_choice == '2': 347 print("\nTerminal Closed") 348 sys.exit(0) 349 350 main() 351 352 # 1101011001010010110101110001100101101001001000110110110010110111