Password encryption and decryption in Python - python

I understand the difference between Hashing and Encryption. I am looking for a simple way to implement encryption/decryption strings in Python. Most of the ways I found on-line was about using Hashing algorithms ( MD5 - SHA-1 etc... ) in order to do one way hashing. But unfortunately, hashing is irreversible. Any suggestions ?

You might be doing something wrong.
If you don't want to give an attacker access to all passwords stored in the database, you should not reverse the hash to recover the password and compare it with input. You should hash the input and compare that to the hashed password.
But maybe you aren't.
Perhaps you still would like to encrypt something, in such a way that it is possible to decrypt it later. There's a module called PyCrypto that can help you with this, implementing a large quantity of unique and strong algorithms to allow secure transport or storage of sensitive data.

Related

How Can I store data so users can't easily tamper with it?

I'm fairly new to Python and as a project I'm working on a little game. I'd like to make sure my data is stored in a format that I won't run into problems moving forward.
The data I'll need to store will be integral to generating some in-game elements through procedural generation—essentially storing names, properties, and possible behaviors for these objects. I would not want players to be able to easily edit a file and change values so that a common level 1 creature suddenly drops 2 million gold pieces.
My intentions are to eventually use Py2Exe or PyInstaller. I have considered XML, YAML, and JSON but I'm not sure which direction I should go or what I may not be aware of.
I'm sure if a user wanted to badly enough they could figure it out—but what is the best way to make it inconvenient for the average user to tamper with said data?
This is impossible to solve. You should use some kind of encryption or signature scheme to make sure the data is not tampered with in combination with obfuscation so that the secret algorithm or secret key cannot be easily extracted from the ciphertext.
Since this is about tamper protection and not data confidentiality, I would suggest to you to use a digital signature algorithm. You would generate a developer private+public key pair, use the private key to sign the static data and put the public key into the code in an obfuscated fashion.
This is protects the manipulation of your data better than any encryption scheme under the assumption that it is harder to manipulate your program than to simply decompile and read the source code.
If you would for example use a symmetric cipher to encrypt that data and store the key in your code, a malicious user could deduce the algorithm and key from your code and implement this. The user would then decrypt the static data, manipulate it and re-encrypt it again. Your program which was not manipulated would never know that the content was changed.
It also has to be asymmetric (such as RSA signature or ECDSA), so using an HMAC over your static data would have the same issue as symmetric encryption.

How could I securely embed a required password into source code?

I am writing a Python code and it is requisite to include a password of one of my online account. I want to cover it in some way as keeping its functionality in the code. Is there nay way to masquerade this kind of credential information as keeping its use in the source code?
I would recommend two levels to secure passwords. 1 encrypt, 2, protect the key used for encrypting in key store.
Details- Encrypt the password using aes 256 or similar based on the risk. Key used for encrypting should be in key store and you can hard code the key store password.
You can also choose number of levels based on risk, usually at least two is recommended.
You should tell us what kind of protection you want. Do you want to make everybody able to execute you script without knowing the password? Do you want to be the only able to execute you script but you want to protect the password from people who can read the source? There may be different solution.
However every solution will require you to insert another password to get access to the stored password. So I think that the best solution would be not to save the password in the source at all.

Access hashed mysql password from python

This seems somewhat trival but I have spent a bit of time googling with no results. Anyways I am developing a web app and I need to store 'sensitive' data in a mysql database and then access it to authenticate api calls from python. I am no security expert but I have a basic understanding of hashing and encrypting.
I know how to encrypt the data with aes_decrypt aes_encrypt and I also know how to hash the data using the password() function supplied by mysql.
My first question is should I be encrypting this data or hashing it? My second question is I do not know how to 'access' or 'use' the password information in python once I hash it using the password() function in mysql.
Any help is much appreciated.
Firstly I am no python expert, my answer is only aimed for a general approach.
Passwords in web applications are usually stored as hashes, not encrypted, this basically makes it harder for someone to get them if your table is compromised. Hashes should be generated as solid as possible. Please do not just a MD5, better use something more secure (from todays perspective) and salt it properly to minimize the risk of rainbow attacks.
I wouldn't use the MySQL Password() function for this. The documentation says:
The PASSWORD() function is used by the authentication system in MySQL
Server; you should not use it in your own applications. For that
purpose, consider MD5() or SHA2() instead.
This leaves SHA2(), if you want to hash with MySQL, though don't forget to salt the string before hashing. My way of doing it would be to hash the string with your application (see python hashlib for reference), salt it like this and then just store the hash in the database. This avoids security issues of your data between your application and the database server.

M2Crypto, Encrypt and Sign at once?

This might be something obvious that I'm missing.
I would like to have my host encrypt a message to a client using the client's public key, and also have that message signed using the hosts private key.
It seems like an obvious scenario, but perhaps my concept is simply wrong. I think that you should be able to do with with a single message, much like you see using PGP. Can this be done with M2Crypto easily?
I tried first encrypting the message and then signing it but I get the message "RSAError: digest too big for rsa key".
I would rather not send the encrypted message and its signature as two separate pieces of data.
Edit:
For the time being I'm using a custom delimiter, to separate the message from the signature, but I feel like this is bad form, and that the format should have provisions for what I'm attempting.
It sounds like you're looking for a hybrid cryptosystem which takes care of encryption and signature together, using appropriate crypto primitives to allow it to work whatever the size of the data, and encapsulating all the components of the cryptogram in one place. PGP, HTTPS and DHIES are good examples. While it seems to me that you could implement such a system using m2crypto, you probably shouldn't; you're much better off reusing an existing protocol than rolling your own. It's far too easy to make mistakes which are hard to spot and render the security useless.

How To Reversibly Store Password With Python On Linux?

First, my question is not about password hashing, but password encryption. I'm building a desktop application that needs to authentificate the user to a third party service. To speed up the login process, I want to give the user the option to save his credentials. Since I need the password to authentificate him to the service, it can't be hashed.
I thought of using the pyCrypto module and its Blowfish or AES implementation to encrypt the credentials. The problem is where to store the key. I know some applications store the key directly in the source code, but since I am coding an open source application, this doesn't seem like a very efficient solution.
So I was wondering how, on Linux, you would implement user specific or system specific keys to increase password storing security.
If you have a better solution to this problem than using pyCrypto and system/user specific keys, don't hesitate to share it. As I said before, hashing is not a solution and I know password encryption is vulnerable, but I want to give the option to the user. Using Gnome-Keyring is not an option either, since a lot of people (including myself) don't use it.
Encrypting the passwords doesn't really buy you a whole lot more protection than storing in plaintext. Anyone capable of accessing the database probably also has full access to your webserver machines.
However, if the loss of security is acceptable, and you really need this, I'd generate a new keyfile (from a good source of random data) as part of the installation process and use this. Obviously store this key as securely as possible (locked down file permissions etc). Using a single key embedded in the source is not a good idea - there's no reason why seperate installations should have the same keys.
Try using PAM. You can make a module that automatically un-encrypts the key when the user logs in. This is internally how GNOME-Keyring works (if possible). You can even write PAM modules in Python with pam_python.
Password Safe is designed by Bruce Schneier and open source. It's for Windows, but you should be able to see what they are doing and possibly reuse it.
http://www.schneier.com/passsafe.html
http://passwordsafe.sourceforge.net/
Read this: If you type A-E-S into your code, you're doing it wrong.

Categories