diff options
author | Sybren A. Stüvel <sybren@stuvel.eu> | 2011-06-20 00:23:49 +0200 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2011-06-20 00:23:49 +0200 |
commit | bc7f521f3334eb39f974e9f6c0bb38f64ae989f8 (patch) | |
tree | 4fd5b28051e465cc435353947d1a13699fc6b0b0 /rsa/core.py | |
parent | 2caf3a0299b54ad6e3333492eda56bde56376dd1 (diff) | |
download | rsa-git-bc7f521f3334eb39f974e9f6c0bb38f64ae989f8.tar.gz |
More separation of the code
Diffstat (limited to 'rsa/core.py')
-rw-r--r-- | rsa/core.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/rsa/core.py b/rsa/core.py new file mode 100644 index 0000000..f42801c --- /dev/null +++ b/rsa/core.py @@ -0,0 +1,39 @@ +'''Core mathematical operations. + +This is the actual core RSA implementation, which is only defined +mathematically on integers. +''' + +import types + +import rsa.common + +def encrypt_int(message, ekey, n): + """Encrypts a message using encryption key 'ekey', working modulo n""" + + if type(message) is types.IntType: + message = long(message) + + if not type(message) is types.LongType: + raise TypeError("You must pass a long or int") + + if message < 0 or message > n: + raise OverflowError("The message is too long") + + #Note: Bit exponents start at zero (bit counts start at 1) this is correct + safebit = rsa.common.bit_size(n) - 2 #compute safe bit (MSB - 1) + message += (1 << safebit) #add safebit to ensure folding + + return pow(message, ekey, n) + +def decrypt_int(cyphertext, dkey, n): + """Decrypts a cypher text using the decryption key 'dkey', working + modulo n""" + + message = pow(cyphertext, dkey, n) + + safebit = rsa.common.bit_size(n) - 2 #compute safe bit (MSB - 1) + message -= (1 << safebit) #remove safebit before decode + + return message + |