Skip to content

Instantly share code, notes, and snippets.

@vzool
Last active February 13, 2019 16:27
Show Gist options
  • Save vzool/035f1fa81b52149e19d56df1e9a9deff to your computer and use it in GitHub Desktop.
Save vzool/035f1fa81b52149e19d56df1e9a9deff to your computer and use it in GitHub Desktop.
import base64
import random as r
MAX_VALUE = 256 * 9
private_key = int(r.random() * MAX_VALUE)
public_key = MAX_VALUE - private_key
shared_key = MAX_VALUE
print("Public Key: %d" % public_key)
print("Private Key: %d" % private_key)
print("Shared Key: %d" % shared_key)
original_text = "Salam, World - سلام يا عالم"
print("Original: %s" % original_text)
################ Encrypted ################
buffer = []
for c in list(original_text):
buffer.append(chr((ord(c) + public_key) % shared_key))
encrypted = "".join(map(str, buffer));
encoded = base64.urlsafe_b64encode(encrypted.encode("utf-8"))
print("Encrypted: %s" % encoded)
################ Decrypted ################
buffer = []
decoded = base64.urlsafe_b64decode(encoded)
encrypted = decoded.decode("utf-8")
for c in list(encrypted):
buffer.append(chr((ord(c) + private_key) % shared_key))
decrypted = "".join(map(str, buffer));
print("Decrypted: %s" % decrypted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment