Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. from fractions import gcd
  2. import string
  3.  
  4. alphabet = string.ascii_uppercase
  5.  
  6. def bin2str(num):
  7.     return "{0:b}".format(num)
  8.  
  9. def factor(n):
  10.     a = 2
  11.     j = 2
  12.     B = 73
  13.  
  14.     while j<=B:
  15.         a = pow(a,j) % n
  16.         d = gcd(a-1, n)
  17.         if 1 < d and d < n:
  18.             return d
  19.  
  20.         j+=1
  21.  
  22.     return -1
  23.  
  24. def phi(n):
  25.     nfac = factor(n)
  26.     print(nfac)
  27.     nfac2 = n/nfac
  28.     return (nfac-1)*(nfac2-1)
  29.  
  30. def text_to_int(text):
  31.     text = text.upper()
  32.     n = len(text)
  33.     result = 0
  34.  
  35.     for i in range(n):
  36.         result += alphabet.index(text[i])*pow(26, n-i-1)
  37.  
  38.     return result
  39.  
  40. def int_to_text(number):
  41.     remainders = []
  42.     while number != 0:
  43.         r = int(number % 26)
  44.         remainders.insert(0, r)
  45.         number = number//26
  46.  
  47.     text = ""
  48.     for j in remainders:
  49.         text += alphabet[j]
  50.     return text
  51.  
  52. def egcd(a, b):
  53.     if a == 0:
  54.         return (b, 0, 1)
  55.     else:
  56.         g, y, x = egcd(b % a, a)
  57.         return (g, x - (b // a) * y, y)
  58.  
  59. def modinv(a, m):
  60.     g, x, y = egcd(a, m)
  61.     if g != 1:
  62.         raise Exception('modular inverse does not exist')
  63.     else:
  64.         return x % m
  65.  
  66.  
  67. def quadmult(x, b, n):
  68.     z = 1
  69.     b = bin2str(int(b))[::-1]
  70.  
  71.     for i in range((len(b)-1),-1,-1):
  72.         z = pow(z,2) % n
  73.         print(i)
  74.         if b[i] == "1":
  75.             z = (z*x) % n
  76.  
  77.     return z
  78.  
  79. def rsa_dec(text, n, b):
  80.     dec_numbers = []
  81.     phi_n = phi(n)
  82.     a = modinv(b, phi_n)
  83.     print(a)
  84.     dec_text = ""
  85.    
  86.     for i in range(len(text)):
  87.         dec_numbers.append(quadmult(text[i], a, n))
  88.         dec_text += int_to_text(dec_numbers[i])
  89.  
  90.     return dec_text
  91.  
  92.  
  93. text = [6340, 8309, 14010, 8936, 27358, 25023, 16481, 25809,
  94. 23614, 7135, 24996, 30590, 27570, 26486, 30388, 9395,
  95. 27584, 14999, 4517, 12146, 29421, 26439, 1606, 17881,
  96. 25774, 7647, 23901, 7372, 25774, 18436, 12056, 13547,
  97. 7908, 8635, 2149, 1908, 22076, 7372, 8686, 1304,
  98. 4082, 11803, 5314, 107, 7359, 22470, 7372, 22827,
  99. 15698, 30317, 4685, 14696, 30388, 8671, 29956, 15705,
  100. 1417, 26905, 25809, 28347, 26277, 7897, 20240, 21519,
  101. 12437, 1108, 27106, 18743, 24144, 10685, 25234, 30155,
  102. 23005, 8267, 9917, 7994, 9694, 2149, 10042, 27705,
  103. 15930, 29748, 8635, 23645, 11738, 24591, 20240, 27212,
  104. 27486, 9741, 2149, 29329, 2149, 5501, 14015, 30155,
  105. 18154, 22319, 27705, 20321, 23254, 13624, 3249, 5443,
  106. 2149, 16975, 16087, 14600, 27705, 19386, 7325, 26277,
  107. 19554, 23614, 7553, 4734, 8091, 23973, 14015, 107,
  108. 3183, 17347, 25234, 4595, 21498, 6360, 19837, 8463,
  109. 6000, 31280, 29413, 2066, 369, 23204, 8425, 7792,
  110. 25973, 4477, 30989]
  111.  
  112. dec_text = rsa_dec(text, 31313, 4913)
  113.  
  114. print(dec_text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement