Advertisement
Guest User

Untitled

a guest
Jul 20th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import random
  2.  
  3. MAXLEN = 18
  4. ENGDICT = [[] for i in range(MAXLEN)]
  5. # https://raw.githubusercontent.com/dwyl/english-words/master/words.txt
  6. # encoder can use any other dictionary
  7. for line in open("words.txt"):
  8.     word = line.strip()
  9.     if len(word) < len(ENGDICT) and " " not in word:
  10.         ENGDICT[len(word)].append(word)
  11.  
  12.  
  13. def text_to_hex(text):
  14.     ans = []
  15.     for c in text:
  16.         ans.append(1 + (ord(c) // 16))
  17.         ans.append(1 + (ord(c) % 16))
  18.     return ans
  19.  
  20.  
  21. def hex_to_text(numbers):
  22.     assert len(numbers) % 2 == 0
  23.     ans = []
  24.     for i in range(0, len(numbers), 2):
  25.         ans.append((numbers[i] - 1) * 16 + numbers[i + 1] - 1)
  26.     return "".join(chr(i) for i in ans)
  27.  
  28.  
  29. def hex_to_code(numbers):
  30.     ans = []
  31.     for num in numbers:
  32.         ans.append(random.choice(ENGDICT[num]))
  33.     return " ".join(ans)
  34.  
  35.  
  36. def code_to_hex(words):
  37.     return [len(i) for i in words.split()]
  38.  
  39.  
  40. def antiai_encode(text):
  41.     numbers = text_to_hex(text)
  42.     return hex_to_code(numbers)
  43.  
  44.  
  45. def antiai_decode(code):
  46.     numbers = code_to_hex(code)
  47.     return hex_to_text(numbers)
  48.  
  49. s = "Hi, fellow human!"
  50. print("=== ORIGINAL TEXT ===")
  51. print(s)
  52. print()
  53. print("=== ENCODED TEXT ===")
  54. code = antiai_encode(s)
  55. print(code)
  56. print()
  57. s1 = antiai_decode(code)
  58. print("=== DECODED TEXT ===")
  59. print(s1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement