O’zbekiston Respublikasi Axborot Texnologiyalari va Kommunikatsiyalarini Rivojlantirish Vazirligi Muhammad al-Xorazmiy nomidagi Toshkent Axborot Texnologiyalari Universiteti Mustaqil ish Mavzu




Download 16,06 Kb.
bet2/3
Sana15.12.2023
Hajmi16,06 Kb.
#119267
1   2   3
Bog'liq
1-fayllar.org

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 16,06 Kb.
1   2   3




Download 16,06 Kb.

Bosh sahifa
Aloqalar

    Bosh sahifa



O’zbekiston Respublikasi Axborot Texnologiyalari va Kommunikatsiyalarini Rivojlantirish Vazirligi Muhammad al-Xorazmiy nomidagi Toshkent Axborot Texnologiyalari Universiteti Mustaqil ish Mavzu

Download 16,06 Kb.