RSA in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abhisek Datta

    #1

    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.lo ad_pub_key("pub key.pem")
    gives me:
    File "KB_Client. py", line 63, in SessionInit
    rsa = M2Crypto.RSA.lo ad_pub_key("pub key.pem")
    File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 225,
    in load_pub_key
    return load_pub_key_bi o(bio)
    File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 237,
    in load_pub_key_bi o
    rsa_error()
    File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 162,
    in rsa_error
    raise RSAError, m2.err_reason_e rror_string(m2. err_get_error() )
    M2Crypto.RSA.RS AError: no start line


    Any help will be highly appreciated.

    Regards,
    -abhisek
  • wittempj@hotmail.com

    #2
    Re: RSA in python

    Did you try this: http://www.amk.ca/python/code/crypto ?

    Comment

    • Abhisek Datta

      #3
      Re: RSA in python

      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, wittempj@hotmai l.com
      <martin.witte@g mail.com> wrote:[color=blue]
      > Did you try this: http://www.amk.ca/python/code/crypto ?
      >
      > --
      > http://mail.python.org/mailman/listinfo/python-list
      >[/color]

      Comment

      • plahey@alumni.caltech.edu

        #4
        Re: RSA in python

        stupid question, but did you try:

        rsa = M2Crypto.RSA.lo ad_pub_key( file("pubkey.pe m") )

        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.

        Comment

        • plahey@alumni.caltech.edu

          #5
          Re: RSA in python

          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.lo ad_pub_key(pubk ey)

          Anyway, things to play with...

          Comment

          • Heikki Toivonen

            #6
            Re: RSA in python

            Abhisek Datta wrote:[color=blue]
            > 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.[/color]

            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

            Comment

            • Abhisek Datta

              #7
              Re: RSA in python

              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-----
              MIGJAoGBALxi3tG XlSwRgn7/Km6mTSge+5ijQgI n3GvnZOeYyOo1Dk ubVtTaFj26
              GWtJo43MEe1a5Ul WKJEOpbKVCr4AAS fFj8YmmRewH4SXd Z+w1Bad8amyzL2h 8F7J
              wJojOnocSs6xDE7 o86CpZRUlojBefa nMdCpu074QFktE6 3OD1zBBAgMBAAE=
              -----END RSA PUBLIC KEY-----

              Traceback (most recent call last):
              File "KB_Client. py", line 96, in ?
              kb.SessionInit( sessionKey="ABC DEFGHIJKLMNO")
              File "KB_Client. py", line 63, in SessionInit
              rsa = M2Crypto.RSA.lo ad_pub_key("pub key.pem")
              File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 225,
              in load_pub_key
              return load_pub_key_bi o(bio)
              File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 237,
              in load_pub_key_bi o
              rsa_error()
              File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 162,
              in rsa_error
              raise RSAError, m2.err_reason_e rror_string(m2. err_get_error() )
              M2Crypto.RSA.RS AError: 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.lo ad_pub_key() and I am getting the output as show above.

              -abhisek

              On 2/17/06, Heikki Toivonen <heikki@osafoun dation.org> wrote:[color=blue]
              > Abhisek Datta wrote:[color=green]
              > > 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.[/color]
              >
              > 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
              >[/color]

              Comment

              • Heikki Toivonen

                #8
                Re: RSA in python

                Abhisek Datta wrote:[color=blue]
                > -----BEGIN RSA PUBLIC KEY-----
                > MIGJAoGBALxi3tG XlSwRgn7/Km6mTSge+5ijQgI n3GvnZOeYyOo1Dk ubVtTaFj26
                > GWtJo43MEe1a5Ul WKJEOpbKVCr4AAS fFj8YmmRewH4SXd Z+w1Bad8amyzL2h 8F7J
                > wJojOnocSs6xDE7 o86CpZRUlojBefa nMdCpu074QFktE6 3OD1zBBAgMBAAE=
                > -----END RSA PUBLIC KEY-----
                >
                > Traceback (most recent call last):[/color]
                [...][color=blue]
                > M2Crypto.RSA.RS AError: no start line[/color]

                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:0906 D06C:PEM routines:PEM_re ad_bio:no
                startline:pem_l ib.c:644:Expect ing: PUBLIC KEY

                $ openssl rsa -in rsa_heikki.pem -check
                2140:error:0906 D06C:PEM routines:PEM_re ad_bio:no
                startline:pem_l ib.c:644:Expect ing: 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:0D06 80A8:asn1 encoding routines:ASN1_C HECK_TLEN:wrong
                tag:tasn_dec.c:
                1282:
                3124:error:0D07 803A:asn1 encoding routines:ASN1_I TEM_EX_D2I:nest ed asn1
                error:ta
                sn_dec.c:374:Ty pe=X509_ALGOR
                3124:error:0D08 303A:asn1 encoding
                routines:ASN1_T EMPLATE_NOEXP_D 2I:nested asn1 e
                rror:tasn_dec.c :743:Field=algo r, Type=X509_PUBKE Y
                3124:error:0906 700D:PEM routines:PEM_AS N1_read_bio:ASN 1 lib:pem_oth.c:8 3:

                $ openssl rsa -in rsa_heikki2.pem -check
                unable to load Private Key
                2304:error:0906 D06C:PEM routines:PEM_re ad_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+2WNb8x 8KoP+JuwRAmC2AJ 0fYurEBdoLb+b88 5bUoLz9nQ0fmwCg qFjv
                UTLYnmP31hUrq+c O46QAywI=
                =lLfI
                -----END PGP SIGNATURE-----

                Comment

                Working...