1. Oqimni shifrlash algoritmini amalga olish a5/1


A5/1 pythonda dastur kodi


Download 76.27 Kb.
bet2/3
Sana18.06.2023
Hajmi76.27 Kb.
#1556280
1   2   3
Bog'liq
5-var(M-ish)

A5/1 pythonda dastur kodi:
class A51:
def __init__(self, key, iv):
self.key = key
self.iv = iv
self.register1 = [0] * 19
self.register2 = [0] * 22
self.register3 = [0] * 23

def clock_register(self, register, mask, feedback):


new_bit = register[0] ^ feedback
register = register[1:] + [new_bit]
return register, register[mask[0]] ^ register[mask[1]] ^ register[mask[2]] ^ register[mask[3]]

def clock_registers(self):


majority = sum([self.register1[8], self.register2[10], self.register3[10]]) >= 2
if len(self.key) > 0:
self.register1, _ = self.clock_register(self.register1, [13, 16, 17, 18], self.register1[8] ^ self.key[0])
self.register2, _ = self.clock_register(self.register2, [20, 21, 22, 7], self.register2[10] ^ self.key[0])
self.register3, _ = self.clock_register(self.register3, [7, 20, 21, 22], self.register3[10] ^ self.key[0])
self.key = self.key[1:]
if len(self.iv) > 0:
self.register1, _ = self.clock_register(self.register1, [13, 16, 17, 18], self.register1[8] ^ self.iv[0])
self.register2, _ = self.clock_register(self.register2, [20, 21, 22, 7], self.register2[10] ^ self.iv[0])
self.register3, _ = self.clock_register(self.register3, [7, 20, 21, 22], self.register3[10] ^ self.iv[0])
self.iv = self.iv[1:]
if majority:
output_bit = self.register1[18] ^ self.register2[21] ^ self.register3[22]
return output_bit
else:
return None

def generate_keystream(self, length):


keystream = ""
while len(keystream) < length:
bit = self.clock_registers()
if bit is not None:
keystream += str(bit)
return keystream
def encrypt(self, plaintext):
keystream = self.generate_keystream(len(plaintext))
ciphertext = "".join(str(int(plaintext[i]) ^ int(keystream[i])) for i in range(len(plaintext)))
return ciphertext

def decrypt(self, ciphertext):


keystream = self.generate_keystream(len(ciphertext))
plaintext = "".join(str(int(ciphertext[i]) ^ int(keystream[i])) for i in range(len(ciphertext)))
return plaintext

Download 76.27 Kb.

Do'stlaringiz bilan baham:
1   2   3




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling