Naitsab_33

Text Encoder Decoder

Jul 1st, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. Encoder:
  2.  
  3. import random
  4.  
  5. text = input("Please input a text: ")
  6. text = list(text)
  7. print(text)
  8. l = (text.__len__()) * random.randint(10000, 20000) #random-generated characters for each input-text character
  9. print(l)
  10. dec1 = list()
  11. code = list()
  12. i = 0
  13. while i < l:
  14.     dec1.append(random.randchar())
  15.     i = i + 1
  16. print(dec1)
  17. dec = list()
  18. filename = input("Please choose a file name (without file-extension): ")
  19. filedec = open(filename + ".dec", "w")
  20. for i in dec1:
  21.     dec.append(str(i))
  22. pos1 = 0
  23.  
  24. for i in text:
  25.     pos1 = random.randint(0, l)
  26.     for i2 in dec:
  27.         while str(pos1) in i2:
  28.             pos1 = random.randint(0, l)
  29.     dec[pos1] = i
  30.  
  31.     code.append(pos1)
  32.  
  33. decstr = ""
  34. for i in dec:
  35.     decstr = decstr + str(i)
  36. filedec.write(decstr)
  37. filecode = open("Decipher.txt", "w")
  38. filecode.write(str(code))
  39.  
  40.  
  41.  
  42. Decoder:
  43.  
  44. from ast import literal_eval
  45. datei = input("choose a file to decode (with file-extension): ")
  46. decipher = literal_eval(input("input the decipher.txt: "))
  47. filedec = open(datei, "r")
  48. dec = list(filedec.read())
  49.  
  50. text = ""
  51. for i in decipher:
  52.     text = text+dec[i]
  53.  
  54. print(text)
Advertisement
Add Comment
Please, Sign In to add comment