Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Playfair cipher with little cheating solution in Uncategorized category for Playfair Cipher by capback250
# migrated from python 2.7
def encode(message, keyword):
fmess = [x.lower() for x in message if x.isdigit() or x.isalpha()]
if len(fmess) % 2:
hvost = 'z' if fmess[-1] == 'x' else 'z'
fmess.append(hvost)
return parser(c(fmess), table(keyword))
def table(keyword):
no_doubles = []
[no_doubles.append(x) for x in keyword if x not in no_doubles]
basic_alpha = 'abcdefghijklmnopqrstuvwxyz0123456789'
codestring = "{}{}".format(''.join(no_doubles), [x for x in basic_alpha if x not in no_doubles])
return [codestring[i:i+6] for i in range(0, 36, 6)]
def c(msg):
for i in range(0, len(msg)-1, 2):
if len(set(msg[i:i+2])) == 1:
pattrn = 'z' if msg[i] == 'x' else 'x'
msg.insert(i+1, pattrn)
if len(msg) % 2:
hvost = 'z' if msg[-1] == 'x' else 'z'
msg.append(hvost)
return msg
def parser(msg, tab):
indexes = {}
for key1, val1 in enumerate(tab):
for key2, val2 in enumerate(val1):
if val2 in msg:
indexes[val2] = [key1, key2]
coded = ''
print(msg, len(msg))
for i in range(0,len(msg),2):
a, b = indexes[msg[i]], indexes[msg[i+1]]
if a[0] == b[0]:
_i, _j = [x for x in [x if x != 5 else -1 for x in [a[1], b[1]]]]
coded += "{}{}".format(tab[a[0]][_i+1], tab[a[0]][_j+1])
elif a[1] == b[1]:
_i, _j = [x for x in [x if x != 5 else -1 for x in [a[0], b[0]]]]
coded += "{}{}".format(tab[_i+1][a[1]], tab[_j+1][a[1]])
else:
coded += "{}{}".format(tab[a[0]][b[1]], tab[b[0]][a[1]])
return coded if coded[-2:] !='00' else coded[:-2]
def decode(secretMessage, code):
tab = table(code)
indexes = {}
for key1, val1 in enumerate(tab):
for key2, val2 in enumerate(val1):
if val2 in secretMessage:
indexes[val2] = [key1, key2]
coded = ''
if len(secretMessage) % 2:
hvost = 'z' if secretMessage[-1] == 'x' else 'z'
secretMessage.append(hvost)
for i in range(0,len(secretMessage)-1, 2):
a, b = indexes[secretMessage[i]], indexes[secretMessage[i+1]]
if a[0] == b[0]:
coded += "{}{}".format(tab[a[0]][a[1]-1], tab[a[0]][b[1]-1])
elif a[1] == b[1]:
coded += "{}{}".format(tab[a[0]-1][a[1]], tab[b[0]-1][a[1]])
else:
coded += "{}{}".format(tab[a[0]][b[1]], tab[b[0]][a[1]])
return coded
Dec. 29, 2015