Lately, I've been reading about cryptography and thought of a method of using hashes to encrypt data. From what I know, hashes are unique to each object and are one way functions. Encryption on the other hand is two way. Encryption relies on a key to decrypt the encrypted data. This key is the key (no pun intended) to the strength of the encryption. How it is created is everything. What I am wondering is could one could modify the data using a hash of a passphrase, to create a secure encryption? For example:

  1. You have a string data.
  2. You generate a hash of a passphrase.
  3. You combine the corresponding characters of the hash and data together i.e

[data = asdfgh] + [hash = qwertyuiop] ---> result=ÒêÉØÛá

Note: This is just an example of how you could use the hash to modify the data. The main point is that you modify the data in any simple way with the hash: addition, subtraction, ORing, etc. You get the point.

After you encrypt it, you can throw out the hash. To decrypt it, all you have to do is to reverse the operation using the same hashing function and salt. All one needs is the passphrase used for the original hash.

This seems like it would work, because if the hashes are truly unique, the only way, it seems, without brute forcing, to decrypt the data would be to use the same hash as what you used to encrypt it.

This seems too simple though, and since I am a noob in this area, I'm sure there are security flaws. Is there anything inherently insecure about this?


In case you want actual code for it, here is a quick python program that demonstrates the just of it:

#Encryption Test
import hashlib

class Key:
    def __init__(self, passphrase):
        self.passphrase = passphrase.encode('utf-8')
        self.salt = 'battery horse staple'.encode('utf-8')
    def hash_key(self):
        return hashlib.sha256(self.passphrase + b';' + self.salt).hexdigest()

class EDObject:
    def __init__(self, edobject):
        self.edobject = edobject
        self.result_l = []
    def encrypt(self, key):
        #Add the decimal values of key (hash) and object together.
        for i in range(len(self.edobject)):
        return ''.join(self.result_l)
    def decrypt(self, key):
        #subtract the decimal values of the key from the encrypted object
        for i in range(len(self.edobject)):
        return ''.join(self.result_l)

if __name__ == '__main__':
    o = EDObject(input('object:'))
    k = Key(input('pass:'))
    if input('e/d:') == 'e':
  • Hashes aren't truly unique. They can only be collision-resistant. It's just that the time it will take you to find a colliding object is very, very long. Commented Jan 29, 2016 at 5:06
  • 3
    I think you would get a better audience for a question like this at crypto.stackexchange.com Commented Jan 29, 2016 at 5:29

2 Answers 2


If I understand your approach and code correctly then you simply don't use the original passphrase for "encryption" but a key derived from passphrase and a static salt. This approach of not using the original passphrase but a derived key is also called key stretching using a key derivation function. The purpose is to make a "better" key by hiding a not so random passphrase behind complex operations which purposely take a long time and thus make brute forcing the original passphrase harder. The derived key is then used for encryption, but still with a good encryption algorithm.

Unfortunately the way you have used these ideas is wrong. You are not using the derived key in a strong encryption but only in a simple operation like XOR. This simple kind of "encryption" makes it easy to get the encryption key (i.e. the key derived from passphrase and salt) if enough encrypted data are known or if a few plain text is known. Once the attacker has this derived key it can decrypt all data, i.e. there is no need to find out the original passphrase and salt used for creating this key.

Apart from that your key derivation function is a fast hash which would make brute forcing easier. But again, one don't even need to reverse your key derivation to decrypt the content.


The key feature of hashes are that they are irreversible, so using this method to "decrypt" data would leave you in the same position that your adversaries are, which is brute-forcing the message.

You might argue that you have the upper-hand in that brute-force competition by "knowing" the password that was used in the hash as a "salt" of sorts, but if you could secure transmit that salt to the target endpoint securely, why not use that secure channel for the actual data?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .