Is there a simple way to encrypt/decrypt a string with a key?
Something like:
key = \'1234\'
string = \'hello world\'
encrypted_string = encrypt(key, st
To expand on dsamersoff's answer.. This is simple and insecure, but for certain tasks may be useful. here's some code:
import crypt
import getpass
import os.path
def auth_func():
return (raw_input('Username:'), getpass.getpass('Password:'))
def xor(a,b):
assert len(b) >= len(a)
return "".join([chr( ord(a[i]) ^ ord(b[i])) for i in range(len(a))])
# create a new credentials file if needed
if not os.path.exists('cred'):
with open('cred', 'w') as f:
user, pwd = auth_func()
f.write ("{}\n".format(user))
f.write ("{}\n".format(xor(pwd, crypt.crypt('secret', 'words'))))
f.close()
# read credentials and print user/password
with open('cred', 'r') as f:
user, pwd = f.read().split('\n')[:2]
print user
print xor(pwd, crypt.crypt('secret', 'words'))