I am looking for a python library for encrypting a string with a single key. I do not want the key to derivable by knowing the input and the output. I have came to cryptography.fernet so far, but I am not sure if this will fulfill my needs. Can the encryption key be derived by knowing the algorithm (assuming the fernet algorithm in this case) and knowing the input and the output?

For example when I encrypt message A with key K and it outputs B, can somehow A and B be put together, so it will output K?


The actual use of this is the following. I need to store a password in a place that others can see (a discord config channel, but that is secondary) in a way so they cannot see the actual password. I thought that the user would encrypt the password one-time with a key only visible to the bot (a discord bot) (with a command). The encrypted password would then be sent back to the user and stored in the channel for the bot to decrypt it and use it later. The problem is: if the user knows the input and the output, if he would be able to guess the key, he would then be able to decrypt all other passwords stored this way. And absolutely noone wants this to happen!

  • 1
    $\begingroup$ Welcome to Cryptography. What do you mean by reversible? What you want may be, given plaintexts-ciphertexts pairs, it should be almost impossible to derive the key. AES is known to be secure this kind of attacks, or you can use ChaCha. The library part is off-topic here. So your question is better, Can AES (or your symmetric cipher) be broken by known-plaintext attack? Or you can mention your real problem in better words. You can edit your question. $\endgroup$ – kelalaka Aug 29 '19 at 17:36
  • $\begingroup$ For passwords, I advise you to look at one of the top questions of information security How to securely hash passwords? $\endgroup$ – kelalaka Aug 29 '19 at 18:24
  • $\begingroup$ Thank you, but I did not plan on storing it in a database. I have to store them in the channel. I am doing an automation, which requires some passwords to be known. $\endgroup$ – Jakub Bláha Aug 29 '19 at 18:47

For example when I encrypt message A with key K and it outputs B, can somehow A and B be put together, so it will output K?

Except for encryption messages where you are specifically restricted to use a specifc key K only once (e.g. OTP), then no, it is infeasible to recover K from A and B.

This is a fairly fundamental requirement on encryption methods (known as "Known Plaintext"); that is, given a plaintext message and the corresponding ciphertext (or more generally, a huge number of messages and ciphertexts, all encrypted with the same key), that is still no help in decrypting another ciphertext (also encrypted with the same key) - being able to recover the key from a plaintext and ciphertext would violate this.

Actually, we put even tougher requirements on encryption methods. We allow the attacker to pick the messages to be encrypted ("Chosen Plaintext"), and also allow the attacker to submit his own ciphertexts (and see how they are decrypted), "Chosen Ciphertext".

An encryption method is generally not considered secure unless it is secure against all these attack scenarios.

| improve this answer | |
  • $\begingroup$ Thank you so much! Now I know that what am I planning to do is completely secure! $\endgroup$ – Jakub Bláha Aug 29 '19 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.