Skip to content

Instantly share code, notes, and snippets.

@en0
Created February 26, 2017 06:38
Show Gist options
  • Select an option

  • Save en0/53d4bdb24d74d4d6d310bf3ed8268d02 to your computer and use it in GitHub Desktop.

Select an option

Save en0/53d4bdb24d74d4d6d310bf3ed8268d02 to your computer and use it in GitHub Desktop.
from binascii import a2b_base64 as b64decode, b2a_base64 as b64encode
a, b = 13, 7
PUB = 5
PRIV = 29
MAX = a * b
def cycleBit(v, c, m):
v2 = v
for i in range(c - 1):
v2 = (v2 * v) % m
return v2
def crypt(s, k, m):
ret = []
for v in map(ord, s):
ret.append(cycleBit(v, k, m))
return "".join(map(chr, ret))
def encrypt(message, key, m):
b = crypt(message, key, m)
return b64encode(b)
def decrypt(message, key, m):
s = b64decode(message)
return crypt(s, key, m)
MySecret = "HELLO, WORLD"
print "Encrypting message: " + MySecret
c = encrypt(MySecret, PUB, MAX)
print "Encrypted message is: " + c
m = decrypt(c, PRIV, MAX)
print "Decrypted message is: " + m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment