PyCrypto and RSA

C

Carmine Noviello

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.
 
G

Gandalf

Hello!

I think you should not do this. RSA encryption is quite expensive (in
terms of processor time). You should only encrypt
a session key with RSA and then use that session key with a symmetric
algorigthm (e.g. Blowfish, AES, TwoFish etc)
to encode a large file. You can create a session key from a
cryptographically secure PRNG (Pseudo Random Number
Generator).

Well, this was not actually an anwer to your question. I did not want to
offend you. It was only a suggestion.

Cheers,

G
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top