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:
- You have a string data.
- You generate a hash of a passphrase.
- 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?
Thanks
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)):
self.result_l.append(chr(ord(self.edobject[i])+ord(key.hash_key()[i])))
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)):
self.result_l.append(chr(ord(self.edobject[i])-ord(key.hash_key()[i])))
return ''.join(self.result_l)
if __name__ == '__main__':
o = EDObject(input('object:'))
k = Key(input('pass:'))
if input('e/d:') == 'e':
#Encrypt
print(o.encrypt(k))
else:
#Decrypt
print(o.decrypt(k))