Rumel

RSA

Nov 7th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. #Program to break RSA Encrytion (192 bits)
  2. #For UNL CSE 235
  3. import fractions
  4. import binascii
  5.  
  6. def moddingFunction(base, exp, modulus):
  7.     exp = bin(exp)
  8.     exp = exp[2:]
  9.     length = len(exp)
  10.     result = 1
  11.     i = 1
  12.     while i <= length:
  13.         if (exp[length - i] == "1"):
  14.             result = result * base % modulus
  15.         i+=1
  16.         base = base * base % modulus
  17.     return result
  18.    
  19. def findAscii(binary):
  20.     num = int(binary, 2)
  21.     s = chr(num)
  22.     return s
  23.    
  24. def parseBinary(binaryString):
  25.     length = len(str(binaryString))
  26.     string = str(binaryString)
  27.     i = 0
  28.     s = ""
  29.     while i < length:
  30.         s+=findAscii(string[i:i+8])
  31.         i+=8
  32.     return s
  33.    
  34. def euclidianAlg(a, b):
  35.     a0 = a
  36.     b0 = b
  37.     t0 = 0
  38.     t = 1
  39.     s0 = 1
  40.     s = 0
  41.     q = a0 / b0
  42.     r = a0 - q * b0
  43.     while r > 0:
  44.         temp = t0 - q * t
  45.         t0 = t
  46.         t = temp
  47.         temp = s0 - q * s
  48.         s0 = s
  49.         s = temp
  50.         a0 = b0
  51.         b0 = r
  52.         q = a0 / b0
  53.         r = a0 - q * b0
  54.         if r > 0:
  55.             gcd = r
  56.    
  57.     return s
  58.    
  59. n = int("71b01d67076bae7f77834fc8de6f7fd5d82c83118debce5b", 16)
  60. a = int("11ce93d4ad2d558e9e96d8a2155593bd8cd9f8c054bf15", 16)
  61.  
  62. p = 44923681168431411353414651281
  63. q = 62052358958489486690048694059
  64. totient = (p - 1) * (q - 1)
  65.  
  66. #find ainv
  67.  
  68. ainv = euclidianAlg(a, totient)
  69. #find b
  70. b = ainv % totient
  71.  
  72. #decrypt is y^b mod n y is the line in hex
  73. #build string at the same time
  74. s = ""
  75. file = open('cipher.txt', 'r')
  76. for line in file:
  77.     num = int(line, 16)
  78.     num = moddingFunction(num, b, n)
  79.     test = pow(num, b, n)
  80.     num = bin(num)
  81.     num = num[2:]
  82.     while (len(num) < 96):
  83.         num = "0" + num
  84.     s+=parseBinary(num)
  85.    
  86. print s
Advertisement
Add Comment
Please, Sign In to add comment