diff options
author | Sybren A. Stüvel <sybren@stuvel.eu> | 2011-06-20 01:06:39 +0200 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2011-06-20 01:06:39 +0200 |
commit | 62abca7fce2b3c4e9b16b43209371bf3c225fb6d (patch) | |
tree | 0cea32fbadc8bab97e509d7f095b5564ae810b0e /rsa/__init__.py | |
parent | 0060065bf34d426bc634ff6dac1ac9475ca4687e (diff) | |
download | rsa-git-62abca7fce2b3c4e9b16b43209371bf3c225fb6d.tar.gz |
Added block padding to be able to work with leading zeroes, breaks all kind of stuff
Diffstat (limited to 'rsa/__init__.py')
-rwxr-xr-x | rsa/__init__.py | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/rsa/__init__.py b/rsa/__init__.py index d9999ae..7fda4e1 100755 --- a/rsa/__init__.py +++ b/rsa/__init__.py @@ -42,6 +42,19 @@ def decode64chops(string): return chops +def block_size(n): + '''Returns the block size in bytes, given the public key. + + The block size is determined by the 'n=p*q' component of the key. + ''' + + # Set aside 2 bits so setting of safebit won't overflow modulo n. + nbits = rsa.common.bit_size(n) - 2 + nbytes = nbits / 8 + + return nbytes + + def chopstring(message, key, n, int_op): """Chops the 'message' into integers that fit into n. @@ -56,13 +69,10 @@ def chopstring(message, key, n, int_op): Used by 'encrypt' and 'sign'. """ - msglen = len(message) - mbits = msglen * 8 - # Set aside 2 bits so setting of safebit won't overflow modulo n. - nbits = rsa.common.bit_size(n) - 2 + nbytes = block_size(n) - nbytes = nbits / 8 + msglen = len(message) blocks = msglen / nbytes if msglen % nbytes > 0: @@ -90,14 +100,25 @@ def gluechops(string, key, n, funcref): messageparts = [] chops = decode64chops(string) #Decode base64 strings into integer chops + + nbytes = block_size(n) for chop in chops: value = funcref(chop, key, n) #Decrypt each chop block = rsa.transform.int2bytes(value) + + # Pad block with 0-bytes until we have reached the block size + blocksize = len(block) + padsize = nbytes - blocksize + if padsize < 0: + raise ValueError('Block larger than block size (%i > %i)!' % + (blocksize, nbytes)) + elif padsize > 0: + block = '\x00' * padsize + block + messageparts.append(block) # Combine decrypted strings into a msg - return ''.join(messageparts) def encrypt(message, key): |