1. Ivan Vučica
  2. Pythia

Commits

Ivan Vučica  committed a6e009d

Added code for inverse modulo that blocked work in 2009. Added remaining decryption functions.

  • Participants
  • Parent commits 8111e90
  • Branches default

Comments (0)

Files changed (3)

File config.xml

View file
 		worldname="PythiaDemo" 
 		owner="Ivan Vucica" 
 		owneremail="ivan@vucica.net" 
-		url="http://khaoticone.hopto.org/"/>
+		url="http://ivan.vucica.net/pythia/"/>
 </pythia>

File pythia.py

View file
 
 	# parse configuration, set up server, etc
 	print "Parsing configuration..",
-	xmldoc = minidom.parse("config.xml")
+	xmldoc = minidom.parse("config.xml")
 	root = xmldoc.firstChild
-	for param in root.childNodes:
+	for param in root.childNodes:
 		if param.nodeName == 'service':
 			srv.mAddress = param.attributes['address'].nodeValue
 			srv.mPort = int(param.attributes['port'].nodeValue)
 print "Engaging ..."
 
 try:
-	#engage()
-	print 1000000000000*100000000000000000*9999999
+	engage()
 except KeyboardInterrupt:
 	print "Caught signal at pythia.py. "

File rsa.py

View file
 	return encryptInt(cyphertext, dkey, n)
 
 def calcGCD(p, q):
-    if p<q: return calcGCD(q, p)
-    if q == 0: return p
-    return calcGCD(q, abs(p%q))
+	if p<q: return calcGCD(q, p)
+	if q == 0: return p
+	return calcGCD(q, abs(p%q))
+
+# invmodp from: http://code.activestate.com/recipes/576737-inverse-modulo-p/
+def invmodp(a, p):
+	'''
+	The multiplicitive inverse of a in the integers modulo p.
+	Return b s.t.
+	a * b == 1 mod p
+	'''
+	
+	r = a
+	d = 1
+	for count in xrange(p):
+		d = ((p // r + 1) * d) % p
+		r = (d * a) % p
+		if r == 1:
+			break
+	else:
+		raise ValueError('%d has no inverse mod %d' % (a, p))
+	return d
+	
+#def __invmodp__test__():
+#	p = 101
+#	for i in range(1, p):
+#		iinv = invmodp(i, p)
+#		assert (iinv * i) % p == 1
+#__invmodp__test__()
 
 
 class RSA:
 		self.mQ = getInt(q)
 		self.mD = getInt(d)
 
-		pm1 = self.mP - 1
-		qm1 = self.mQ - 1
-	
+		self.mU = invmodp(self.mP, self.mQ)
+		self.mDP = self.mD % (self.mP - 1)
+		self.mDQ = self.mD % (self.mQ - 1)
+		
+		self.mMod = self.mP * self.mQ
+		
+		self.keySet = true
 
+	def decrypt(characters):
+		c = getInt(characters)
+		tmp = c % self.mP
+		v1 = (tmp ** self.mDP) % self.mP
+		tmp = c % self.mQ
+		v2 = (tmp ** self.mDQ) % self.mQ
+
+		u2 = v2 - v1
+		tmp = u2 * self.mU
+		u2 = tmp * self.mQ
+		if u2 < 0:
+			tmp = u2 + self.mQ
+			u2 = tmp
+
+		tmp = u2 * self.mP
+		c = 0
+		c = v1 + tmp
+
+		return getString(c)
+