12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- class DH_Endpoint(object):
- def __init__(self, public_key1, public_key2, private_key):
- self.public_key1 = public_key1
- self.public_key2 = public_key2
- self.private_key = private_key
- self.full_key = None
-
- def generate_partial_key(self):
- partial_key = self.public_key1**self.private_key
- partial_key = partial_key%self.public_key2
- return partial_key
-
- def generate_full_key(self, partial_key_r):
- full_key = partial_key_r**self.private_key
- full_key = full_key%self.public_key2
- self.full_key = full_key
- return full_key
-
- def encrypt_message(self, message):
- encrypted_message = ""
- key = self.full_key
- for c in message:
- encrypted_message += chr(ord(c)+key)
- return encrypted_message
-
- def decrypt_message(self, encrypted_message):
- decrypted_message = ""
- key = self.full_key
- for c in encrypted_message:
- decrypted_message += chr(ord(c)-key)
- return decrypted_message
- def generate_partial_key(self):
- partial_key = self.public_key1**self.private_key
- partial_key = partial_key%self.public_key2
- return partial_key
- def generate_full_key(self, partial_key_r):
- full_key = partial_key_r**self.private_key
- full_key = full_key%self.public_key2
- self.full_key = full_key
- return full_key
- def encrypt_message(self, message):
- encrypted_message = ""
- key = self.full_key
- for c in message:
- encrypted_message += chr(ord(c)+key)
- return encrypted_message
- def decrypt_message(self, encrypted_message):
- decrypted_message = ""
- key = self.full_key
- for c in encrypted_message:
- decrypted_message += chr(ord(c)-key)
- return decrypted_message
- message="Hello, World!"
- a_public=197
- a_private=199
- b_public=151
- b_private=157
- Alex = DH_Endpoint(a_public, b_public, a_private)
- Elya = DH_Endpoint(a_public, b_public, b_private)
- a_partial=Alex.generate_partial_key()
- print(a_partial)
- b_partial=Elya.generate_partial_key()
- print(b_partial)
- a_full=Alex.generate_full_key(b_partial)
- print(a_full)
- b_full=Elya.generate_full_key(a_partial)
- print(b_full)
- b_encrypted=Elya.encrypt_message(message)
- print("Encrypted message: ",b_encrypted)
- message = Alex.decrypt_message(b_encrypted)
- print("Decrypted message:",message)
|