473,385 Members | 1,673 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.

RSA in python

Hello,

I am looking for good RSA implementations in python that can import a
public key in PEM format and encrypt a buffer using the imported
public key. I tried m2crypto, but somehow it is giving me exceptions
which I couldnt solve as of now.

I get the following with m2crypto:
rsa = M2Crypto.RSA.load_pub_key("pubkey.pem")
gives me:
File "KB_Client.py", line 63, in SessionInit
rsa = M2Crypto.RSA.load_pub_key("pubkey.pem")
File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 225,
in load_pub_key
return load_pub_key_bio(bio)
File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 237,
in load_pub_key_bio
rsa_error()
File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 162,
in rsa_error
raise RSAError, m2.err_reason_error_string(m2.err_get_error())
M2Crypto.RSA.RSAError: no start line
Any help will be highly appreciated.

Regards,
-abhisek
Feb 17 '06 #1
7 9034
Did you try this: http://www.amk.ca/python/code/crypto ?

Feb 17 '06 #2
I tried the ezPyCrypto which is basically a higher level wrapper to pyCrypto
but it fails to import public key in PEM format.

-abhisek

On 17 Feb 2006 07:02:51 -0800, wi******@hotmail.com
<ma**********@gmail.com> wrote:
Did you try this: http://www.amk.ca/python/code/crypto ?

--
http://mail.python.org/mailman/listinfo/python-list

Feb 17 '06 #3
stupid question, but did you try:

rsa = M2Crypto.RSA.load_pub_key( file("pubkey.pem") )

It is not clear from the documentation what they want for "file" (and
because Python has no type declarations, you are left to guess...).

May not work... just thought I would ask.

Feb 17 '06 #4
Looking at the api documentation again, it is possible that they want
this:

pubkey = open( 'pubkey.pem', 'rb' ).read() # binary read here?
rsa = M2Crypto.RSA.load_pub_key(pubkey)

Anyway, things to play with...

Feb 17 '06 #5
Abhisek Datta wrote:
I am looking for good RSA implementations in python that can import a
public key in PEM format and encrypt a buffer using the imported
public key. I tried m2crypto, but somehow it is giving me exceptions
which I couldnt solve as of now.


What you are trying to do should be covered by M2Crypto. Without more
context for the error (what was the file you were trying load, etc.) it
is hard to say what is wrong. My first guess is that your pem file did
not include the BEGIN/END lines.

I would suggest you take a look at the tests and samples included in
M2Crypto. They do show how to work with PEM files.
M2Crypto 0.15: http://wiki.osafoundation.org/bin/vi...ts/MeTooCrypto
Feb 17 '06 #6
Hello,

Here is some debugging output:

---
pyKB-DEBUG: Connecting to http://192.168.0.2:8080/RPC2
pyKB-DEBUG: Initializing session: (sessionKey: ABCDEFGHIJKLMNO)
pyKB-DEBUG: Received public key:
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBALxi3tGXlSwRgn7/Km6mTSge+5ijQgIn3GvnZOeYyOo1DkubVtTaFj26
GWtJo43MEe1a5UlWKJEOpbKVCr4AASfFj8YmmRewH4SXdZ+w1B ad8amyzL2h8F7J
wJojOnocSs6xDE7o86CpZRUlojBefanMdCpu074QFktE63OD1z BBAgMBAAE=
-----END RSA PUBLIC KEY-----

Traceback (most recent call last):
File "KB_Client.py", line 96, in ?
kb.SessionInit(sessionKey="ABCDEFGHIJKLMNO")
File "KB_Client.py", line 63, in SessionInit
rsa = M2Crypto.RSA.load_pub_key("pubkey.pem")
File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 225,
in load_pub_key
return load_pub_key_bio(bio)
File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 237,
in load_pub_key_bio
rsa_error()
File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 162,
in rsa_error
raise RSAError, m2.err_reason_error_string(m2.err_get_error())
M2Crypto.RSA.RSAError: no start line
---

You can clearly see the public key which I am receiving from an xmlrpc
server. Then I saved the public key in "pubkey.pem" file and then use
M2Crypto.RSA.load_pub_key() and I am getting the output as show above.

-abhisek

On 2/17/06, Heikki Toivonen <he****@osafoundation.org> wrote:
Abhisek Datta wrote:
I am looking for good RSA implementations in python that can import a
public key in PEM format and encrypt a buffer using the imported
public key. I tried m2crypto, but somehow it is giving me exceptions
which I couldnt solve as of now.


What you are trying to do should be covered by M2Crypto. Without more
context for the error (what was the file you were trying load, etc.) it
is hard to say what is wrong. My first guess is that your pem file did
not include the BEGIN/END lines.

I would suggest you take a look at the tests and samples included in
M2Crypto. They do show how to work with PEM files.
M2Crypto 0.15: http://wiki.osafoundation.org/bin/vi...ts/MeTooCrypto
--
http://mail.python.org/mailman/listinfo/python-list

Feb 18 '06 #7
Abhisek Datta wrote:
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBALxi3tGXlSwRgn7/Km6mTSge+5ijQgIn3GvnZOeYyOo1DkubVtTaFj26
GWtJo43MEe1a5UlWKJEOpbKVCr4AASfFj8YmmRewH4SXdZ+w1B ad8amyzL2h8F7J
wJojOnocSs6xDE7o86CpZRUlojBefanMdCpu074QFktE63OD1z BBAgMBAAE=
-----END RSA PUBLIC KEY-----

Traceback (most recent call last): [...] M2Crypto.RSA.RSAError: no start line


The files in M2Crypto tests directory don't have any files that have
"BEGIN RSA PUBLIC KEY" in them - haven't checked if this is legal or not.

However, plain OpenSSL does not like that file either:

$ openssl rsa -in rsa_heikki.pem -pubin
3440:error:0906D06C:PEM routines:PEM_read_bio:no
startline:pem_lib.c:644:Expecting: PUBLIC KEY

$ openssl rsa -in rsa_heikki.pem -check
2140:error:0906D06C:PEM routines:PEM_read_bio:no
startline:pem_lib.c:644:Expecting: ANY PRIVATE KEY

If I take out the "RSA " part from the delimiter lines I get:

$ openssl rsa -in rsa_heikki2.pem -pubin
unable to load Public Key
3124:error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong
tag:tasn_dec.c:
1282:
3124:error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1
error:ta
sn_dec.c:374:Type=X509_ALGOR
3124:error:0D08303A:asn1 encoding
routines:ASN1_TEMPLATE_NOEXP_D2I:nested asn1 e
rror:tasn_dec.c:743:Field=algor, Type=X509_PUBKEY
3124:error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib:pem_oth.c:83:

$ openssl rsa -in rsa_heikki2.pem -check
unable to load Private Key
2304:error:0906D06C:PEM routines:PEM_read_bio:no start
line:pem_lib.c:644:Expect
ing: ANY PRIVATE KEY
My suggestion would be to ask on the openssl-users list.

--
Heikki Toivonen

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFD+2WNb8x8KoP+JuwRAmC2AJ0fYurEBdoLb+b885bUoL z9nQ0fmwCgqFjv
UTLYnmP31hUrq+cO46QAywI=
=lLfI
-----END PGP SIGNATURE-----

Feb 21 '06 #8

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

Similar topics

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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.