Skip to content

Instantly share code, notes, and snippets.

@darksinge
Created September 26, 2018 02:19
Show Gist options
  • Save darksinge/e8b8a630620e299fa1daa0329dd9aad5 to your computer and use it in GitHub Desktop.
Save darksinge/e8b8a630620e299fa1daa0329dd9aad5 to your computer and use it in GitHub Desktop.
Simple Encryption
encode_table = {
'a': "01",
'b': "02",
'c': "03",
'd': "04",
'e': "05",
'f': "06",
'g': "07",
'h': "08",
'i': "09",
'j': "10",
'k': "11",
'l': "12",
'm': "13",
'n': "14",
'o': "15",
'p': "16",
'q': "17",
'r': "18",
's': "19",
't': "20",
'u': "21",
'v': "22",
'w': "23",
'x': "24",
'y': "25",
'z': "26"
}
def gcd(a, b):
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).
"""
while b:
a, b = b, a % b
return a
def is_prime(n):
d = n - 1
s = 0
while not d & 1:
s += 1
d >>= 1
for a in (2, 13, 23, 1662803):
if pow(a, d, n) != 1 and all(pow(a, (1 << r) * d, n) != n - 1 for r in range(0, s)):
return False
return True
def encode_message(msg): # type: (str) -> int
encoded = ''
for i in msg:
encoded += encode_table[i]
return int(encoded)
def decode_message(msg): # type: (int|str) -> str
if isinstance(msg, int):
msg = str(msg)
decoded = ''
for i in range(0, len(msg), 2):
value = msg[i:i+2]
key = ''
for key_, value_ in encode_table.items():
if value == value_:
key = key_
break
if len(key) == 0:
continue
decoded += key
return decoded
def encrypt(msg, key):
msg = int(msg)
msg = msg * pow(10, 2)
while not is_prime(msg):
msg += 1
return msg * key
if __name__ == '__main__':
secret = "062114032009151449"
message = decode_message(secret)
print("The secret message is: %s" % message)
key = 131
msg1 = "ilovejenniwithallmyheart"
secret1 = encode_message(msg1)
encoded1 = encrypt(secret1, key)
print("The encoded message is: %d" % encoded1)
msg2 = "ilikecodingbecauseitisuseful"
secret2 = encode_message(msg2)
encoded2 = encrypt(secret2, key)
print("The encoded message is: %d" % encoded2)
divisor = gcd(encoded1, encoded2)
print("The secret key was found to be: %d" % divisor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment