views:

630

answers:

6

I have a 'public key' in a variable named varkey, for gettting the public key i used the urllib and stored that public key in a variable now i want to encrypt a msg/string using the public key .

its ok if somebody guide me for any API library

Thanks

A: 

You might want to have a look at:

http://www.example-code.com/python/encryption.asp

or this

http://passingcuriosity.com/2009/aes-encryption-in-python-with-m2crypto/

John Boker
good examples, but I didn't see any public key algorithms
cobbal
A: 

You could use MD5 or SHA1 hashing along with your key...

Gabriel Hurley
+1  A: 

From my recent python experience, python doesn't do encryption natively. You need to use an external (3rd party) package. Each of these, obviously, offers a different experience. Which are you using? This will probably determine how your syntax will vary.

PinkTriangles
+3  A: 

My blog post (the passingcuriosity.com link in John Boker's answer) does AES -- a symmetric encryption algorithm -- using the M2Crypto library. M2Crypto is a Python wrapper around OpenSSL. The API is pretty much a straight translation of OpenSSL's into Python, so the somewhat sketchy documentation shouldn't be too much of a problem. If the public key encryption algorithm you need to use is supported by M2Crypto, then you could very well use it to do your public key cryptography.

I found the M2Crypto test suite to be a useful example of using its API. In particular, the RSA (in test_rsa.py), PGP (in test_pgp.py), and EVP (in test_evp.py) tests will help you figure out how to set up and use the library. Do be aware that they are unit-tests, so it can be a little tricky to figure out exactly what code is necessary and what is an artefact of being a test.

PS: As I'm new, my posts can only contain one link so I had to delete most of them. Sorry.

Example

from M2Crypto import RSA

rsa = RSA.load_pub_key('rsa.pub.pem')
encrypted = rsa.public_encrypt('your message', RSA.pkcs1_oaep_padding)
print encrypted.encode('base64')

Output

X3iTasRwRdW0qPRQBXiKN5zvPa3LBiCDnA3HLH172wXTEr4LNq2Kl32PCcXpIMxh7j9CmysLyWu5
GLQ18rUNqi9ydG4ihwz3v3xeNMG9O3/Oc1XsHqqIRI8MrCWTTEbAWaXFX1YVulVLaVy0elODECKV
4e9gCN+5dx/aG9LtPOE=
thsutton
A: 

Here's the script that demonstrates how to encrypt a message using M2Crypto ($ easy_install m2crypto) given that public key is in varkey variable:

#!/usr/bin/env python
import urllib2
from M2Crypto import BIO, RSA

def readkey(filename):
    try:
        key = open(filename).read()
    except IOError:
        key = urllib2.urlopen(
            'http://svn.osafoundation.org/m2crypto/trunk/tests/' + filename
            ).read()
        open(filename, 'w').write(key)
    return key

def test():
    message = 'disregard the -man- (I mean file) behind curtain'
    varkey = readkey('rsa.pub.pem')
    # demonstrate how to load key from a string
    bio = BIO.MemoryBuffer(varkey)
    rsa = RSA.load_pub_key_bio(bio)
    # encrypt
    encrypted = rsa.public_encrypt(message, RSA.pkcs1_oaep_padding)
    print encrypted.encode('base64')
    del rsa, bio    
    # decrypt
    read_password = lambda *args: 'qwerty'
    rsa = RSA.load_key_string(readkey('rsa.priv2.pem'), read_password)
    decrypted = rsa.private_decrypt(encrypted, RSA.pkcs1_oaep_padding)
    print decrypted
    assert message == decrypted

if __name__ == "__main__":
    test()

Output

gyLD3B6jXspHu+o7M/TGLAqALihw7183E2effp9ALYfu8azYEPwMpjbw9nVSwJ4VvX3TBa4V0HAU
n6x3xslvOnegv8dv3MestEcTH9b3r2U1rsKJc1buouuc+MR77Powj9JOdizQPT22HQ2VpEAKFMK+
8zHbkJkgh4K5XUejmIk=

disregard the -man- (I mean file) behind curtain
J.F. Sebastian
A: 

Very funny. Have you ever heard about "RSAError: data too large for key size" ? No???? Try your sample with more long message:

encrypted = rsa.public_encrypt('My blog post (the passingcuriosity.com link in John Boker's answer) does AES -- a symmetric encryption algorithm -- using the M2Crypto library', RSA.pkcs1_oaep_padding)

best regards

sorokin.in.ua