PyCrypto and RSA

Carmine Noviello cnoviello at hotmail.com
Sat Feb 28 03:14:52 EST 2004


Hi,
I want to say first of all that I'm totally a newbie in cryptography world.
Is there someone can show me a simple example on how to use PyCrypto lib and
RSA to encrypt large text file? I'm trying to do it in this way.
First of all I split the text file in sub parts each one of  key.size() // 8
size. Then I encrypt using RSA PublicKey algorithm and store each line in a
target file. This is the pseudo-code:

size = len(plaintext)
bits = []
pos = 0
chunklen = key.size() // 8
while pos < size:
     cipheredText = key.encrypt(plaintext[pos:pos+chuncklen], "")
     bits.append(cipheredText[0])
     pos += chunklen

file.writelines(bits)

This code seemingly works. I have problems with the code that decrypts the
ciphered text.

blocksize = (privkey.size() // 8) + 1
plaintext = ""
line = fo.read(blocksize)
while line != "":
     plaintext += privkey.decrypt(line)
     line = file.read(blocksize)

Often, but not every time, this code raise an exception "Ciphertext too
large". I can't understand where I went wrong.

Thanks in advance.

-- 
Don't you know why your Python application has crashed?
Take a look to http://www.pycrash.org





More information about the Python-list mailing list