[Nym3-commit] r245 - trunk/nym3

laurent at conuropsis.org laurent at conuropsis.org
Sat May 7 12:31:58 CEST 2005


Author: laurent
Date: 2005-05-07 12:31:56 +0200 (Sat, 07 May 2005)
New Revision: 245

Modified:
   trunk/nym3/Crypto.py
Log:
Added nym_decrypt, with tests.


Modified: trunk/nym3/Crypto.py
===================================================================
--- trunk/nym3/Crypto.py	2005-05-06 20:58:18 UTC (rev 244)
+++ trunk/nym3/Crypto.py	2005-05-07 10:31:56 UTC (rev 245)
@@ -25,7 +25,7 @@
 
 
 import random
-from mixminion.Packet import compressData, OAEP_OVERHEAD
+import mixminion.Packet as _mp
 import mixminion.Crypto as _cr
 import Message
 
@@ -35,7 +35,7 @@
     key = _cr.pk_decode_public_key(key)
     n = Message.nbBits(_cr.pk_get_modulus(key)) / 8
     assert n == 128 or n == 256
-    dataC = compressData(data)
+    dataC = _mp.compressData(data)
     paddingLen = 128 - (len(dataC) % 128)
     dataP = dataC + chr(0) * paddingLen
     k = ""
@@ -45,6 +45,49 @@
     dataE = _cr.lioness_encrypt(dataP, K.getLionessKeys(""))
     #42 : size of OAEP padding(bytes)
     #20 : size of k (bytes)
-    rsaLen = n - OAEP_OVERHEAD - 20
+    rsaLen = n - _mp.OAEP_OVERHEAD - 20
     rsaPart = _cr.pk_encrypt(k + dataE[:rsaLen], key)
     return rsaPart + dataE[rsaLen:]
+
+def nym_decrypt(data, key):
+    """Decrypts data with the given key, follwoing specs §4.2.
+       Key is the ASN.1 encoding of the actual public key"""
+    key = _cr.pk_decode_private_key(key)
+    n = Message.nbBits(_cr.pk_get_modulus(key)) / 8
+    rsaPart = data[:n]
+    rsaPart = _cr.pk_decrypt(rsaPart, key)
+    enc = data[n:]
+    K = rsaPart[:20]
+    enc = rsaPart[20:] + enc
+    K = _cr.Keyset(K)
+    clear = _cr.lioness_decrypt(enc, K.getLionessKeys(""))
+    return _mp.uncompressData(clear)
+
+if __name__ == '__main__':
+    testkeyfile = '/tmp/2048'
+    try:
+	f = open(testkeyfile + ".pub", "r")
+	pubasn = f.read()
+	f.close()
+	f = open(testkeyfile + ".sec", "r")
+	secasn = f.read()
+	f.close()
+    except:
+	key = _cr.pk_generate(bits = 2048)
+	pubasn = _cr.pk_encode_public_key(key)
+	secasn = _cr.pk_encode_private_key(key)
+	f = open(testkeyfile + ".pub", "w")
+	f.write(pubasn)
+	f.close()
+	f = open(testkeyfile + ".sec", "w")
+	f.write(secasn)
+	f.close()
+	
+    data = """I must not fear. Fear is the mind-killer. Fear is the
+	      little-death that brings total obliteration. I will face my fear.
+	      I will permit it to pass over me and through me. And when it has
+	      gone past I will turn the inner eye to see its path. Where the
+	      fear has gone there will be nothing. Only I will remain."""
+    cipher = nym_encrypt(data, pubasn)
+    clear = nym_decrypt(cipher, secasn)
+    print clear == data



More information about the Nym3-commit mailing list