Skip to content

Instantly share code, notes, and snippets.

@blewert
Created July 29, 2020 00:44
Show Gist options
  • Save blewert/0aa22d1eb5a6c11e671ddb87a29e8380 to your computer and use it in GitHub Desktop.
Save blewert/0aa22d1eb5a6c11e671ddb87a29e8380 to your computer and use it in GitHub Desktop.
from math import sqrt;
import random
import string
s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
keyset = ''.join(random.sample(s,len(s)));
cleartext = "wepogkwepokgwep jgwokegpo wkepgo kwepogkwpoekgpowekgpowekgpowekgpokweg wekgpowegpokweg".upper();
delchars = ''.join(c for c in map(chr, range(256)) if not c.isalnum());
cleartext = cleartext.translate(str.maketrans('','',string.punctuation));
cleartext = cleartext.translate(str.maketrans('','','1234567890'));
# grid_width = int(sqrt(len(cleartext)) * 1.0);
grid_width = len(s);
rows = int(len(cleartext) / grid_width);
def rotn(s,n):
result = ""
# Loop over characters.
for v in s:
# Convert to number with ord.
c = ord(v)
# Shift number back or forward.
if c >= ord('a') and c <= ord('z'):
if c > ord('m'):
c -= n
else:
c += n
elif c >= ord('A') and c <= ord('Z'):
if c > ord('M'):
c -= n
else:
c += n
# Append to result.
result += chr(c)
# Return transformation.
return result
def cmap(v):
if not (ord(v) >= ord('A') and ord(v) <= ord('Z')):
return v;
return keyset[ord(v) - ord('A')];
def cipher(input, n):
#rotate
v = rotn(input, n).upper();
#map
return cmap(v);
for i in range(0, rows+1):
s = "";
for j in range(0, grid_width):
p = j + grid_width * i;
if p >= len(cleartext):
continue;
s += cipher(cleartext[p], 1 + ((p*2)%16)) + " ";
print(s);
print(" ");
print(''.join([(c + " ") for c in keyset]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment