Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. #ile: BST_Cipher.py
  2.  
  3. # Description: BST Cipher
  4.  
  5. # Student Name: Sungho Park
  6.  
  7. # Student UT EID: sp44443
  8.  
  9. # Partner's Name: Ohyoon Kwon
  10.  
  11. # Partner's UT EID: ok2364
  12.  
  13. # Course Name: CS 313E
  14.  
  15. # Unique Number: 51345
  16.  
  17. # Date Created: NOV/12/2018
  18.  
  19. # Date Last Modified: NOV/12/2018
  20.  
  21.  
  22. class Node(object):
  23. def __init__(self, data):
  24. self.data = data
  25. self.lchild = None
  26. self.rchild = None
  27.  
  28. class Tree (object):
  29. # the init() function creates the binary search tree with the
  30. # encryption string. If the encryption string contains any
  31. # character other than the characters 'a' through 'z' or the
  32. # space character drop that character.
  33. def __init__ (self, encrypt_str):
  34. self.root = None
  35. self.extract(encrypt_str)
  36.  
  37. # the insert() function adds a node containing a character in
  38. # the binary search tree. If the character already exists, it
  39. # does not add that character. There are no duplicate characters
  40. # in the binary search tree.
  41. def insert(self, ch):
  42. new_node = Node(ch)
  43. asc = ord(ch)
  44. if (self.root == None):
  45. self.root = new_node
  46. else:
  47. current = self.root
  48. parent = self.root
  49. while (current != None):
  50. parent = current
  51. if (asc < ord(current.data)):
  52. current = current.lchild
  53. else:
  54. current = current.rchild
  55.  
  56. if (asc < ord(parent.data)):
  57. parent.lchild = new_node
  58. elif (asc == ord(parent.data)):
  59. pass
  60. else:
  61. parent.rchild = new_node
  62.  
  63.  
  64. # the search() function will search for a character in the binary
  65. # search tree and return a string containing a series of lefts
  66. # (<) and rights (>) needed to reach that character. It will
  67. # return a blank string if the character does not exist in the tree.
  68. # It will return * if the character is the root of the tree.
  69.  
  70. def search (self, ch):
  71. current = self.root
  72. string = ""
  73. if ord(ch) == ord(self.root.data):
  74. string += "*"
  75. if current == None:
  76. return ""
  77. for i in range(len(ch)):
  78. while (current != None) and (ord(current.data) != ord(ch)):
  79. if ord(ch) < ord(current.data):
  80. string += "<"
  81. current = current.lchild
  82. else:
  83. string += ">"
  84. current = current.rchild
  85. return string
  86.  
  87. # the traverse() function will take string composed of a series of
  88. # lefts (<) and rights (>) and return the corresponding
  89. # character in the binary search tree. It will return an empty string
  90. # if the input parameter does not lead to a valid character in the tree.
  91. def traverse (self, st):
  92. current = self.root
  93. string = ""
  94. if st == "":
  95. return string
  96. if current == None:
  97. return string
  98. for i in st:
  99. if i == ">":
  100. current = current.rchild
  101. elif i == "<":
  102. current = current.lchild
  103. elif i == "*":
  104. string += current.data
  105. return string
  106. return current.data
  107.  
  108.  
  109. # the encrypt() function will take a string as input parameter, convert
  110. # it to lower case, and return the encrypted string. It will ignore
  111. # all digits, punctuation marks, and special characters.
  112. def encrypt (self, st):
  113. st = st.lower()
  114. string = ""
  115. count = 0
  116.  
  117. for i in st:
  118. a = self.search(i)
  119. string += a
  120. count += 1
  121. if (count < len(st)):
  122. string += "!"
  123. return string
  124.  
  125.  
  126. # the decrypt() function will take a string as input parameter, and
  127. # return the decrypted string.
  128. def decrypt (self, st):
  129. string1 = ""
  130. string2 = ""
  131.  
  132. for i in st:
  133. if i == "!":
  134. string2 += self.traverse(string1)
  135. string1 = ""
  136. else:
  137. string1 += i
  138. string2 += self.traverse(string1)
  139. return string2
  140.  
  141. #extracting received string and sending each character to the insert function
  142. def extract(self, str):
  143. templist = []
  144. str = str.lower()
  145. for i in str:
  146. if i.isalpha() or i == " ":
  147. if i not in templist:
  148. templist.append(i)
  149. for i in templist:
  150. self.insert(i)
  151.  
  152. def main():
  153. str = input("Enter the encryption key: ")
  154. newstr = ''
  155. for ch in str:
  156. if (ch == " " or ch.isalpha()):
  157. newstr = newstr + ch
  158.  
  159. tree1 = Tree(newstr)
  160. print()
  161. inp1 = input("Enter string to be encrypted: ")
  162. newstr1 = ''
  163. for ch in inp1:
  164. if (ch == " " or ch.isalpha()):
  165. newstr1 = newstr1 + ch
  166. print("Encrypted String:", tree1.encrypt(newstr1))
  167. print()
  168. inp2 = input("Enter string to be decrypted: ")
  169. print("Decrypted String:", tree1.decrypt(inp2))
  170. print()
  171.  
  172. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement