Skip to content

Instantly share code, notes, and snippets.

@moonexpr
Last active September 24, 2022 04:52
Show Gist options
  • Select an option

  • Save moonexpr/88f62a9df26e4e1480c3d57f0ef8dd9b to your computer and use it in GitHub Desktop.

Select an option

Save moonexpr/88f62a9df26e4e1480c3d57f0ef8dd9b to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import random
import sys
class Ciper:
def __init__(self):
self.numbers = [1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, 103, 109, 129, 130]
def generateNoToken(self, string):
token = string[random.randint(0, len(string) - 1)]
return self.generate(string, token)
def clampAndModuate(self, n, min, max):
range = max - min
return (abs(n - min) % range) + min
def generate(self, string, token, omitKey=False):
first = 0
shift = ord(token)
if shift <= ord('Z'):
first = ord('A')
else:
first = ord('a')
out = ''
for i, c in enumerate(string):
alphaIndex = ord(c) - first - 1
n = self.numbers[0 + (shift + alphaIndex) % 24]
if i % (shift - first + 1) == 0:
for j in range(alphaIndex):
out += chr(ord('a') + n)
else:
out += str(n)
out += "h=, %c" % (chr(shift)) if not omitKey else "h="
return out
class InteractiveMode:
def __init__(self):
self.c = Ciper()
self.bStop = False
def stop(self):
sys.exit(0)
def start(self):
while True:
self.invokeStep()
def invokeStep(self):
print('> ', end='')
string = input()
if string == 'q' or string == 'Q':
self.stop()
if(len(string) == 0):
return
chunks = string.split(' ')
if len(chunks) >= 2:
print(self.c.generate(chunks[0], chunks[1]))
else:
print(self.c.generateNoToken(string))
def abort():
print('E: bad input')
# Why the HELL is there no argc??
argc = len(sys.argv)
if argc == 1:
InteractiveMode().start()
elif argc == 2:
print(Ciper().generateNoToken(sys.argv[1]))
elif argc == 3:
print(Ciper().generate(sys.argv[1],sys.argv[2], True))
else:
abort()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment