Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Nov 7 17:41:55 2019
  4.  
  5. @author: Rabeles
  6. """
  7. import random
  8. import itertools as it
  9. import sympy
  10. import math
  11.  
  12. def computeGCD(x, y):
  13.  
  14. while(y):
  15. x, y = y, x % y
  16.  
  17. return x
  18.  
  19. def modulo_multiplicative_inverse(A, M):
  20. return fast_power(A, M-2, M)
  21.  
  22. def fast_power(base, power, MOD):
  23.  
  24.  
  25. result = 1
  26. while power > 0:
  27. # If power is odd
  28. if power % 2 == 1:
  29. result = (result * base) % MOD
  30.  
  31. # Divide the power by 2
  32. power = power // 2
  33. # Multiply base to itself
  34. base = (base * base) % MOD
  35.  
  36. return result
  37. p = next(i for i in map(lambda x: random.randint(1000000000000000000,9999999999999999999) |1,it.count()) if sympy.isprime(i))
  38. print(p)
  39. q = next(i for i in map(lambda x: random.randint(1000000000000000000,9999999999999999999) |1,it.count()) if sympy.isprime(i))
  40.  
  41. print(q)
  42. n = (p*q)
  43. print(n)
  44. fí_n = (p-1)*(q-1)
  45. print("tohle je eulerova fce",fí_n)
  46. while(True):
  47. e = random.randint(1,fí_n-1)
  48. if(computeGCD(e, fí_n) == 1):
  49. break
  50. print ("tohle je koef E po GCD",e)
  51. d = modulo_multiplicative_inverse(e, fí_n)
  52. print (d)
  53.  
  54. print("veřejny klič je (",n,",",e,")")
  55. print("privátní klič je (",n,",",d,")")
  56.  
  57.  
  58. s = "AHOJ"
  59. h = ''.join(str(ord(c)) for c in s)
  60. h = int(h)
  61. print(h)
  62. prevodlistu = ""
  63. for c in s:
  64. prevod = ord(c)
  65. x = f'{prevod:011b}'
  66. prevodlistu += x
  67. # print(x)
  68. cisloslovahoj = int(prevodlistu,2)
  69. print(cisloslovahoj)
  70.  
  71. c = fast_power(cisloslovahoj,e,n)
  72.  
  73. m = fast_power(c,d,n)
  74.  
  75.  
  76. #print("binárí hodnota je " ,f'{h:011b}')
  77. #x = f'{h:011b}'
  78. #x1 = ''.join(str(ord(c)) for c in x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement