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.
Related
I'm creating a website where users can write about their day in a sort of diary. Right now, if a user decides to write something even remotely personal I can immediately see that just by querying the database for their entry content. I was wondering what the best way would be to encrypt this diary entry I get from the user so it would at least take some more effort from myself to see what people are writing. I am using Flask and SQLAlchemy.
if its a matter of human readability, you could just base64 encode it - therefore obfuscating it, instead of encrypting it. This is included in the python standard library, here's an example:
https://www.base64encoder.io/python/
If it needs to be encrypted, you could assign the user a private key and encrypt the data with its corresponding public key. This may be overkill, unless you do not want anyone else to be able to read it, even with a lot of effort put into it. Obviously you need to keep those private keys secret to anyone except the user, which would be complicated and you would not be able to assist the users if they lose their keys.
I have a script A that needs to perform authentication on my professional email. Right now, I put that data in a python dict() in a script B that is imported into script A at runtime in an unencrypted form. The script B is not under version control.
This is only for a personal project, so I don't need to have very reusable code, but the authentication in question yields access to critical data.
Is importing unencrypted data at runtime as described in this answer secure?
No, not really.
Two notable issues.
First, if the attacker has access to your main source, finding the authentication information is quite straightforward.
Second, should someone stumble across the B file, there's your credentials laid out in plain sight.
Dealing with this is a key management problem, since what you want to do is encrypt the authentication information, but then you need a key, and you're back to square one. It's an intractable problem.
The game is burying the key.
The simplest solution is to simply prompt for the key (or the credentials) at startup, and not store it anywhere save in memory.
Or you can do a simple encryption of the credentials, store the key in a file, and read it in. This will defeat casual snooping (someone just looking at files), but nobody else.
So, it depends on what you feel your threat profile really is.
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.
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.
I'm learning socket programming (in python) and I was wondering what the best/typical way of encapsulating data is? My packets will be used to issue run, stop, configure, etc. commands on the receiving side. Is it helpful to use JSON or just straight text?
I suggest you use a fixed, or mostly fixed format, as this make things easier.
By then using features such as the standard library's struct.Struct, with its pack() and umpack() methods, or possibly a slightly more featured pacakges such as Construct, you should have much of the parsing work done for you ;-)
I suggest plain text to begin with - it is easier to debug. The format that your text takes depends on what you're doing, how many commands, arguments, etc. Have you fleshed out how your commands will look? Once you figure out what that looks like it'll likely suggest a format all on its own.
Are you using TCP or UDP? TCP is easy since it is a stream, but if you're using UDP keep in mind the maximum size of UDP packets and thus how big your message can be.
If you're developing something as a learning exercise you might find it best to go with a structured text (ie. human readable and human writable) format.
An example would be to use a fixed number of fields per command, fixed width text fields and/or easily parsable field delimiters.
Generally text is less efficient in terms of packet size, but it does have the benefits that you can read it easily if you do a packet capture (eg. using wireshark) or if you want to use telnet to mimic a client.
And if this is only a learning exercise then ease of debugging is a significant issue.
Take a look at how scapy (an awesome Python packet manipulation library) implements it. Looks like that have a handful of fields.