Guest User

Untitled

a guest
Dec 6th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import random
  2. import math
  3.  
  4. complexity = int(input())
  5.  
  6. inp = input()
  7. soobsh1 = int(inp)
  8.  
  9. def evkl(a, b, x, y):
  10. if a == 0:
  11. x = 0
  12. y = 1
  13. return (b, (x, y))
  14. x_tmp = 0.5
  15. y_tmp = 0.5
  16. d, (x_tmp, y_tmp) = evkl(b % a, a, x_tmp, y_tmp)
  17. x = y_tmp - (b // a) * x_tmp
  18. y = x_tmp
  19. return (d, (x, y))
  20.  
  21.  
  22. prost = [2]
  23. for i in range(3, complexity + 1, 2):
  24. if (i > 10) and (i % 10 == 5):
  25. continue
  26. for j in prost:
  27. if j * j - 1 > i:
  28. prost.append(i)
  29. break
  30. if (i % j == 0):
  31. break
  32. else:
  33. prost.append(i)
  34. dlin = len(prost)
  35.  
  36. fir = prost[random.randint(dlin - 20, dlin) - 1]
  37. sec = prost[random.randint(dlin - 20, dlin) - 1]
  38.  
  39. while fir == sec:
  40. sec = random.randint(dlin // 2, dlin)
  41. mod = fir * sec
  42. counter = 0
  43. while mod <= soobsh1:
  44. fir = random.randint(dlin // 2, dlin)
  45. while fir == sec:
  46. sec = random.randint(dlin // 2, dlin)
  47. counter += 1
  48. if counter == 1000:
  49. print("Too much information in text. Increase complexity.")
  50. exit(0)
  51.  
  52.  
  53. fi = (fir - 1) * (sec - 1)
  54. exp = []
  55. for i in range(dlin - 1, 0, -1):
  56. tmp = prost[i]
  57. if tmp >= fi or math.gcd(fi, tmp) != 1:
  58. continue
  59. exp.append(tmp)
  60.  
  61. e = exp[random.randint(0, len(exp) - 1)]
  62.  
  63.  
  64.  
  65. open = [e, mod]
  66. print("OpenKey =", open)
  67.  
  68. tmp1, tmp2, tmp3 = e, fi, 1
  69. d1, k = 0.5, 0.5
  70. tmp4, (d1, k) = evkl(tmp1, tmp2, d1, k)
  71. if tmp3 % (tmp1 * d1 + tmp2 * k) != 0:
  72. print("wrong e")
  73. exit(0)
  74. d = d1 * (tmp3 // (tmp1 * d1 + tmp2 * k))
  75. d = d + random.randint(1, complexity) * fi
  76.  
  77. '''
  78. d = 1
  79. while (d * e) % fi != 1:
  80. d += 1
  81. d = d + random.randint(1, complexity) * fi
  82. '''
  83.  
  84. hidden = [d, mod]
  85. print("HiddenKey =", hidden)
  86.  
  87. shifred1 = pow(soobsh1, e, mod)
  88. print("Shifred1 =", shifred1)
  89.  
  90. deshifred1 = pow(shifred1, d, mod)
  91. print("Deshifred1 =", deshifred1)
  92. print()
  93. deshifred = deshifred1
  94. print("Deshifred =", deshifred)
Advertisement
Add Comment
Please, Sign In to add comment