Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. # -*- coding: cp1252 -*-
  2. from random import shuffle,randint,choice
  3. from copy import copy
  4. alphabet=range(0,26)
  5.  
  6. def shift(l, n): # Method to rotate arrays/cogs
  7. return l[n:] + l[:n]
  8.  
  9. class cog: # Simple substitution cipher for each cog
  10. def create(self):
  11. self.transformation=copy(alphabet)
  12. shuffle(self.transformation)
  13. return
  14. def passthrough(self,i):
  15. return self.transformation[i]
  16. def passthroughrev(self,i):
  17. return self.transformation.index(i)
  18. def rotate(self):
  19. self.transformation=shift(self.transformation, 1)
  20. def setcog(self,a):
  21. self.transformation=a
  22.  
  23. class enigma: # Enigma class
  24. def __init__(self, nocogs,printspecialchars):
  25. self.printspecialchars=printspecialchars
  26. self.nocogs=nocogs
  27. self.cogs=[]
  28. self.oCogs=[] # Create backup of original cog positions for reset
  29.  
  30. for i in range(0,self.nocogs): # Create cogs
  31. self.cogs.append(cog())
  32. self.cogs[i].create()
  33. self.oCogs.append(self.cogs[i].transformation)
  34.  
  35. # Create reflector
  36. refabet=copy(alphabet)
  37. self.reflector=copy(alphabet)
  38. while len(refabet)>0:
  39. a=choice(refabet)
  40. refabet.remove(a)
  41. b=choice(refabet)
  42. refabet.remove(b)
  43. self.reflector[a]=b
  44. self.reflector[b]=a
  45.  
  46. def print_setup(self): # To print the enigma setup for debugging/replication
  47. print "Enigma Setup:\nCogs: ",self.nocogs,"\nCog arrangement:"
  48. for i in range(0,self.nocogs):
  49. print self.cogs[i].transformation
  50. print "Reflector arrangement:\n",self.reflector,"\n"
  51.  
  52. def reset(self):
  53. for i in range(0,self.nocogs):
  54. self.cogs[i].setcog(self.oCogs[i])
  55.  
  56. def encode(self,text):
  57. ln=0
  58. ciphertext=""
  59. for l in text.lower():
  60. num=ord(l)%97
  61. if (num>25 or num<0):
  62. if (self.printspecialchars): # readability
  63. ciphertext+=l
  64. else:
  65. pass # security
  66. else:
  67. ln+=1
  68. for i in range(0,self.nocogs): # Move thru cogs forward...
  69. num=self.cogs[i].passthrough(num)
  70.  
  71. num=self.reflector[num] # Pass thru reflector
  72.  
  73. for i in range(0,self.nocogs): # Move back thru cogs...
  74. num=self.cogs[self.nocogs-i-1].passthroughrev(num)
  75. ciphertext+=""+chr(97+num) # add encrypted letter to ciphertext
  76.  
  77. for i in range(0,self.nocogs): # Rotate cogs...
  78. if ( ln % ((i*6)+1) == 0 ): # in a ticker clock style
  79. self.cogs[i].rotate()
  80. return ciphertext
  81.  
  82. plaintext=raw_input("What do you wanna encrypt ? ")
  83.  
  84. x=enigma(4,True)
  85. #x.print_setup()
  86.  
  87. print "Plaintext:\n"+plaintext+"\n"
  88. ciphertext=x.encode(plaintext)
  89. print "Ciphertext:\n"+ciphertext+"\n"
  90.  
  91. # To proove that encoding and decoding are symmetrical
  92. # we reset the enigma to starting conditions and enter
  93. # the ciphertext, and get out the plaintext
  94. x.reset()
  95. ciphertext2 = raw_input("What do you wanna decode ? ")
  96. decodeplaintext=x.encode(ciphertext2)
  97. print "Plaintext:\n"+decodeplaintext+"\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement