Skip to content

Instantly share code, notes, and snippets.

@doraneko94
Last active July 4, 2018 07:48
Show Gist options
  • Save doraneko94/eb0d727881143048118b0b53194936ce to your computer and use it in GitHub Desktop.
Save doraneko94/eb0d727881143048118b0b53194936ce to your computer and use it in GitHub Desktop.
The code of "Enigma".
import random
import sys
alpha = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
hand1 = 22 #Change
hand2 = 5 #Change
rot_state1 = 0 #Change
rot_state2 = 1 #Change
rot_state3 = 2 #Change
rot_state0 = 3 #Change
chr1 = 23 #Change
chr2 = 3 #Change
chr3 = 7 #Change
random.seed(rot_state1)
alpha1 = random.sample(alpha, len(alpha))
random.seed(rot_state2)
alpha2 = random.sample(alpha, len(alpha))
random.seed(rot_state3)
alpha3 = random.sample(alpha, len(alpha))
random.seed(rot_state0)
alpha0 = random.sample(alpha, len(alpha))
if len(sys.argv) < 3:
print("USEAGES:")
print("enigma.py pack/unpack word(Capital Letters)")
exit
target = sys.argv[2]
target = target.upper()
out = []
if sys.argv[1] == "pack":
for i in target:
chr1 -= 1
if chr1 < 0:
chr1 = 25
if chr1 == hand1:
chr2 -= 1
if chr2 < 0:
chr2 = 26
if chr2 == hand2:
chr3 -= 1
if chr3 < 0:
chr3 = 26
now = chr1 + alpha.index(i)
if now > 25:
now -= 26
nexti = alpha1[now]
now = alpha.index(nexti) - chr1
if now < 0:
now += 26
now = chr2 + now
if now > 25:
now -= 26
nexti = alpha2[now]
now = alpha.index(nexti) - chr2
if now < 0:
now += 26
now = chr3 + now
if now > 25:
now -= 26
nexti = alpha3[now]
now = alpha.index(nexti) - chr3
if now < 0:
now += 26
nexti = alpha0[now]
now = alpha.index(nexti)
now = chr3 + now
if now > 25:
now -= 26
nexti = alpha[alpha3.index(alpha[now])]
now = alpha.index(nexti) - chr3
if now < 0:
now += 26
now = chr2 + now
if now > 25:
now -= 26
nexti = alpha[alpha2.index(alpha[now])]
now = alpha.index(nexti) - chr2
if now < 0:
now += 26
now = chr1 + now
if now > 25:
now -= 26
nexti = alpha[alpha1.index(alpha[now])]
now = alpha.index(nexti) - chr1
if now < 0:
now += 26
out.append(alpha[now])
print("A:"+"".join(out))
if sys.argv[1] == "unpack":
for _ in target:
chr1 -= 1
if chr1 < 0:
chr1 = 25
if chr1 == hand1:
chr2 -= 1
if chr2 < 0:
chr2 = 25
if chr2 == hand2:
chr3 -= 1
if chr3 < 0:
chr3 = 25
for i in target[::-1]:
now = chr1 + alpha.index(i)
if now > 25:
now -= 26
nexti = alpha1[now]
now = alpha.index(nexti) - chr1
if now < 0:
now += 26
now = chr2 + now
if now > 25:
now -= 26
nexti = alpha2[now]
now = alpha.index(nexti) - chr2
if now < 0:
now += 26
now = chr3 + now
if now > 25:
now -= 26
nexti = alpha3[now]
now = alpha.index(nexti) - chr3
if now < 0:
now += 26
nexti = alpha[now]
now = alpha0.index(nexti)
now = chr3 + now
if now > 25:
now -= 26
nexti = alpha[alpha3.index(alpha[now])]
now = alpha.index(nexti) - chr3
if now < 0:
now += 26
now = chr2 + now
if now > 25:
now -= 26
nexti = alpha[alpha2.index(alpha[now])]
now = alpha.index(nexti) - chr2
if now < 0:
now += 26
now = chr1 + now
if now > 25:
now -= 26
nexti = alpha[alpha1.index(alpha[now])]
now = alpha.index(nexti) - chr1
if now < 0:
now += 26
out.append(alpha[now])
chr1 += 1
if chr1 > 25:
chr1 = 0
if hand1 == 25:
if chr1 == 0:
chr2 += 1
elif chr1 == hand1 + 1:
chr2 += 1
if chr2 > 25:
chr2 = 0
if hand1 == 25:
if chr2 == 0:
chr3 += 1
elif chr2 == hand2 + 1:
chr3 += 1
if chr3 > 25:
chr3 = 0
print("A:"+"".join(out[::-1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment