Last active
February 13, 2019 16:27
-
-
Save vzool/035f1fa81b52149e19d56df1e9a9deff to your computer and use it in GitHub Desktop.
How to make a simple public-key cryptographic algorithm? https://stackoverflow.com/questions/19017419/how-to-make-a-simple-public-key-cryptographic-algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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