Commits

Sybren Stüvel  committed 74a192d

Added buggy CRT decryption

  • Participants
  • Parent commits cbb1af2
  • Branches crt

Comments (0)

Files changed (2)

     message = pow(cyphertext, dkey, n)
     return message
 
+def decrypt_crt(cyphertext, priv_key):
+    '''Decrypts a cypher text using the private key.
+
+    :param cyphertext: integer that has to be decoded.
+    :param priv_key: :py:class:`rsa.PrivateKey` to decode with
+    '''
+
+    assert_int(cyphertext, 'cyphertext')
+    m1 = (cyphertext * priv_key.exp1) % priv_key.p
+    m2 = (cyphertext * priv_key.exp2) % priv_key.q
+    h = (priv_key.coef * (m1 - m2)) % priv_key.p
+    message = m2 + h * priv_key.q
+
+    return message
+

File tests/test_integers.py

         encrypted = rsa.core.encrypt_int(message, self.pub.e, self.pub.n)
         print "\tEncrypted: %d" % encrypted
 
-        decrypted = rsa.core.decrypt_int(encrypted, self.priv.d, self.pub.n)
+        decrypted = rsa.core.decrypt_int(encrypted, self.priv.d, self.priv.n)
+        print "\tDecrypted: %d" % decrypted
+
+        self.assertEqual(message, decrypted)
+
+    def test_decode_crt(self):
+
+        message = 42
+        print "\tMessage:   %d" % message
+
+        encrypted = rsa.core.encrypt_int(message, self.pub.e, self.pub.n)
+        print "\tEncrypted: %d" % encrypted
+
+        decrypted = rsa.core.decrypt_crt(encrypted, self.priv)
         print "\tDecrypted: %d" % decrypted
 
         self.assertEqual(message, decrypted)
 
         message = 42
 
-        signed = rsa.core.encrypt_int(message,self.priv.d, self.pub.n)
+        signed = rsa.core.encrypt_int(message,self.priv.d, self.priv.n)
         print "\tSigned:    %d" % signed
 
         verified = rsa.core.decrypt_int(signed, self.pub.e,self.pub.n)