473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

PyCrypto and RSA

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
Jul 18 '05 #1
2 7004

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

Carmine Noviello wrote:
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.


Jul 18 '05 #2
Ok! I resolved the problem.

--
Don't you know why your Python application has crashed? Take a loot to
http://www.pycrash.org
My Home Page: http://cnoviello.altervista.org
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Haim Ashkenazi | last post by:
Hi I'm trying to compile pycrypto on windows. I've installed mingw and try to run the command 'python setup.py build -c mingw32 install' and I get this error: $ python setup.py build -c...
0
by: Fuzzyman | last post by:
The location of the prebuilt windows installer for PyCypto 2.0 (for python 2.4) has changed. Apologies for any confusion, this is because of a website reorganisation at Voidspace. The new...
11
by: dirvine | last post by:
Does anyone know if pycrypto is active at all. I have been browsing groups etc. for some info and have found entries from 2003 (latest) regarding some bits I was looking for in particular reference...
2
by: Mike Meng | last post by:
Hi all, I'm learning Twisted and downloaded pyOpenSSL and pycrypto win32 installer on http://twisted.sourceforge.net/contrib/ . But I find the lastest version are for Python 2.3. I try to...
13
by: luca72 | last post by:
Hello I have to make an easy operation but reading the pycrypto doc. a never see AES example I have to cript this key 'ea523a664dabaa4476d31226a1e3bab0' with the AES. Can you help me for make it...
2
by: Ning | last post by:
I'm trying to write an IM client which sends encrypted messages to the server. I tried to use pycrypto library, but when I came to 3DES cypher I was confused about the keysize to use. In the...
3
by: mirandacascade | last post by:
Operating system: Win XP Vsn of Python: 2.4 Situation is this: Required to calcluate a message digest. The process for calcluating the digest must use an SHA-256 algorithm. Questions: 1) Is...
5
by: mirandacascade | last post by:
Attempting to determine whether the PyCrypto package has the capability to perform AES256 encryption. I received the following C# snippet: CryptoProvider provider = new CryptoProvider();...
0
by: yaipa | last post by:
I snipped this bit of code out of Andrew Kuchling 'pyCrypto' test fixture. Having a need to XOR Binascii Hex strings in my current project, I found it very helpful to cut down on a bit of code...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.