Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Python function to encrypt and decrypt password

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Belgium
    Beans
    78
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Python function to encrypt and decrypt password

    Hi,

    I want to write a password to a configfile but instead of writing the password as plain text i would like to save it in a human unreadable format.

    the security doesn't need to be excellent, but the purpose is that users that open the file can not read the password immediately...

    the function also has to be able to decrypt the password again so i can use it in my application.

    is there any simple python function for this or a project that uses such a fuction?

    thanks

  2. #2
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Python function to encrypt and decrypt password

    You could use the bzip2 module. The security wont be very high, but the password can definitely be stored in a non-human readable form.

    PHP Code:
    >>> import bz2
    >>> password "NoVa52:)"
    >>> encrypted_password bz2.compress(password)
    >>> 
    encrypted_password
    'BZh91AY&SY\xae\x1d\x86!\x00\x00\x02\x1f\x00\x00 \x12\x10\x00\x01\x01\x00 \x00\xa0\x00"\x06\'\xa8C\x02\x12\xe6\xc0\xf1w$S\x85\t\n\xe1\xd8b\x10'
    >>> print bz2.decompress(encrypted_password)
    NoVa52:)
    >>> 

  3. #3
    Join Date
    Dec 2009
    Beans
    Hidden!

    Re: Python function to encrypt and decrypt password

    Quote Originally Posted by NovaAesa View Post
    You could use the bzip2 module. The security wont be very high, but the password can definitely be stored in a non-human readable form.

    PHP Code:
    >>> import bz2
    >>> password = "NoVa52:)"
    >>> 
    encrypted_password bz2.compress(password)
    >>> 
    encrypted_password
    'BZh91AY&SY\xae\x1d\x86!\x00\x00\x02\x1f\x00\x00 \x12\x10\x00\x01\x01\x00 \x00\xa0\x00"\x06\'\xa8C\x02\x12\xe6\xc0\xf1w$S\x85\t\n\xe1\xd8b\x10'
    >>> print bz2.decompress(encrypted_password)
    NoVa52:)
    >>> 
    Thus your password will always be bz2.decompress. so much for encryption. Maybe store an sha-512 sum and do a comparison to validate the data.
    Last edited by d3v1150m471c; November 6th, 2011 at 08:40 AM.

  4. #4
    Join Date
    Oct 2005
    Location
    Belgium
    Beans
    78
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Python function to encrypt and decrypt password

    Thank you for your respond and the compression works very good.
    but when i write it to a file and after read it and decompress it i get this error:
    Code:
    Traceback (most recent call last):
      File "<pyshell#20>", line 1, in <module>
        bz2.decompress(password)
    ValueError: couldn't find end of stream
    This is what i did:
    Code:
    >>> f=open("test.txt", "w")
    >>> f.write(bz2.compress("mypassword"))
    >>> f.close()
    >>> w = open("test.txt", "r")
    >>> password = w.readline()
    >>> w.close()
    >>> bz2.decompress(password)
    Traceback (most recent call last):
      File "<pyshell#20>", line 1, in <module>
        bz2.decompress(password)
    ValueError: couldn't find end of stream
    >>>
    It works fine if i dont write it to a file

  5. #5
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Python function to encrypt and decrypt password

    I'll fiddle around with it a bit and see if I can work it out.

  6. #6
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Python function to encrypt and decrypt password

    Okay, I worked it out. Try this:

    PHP Code:
    import bz2
    f
    =open("test.txt""w")
    f.write(bz2.compress("mypassword"))
    f.close()
    open("test.txt""r")
    password bz2.decompress(w.read())
    w.close()
    print 
    password 
    The readline method only reads up to the end of line charactor '\n'. The compressed password had a \n in it, which would have made it read only half of the password and hence raise an error. The read method reads the whole file instead.

    EDIT: btw here's a link to the docs for the bz2 module: http://www.python.org/doc/2.4/lib/module-bz2.html
    Last edited by NovaAesa; January 11th, 2008 at 01:04 PM. Reason: addition

  7. #7
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Python function to encrypt and decrypt password

    Also, you can store any object as pickle

  8. #8
    Join Date
    Oct 2005
    Location
    Belgium
    Beans
    78
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Python function to encrypt and decrypt password

    The problem is that i also have other fields in the configfile and i acces them using the iniparser module.
    so the iniparser module has to be able to get the password and send it to the program where it will be decompressed so i dont think using pickle module is an option.

    @NovaAesa: it still doesn't work but currently i am at work and using python for windows...this evening i will try it on linux.

  9. #9
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Python function to encrypt and decrypt password

    @pmasiar - i think the pickle module stores things as plain text doesn't it? So it might pose a security risk. I think it would store it something along the lines of:
    S'mypassword'
    p0
    .
    @G|N| tell me if it works when you try on Linux. I have never used python on windows so I don't know if there would be any difference.

  10. #10
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: Python function to encrypt and decrypt password

    Do you really need to decrypt it, or can you just encrypt the password that the user enters and check that with the previously encrypted password (in the file). This is how almost all password systems work. If you can do this, then I would suggest using sha or md5. Both of which are typically used by websites, for instance, to hash passwords for safe storage. They aren't reversible (technically) but you use the same algorithm on the login password so you're comparing the two hashes.

    BTW:

    NovaAesa, if you change the extension of your "txt" file to "bz2", you can unzip the password with most archiving programs
    Last edited by Wybiral; January 11th, 2008 at 03:43 PM.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •