Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. import random
  2. import numpy as np
  3. import math
  4.  
  5. alphabet = 'abcdefghijklmnopqrstuvwxyz '
  6.  
  7.  
  8. def func_c(i):
  9. return alphabet.index(i)
  10.  
  11.  
  12. def alphabet_coded():
  13. res = []
  14. for i in alphabet:
  15. res.append(func_c(i))
  16. return res
  17.  
  18.  
  19. coded_alphabet = alphabet_coded()
  20.  
  21.  
  22. def generate_key(num):
  23. res = []
  24. result = []
  25. for i in range(3):
  26. res.append(random.choice(range(len(alphabet)+1)))
  27. for i in range(3, num + 1):
  28. res.append((res[i - 1] + res[i - 3]) % len(alphabet))
  29. for i in range(len(res) - 1):
  30. result.append((res[i] + res[i + 1]) % len(alphabet))
  31. return result
  32.  
  33.  
  34. def cypher(message, key):
  35. res = []
  36. # key = generate_key(len(message))
  37. for i in range(len(message)):
  38. res.append((func_c(message[i]) + key[i]) % len(alphabet))
  39. print(message)
  40. print(key)
  41. # print(res)
  42. return res
  43.  
  44.  
  45. def decypher(coded_message, key):
  46. res = ''
  47. for i in range(len(coded_message)):
  48. res += alphabet[(coded_message[i] + len(alphabet) - key[i]) % len(alphabet)]
  49. return res
  50.  
  51.  
  52. def main():
  53. # message = input('Enter the message: ')
  54. f = open('Input.txt')
  55. message = f.read()
  56. # print(message)
  57. # print(alphabet)
  58. for i in alphabet:
  59. print(i, end = " ")
  60. print()
  61. print(coded_alphabet)
  62. key = generate_key(len(message))
  63. coded = cypher(message, key)
  64. print(coded)
  65. decoded = decypher(coded, key)
  66. print(decoded)
  67.  
  68.  
  69. if __name__ == '__main__':
  70. main()
  71.  
  72.  
  73.  
  74. alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIKLMNOPQRSTUVWXYZ .,!:;*'"
  75. key_size = 0
  76. def encrypt(text,keyword):
  77. keyword_matrix = keyword_to_matrix(keyword)
  78. keyword_matrix = np.array(keyword_matrix)
  79. text_matrix = np.array(text_to_matrix(text))
  80.  
  81. result = [] # final result
  82. for i in range(len(text_matrix)):
  83.  
  84. row = [] # the new row in new matrix
  85. for j in range(len(keyword_matrix)):
  86.  
  87. product = 0 # the new element in the new row
  88. for v in range(len(text_matrix[i])):
  89. product += (text_matrix[i][v] * keyword_matrix[v][j])
  90. row.append(product ) # append sum of product into the new row
  91.  
  92. result.append(row) # append the new row into the final result
  93. result= np.array(result)
  94. result %=len(alphabet)
  95. print(keyword_matrix,'\n')
  96. print(text_matrix,'\n')
  97. print(result,'\n')
  98. f = open("out.txt",'w')
  99. for i in result:
  100. for j in i:
  101. f.write(alphabet[j])
  102. f.close()
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. #print(result)
  110.  
  111. def decode(keyword):
  112. keyword_matrix = keyword_to_matrix(keyword)
  113. keyword_matrix = np.array(keyword_matrix)
  114. text = read_file("out.txt")
  115. text_matrix = np.array(text_to_matrix(text))
  116. keyword_matrix_inv = np.linalg.inv(keyword_matrix)
  117. # print(np.matmul(keyword_matrix, keyword_matrix_inv))
  118. #result = np.matmul(text_matrix,keyword_matrix_inv)
  119.  
  120. result = [] # final result
  121. for i in range(len(text_matrix)):
  122.  
  123. row = [] # the new row in new matrix
  124. for j in range(len(keyword_matrix_inv)):
  125.  
  126. product = 0 # the new element in the new row
  127. for v in range(len(text_matrix[i])):
  128. product += (text_matrix[i][v] * keyword_matrix_inv[v][j]) % len(alphabet)
  129. row.append(product % len(alphabet)) # append sum of product into the new row
  130.  
  131. result.append(row)
  132.  
  133. print(np.linalg.det(keyword_matrix))
  134. print(text_matrix,'\n')
  135. print(keyword_matrix, '\n')
  136. print(keyword_matrix_inv,'\n')
  137. print(result,'\n')
  138.  
  139.  
  140. #print(np.matmul(keyword_matrix,keyword_matrix_inv))
  141. #print(len(alphabet))
  142.  
  143. #result = math.floor(result)
  144.  
  145. f = open("result.txt",'w')
  146.  
  147.  
  148. result = np.floor(result)
  149. print(result, '\n')
  150. for i in result:
  151. for j in i:
  152. f.write(alphabet[int(j)])
  153.  
  154.  
  155.  
  156. def text_to_matrix(text):
  157. size = len(text)
  158. matrix = []
  159. row = []
  160. count = 0
  161. for i in range(size//key_size):
  162. for j in range(key_size):
  163. row.append(alphabet.index(text[count]))
  164. count += 1
  165. matrix.append(row)
  166. row = []
  167. return matrix
  168.  
  169. def keyword_to_matrix(keyword):
  170. global key_size
  171. key_size = int(len(keyword) ** (1 / 2))
  172. key_matrix = []
  173. row = []
  174. count = 0
  175. for i in range(key_size):
  176. for j in range(key_size):
  177. row.append(alphabet.index(keyword[count]))
  178. count += 1
  179. key_matrix.append(row)
  180. row = []
  181. return key_matrix
  182.  
  183. def read_file(path):
  184. f = open(path, "r")
  185. matrix = []
  186. text = ''
  187. lines = f.readlines()
  188. for i in lines:
  189. text += i
  190. text = text.replace('\n', ' ')
  191. return text
  192.  
  193. def main():
  194. keyword = "qwertyuio"
  195. text = read_file('text.txt')
  196. encrypt(text,keyword)
  197. decode(keyword)
  198.  
  199. if __name__=="__main__":
  200. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement