Hrispens

Вычислительные системы и компьютерные сети

Feb 22nd, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import base64
  2. import math
  3.  
  4. def isPrime(n):
  5. d = 2
  6. while n % d != 0:
  7. d += 1
  8. return d == n
  9. #
  10.  
  11.  
  12.  
  13. print("print P")
  14. while True:
  15. p = int(input())
  16. if isPrime(p):
  17. print("P is prime")
  18.  
  19. break
  20. else:
  21. print("P is not prime")
  22.  
  23. print("print Q")
  24. while True:
  25. q = int(input())
  26. if isPrime(q):
  27. print("Q is prime")
  28. break
  29. else:
  30. print("Q is not prime")
  31.  
  32.  
  33. n=p*q
  34. phi=(q-1)*(p-1)
  35. e= 7
  36. # def egcd(a, b):
  37. # if a == 0:
  38. # return (b, 0, 1)
  39. # else:
  40. # g, x, y = egcd(b % a, a)
  41. # return (g, y - (b // a) * x, x)
  42. #
  43. #
  44. # def mulinv(b, n):
  45. # g, x, _ = egcd(b, n)
  46. # if g == 1:
  47. # return x % n
  48. d = 531
  49.  
  50. for i in range(1000):
  51. if i*e%phi==1:
  52. d = i
  53. break
  54.  
  55.  
  56.  
  57.  
  58. print("Write a message")
  59. message=input()
  60. message_symbols = []
  61. private_symbols=[]
  62. decoded_message = []
  63. for s in message:
  64. message_symbols.append(ord(s))
  65.  
  66. for o in message_symbols:
  67. l = o**e%n
  68. b = str(l).encode("UTF-8")
  69. private_symbols.append(b)
  70.  
  71. print(private_symbols)
  72. for r in private_symbols:
  73. b = int(r.decode())
  74.  
  75. l = b**d%n
  76. decoded_message.append(chr(l))
  77.  
  78. print(f"Decoded message: {''.join(decoded_message)}")
  79.  
  80.  
  81.  
  82.  
  83.  
Add Comment
Please, Sign In to add comment