3

i know how to save a users input to a text file but how do i encrypt it? here is what i have for saving a users input to text file. i tried f.encrypt("Passwords_log.txt" but had no results

import time
password1 = input("Please type a password: ")
print("Your password has passed the verification!")
time.sleep(1)
print("Saving and encrypting password...")
time.sleep(2)
f=open("Passwords_log.txt",'a')
f.write(password)
f.write('\n')
f.close()
print("Done!")
theRumblers100
  • 81
  • 1
  • 2
  • 5
  • Where did you see `file.encrypt`? :) You can read more about [File objects](https://docs.python.org/2/library/stdtypes.html#bltin-file-objects) in the Python docs. – André Laszlo Jul 01 '15 at 12:26
  • Do you want to encrypt the password and save it to the file, or save the password to the file, and then encrypt the file? – Avión Jul 01 '15 at 12:38

4 Answers4

4

There are some Python packages worth checking out that deal with cryptography.

Cryptography

PyCrypto

A simple example from cryptography would be the following:

from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")
plain_text = cipher_suite.decrypt(cipher_text)
Steven Correia
  • 461
  • 10
  • 18
0

I guess you want to get some sort of a hash for the password, but file objects have nothing to do with that. You may try to use base64 encoding (like here), or any other other algorithm of the kind.

Your code:

import time
import base64
password1 = raw_input("Please type a password: ")
print("Your password has passed the verification!")
time.sleep(1)
print("Saving and encrypting password...")
time.sleep(2)
f=open("Passwords_log.txt",'a')
password = base64.b64encode(password)
f.write(password)
f.write('\n')
f.close()
print("Done!")
Community
  • 1
  • 1
oopcode
  • 1,912
  • 16
  • 26
0

check out password hashing.

then i'd suggest you use https://pythonhosted.org/passlib/ or pycrypto; depending on what alorithm you choose.

this is just to store the encrypted password. to then encrypt data have a look at https://pypi.python.org/pypi/pycrypto/2.6.1.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
0

You said, you tried base64 but it didn't work. Here is how you can make it work:

import base64
import time
password1 = input("Please type a password: ")
print("Your password has passed the verification!")
time.sleep(1)
print("Saving and encrypting password...")
time.sleep(2)
f=open("Passwords_log.txt",'a')
cr = base64.encodestring(password1)
f.write(cr)
f.write('\n')
f.close()
print("Done!")

This is not real encryption, I wouldn't recommend it for passwords, but since you said in your comment that you tried to use base64 and it didn't work, I thought I should show you how to use base64 in your code.

Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48
  • Just like @Joe R said, Python might not be the best choice for encryption. See this answer from crypto.stackexchange for more details. http://crypto.stackexchange.com/a/19969 – Steven Correia Jul 01 '15 at 13:10