Skip to content

Instantly share code, notes, and snippets.

@wise-bit
Created July 26, 2021 01:11
Show Gist options
  • Select an option

  • Save wise-bit/19f31670e6ff335636018c44eb0a26b5 to your computer and use it in GitHub Desktop.

Select an option

Save wise-bit/19f31670e6ff335636018c44eb0a26b5 to your computer and use it in GitHub Desktop.
'''
Description
---------------------------------------
Substitution encryption algorithm to demonstrate the enigma cipher using
code-golf coding strategies to write the program.
Supports:
---------------------------------------
(i) Generating keys
(ii) Encryption
(iii) Decryption
Input format:
---------------------------------------
[ENCODE/DECODE]
[shift]
[key 1]
[key 2]
[key 3]
[original string]
'''
from random import*
i,l=input,range
def a(): # Runs the actual cipher algorithm, 355 characters, unindent for correct character count
def f(s,n,j):return chr((ord(s[0])-(65-n,39+n)[j])%26+65)+f(s[1:],n+1,j)if s!=""else""
p,o,n,r=[chr(z+65)for z in l(26)],i(),int(i()),[]
for _ in l(3):r.append(i())
m=i()
if o[0]=="E":
m=f(m,n,0)
for c in l(3):m="".join([r[c][p.index(q)]for q in m])
else:
for c in l(2,-1,-1):m="".join([p[r[c].index(q)]for q in m])
m=f(m,n,1)
print(m)
def c(): # Generates pseudo-random shift key and encryption keys
print(randrange(26)+1)
for _ in l(3):v=[chr(z+65)for z in range(26)];shuffle(v);print("".join(v))
a()if i()=="r"else c() # Function calls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment