Python Forum
Trying to encrypt and decrypt password into a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to encrypt and decrypt password into a file
#1
Hi all,

I'm new to Python and probably I'm not going to be a good programmer. however, I have a use case that I try to complete:
I need a Python script that will ask me to type a password which will be encrypt and will place it into an external file.
I also need the decrypt as I'm going to use this bit for SSH using paramiko

I've search so much across the net, but didn't find how to do it. or else I'm just very bad at python and didn't get it :(

I came across this link which is very useful
https://www.thepythoncode.com/article/en...ric-python
but that's not exactly how I need it to work.

so here I am, trying to ask experts

btw I've tried the code below which is clearly not good
#!/usr/bin/env python3

print("Please type your new password.""\n")
text_file = open("sessionsdb.txt", "w")

value_list = []


for i in range(1):
    print("New password: ")
    line = input()
    value_list.append(line + "\n")


text_file.writelines(value_list)

text_file.close()

print("Your password has been encrypted.""\n")

from typing import List
file = open("sessionsdb.txt")
sessionsdb: List[str] = file.read().splitlines()

from cryptography.fernet import Fernet
key = b'PCHl_MjGyEyBxLYha3S-cWg_SDDmjT4YYaKYh4Z7Yug='
cipher_suite = Fernet(key)
ciphered_text = cipher_suite.encrypt(b'sessionsdb[0]')
unciphered_text = (cipher_suite.decrypt(ciphered_text))
print(unciphered_text)
print(ciphered_text)
Reply
#2
Please show your attempts and the errors that have been given so they can be reviewed.
"Often stumped... But never defeated."
Reply
#3
There is no error code really. it just won't call sessionsdb[0] at ciphered_text = cipher_suite.encrypt


Output:
Please type your new password. New password: typingmynewpassword Your password has been encrypted. b'sessionsdb[0]' b'gAAAAABfMQhPOwq8TwCrmtCVva1uzoaLunHgFU_9BsFQ_u2ROoibUdlkf7tv6999ycPGobDKzk4-GJCDHpmh1qD0f5y3in2EYg=='
Reply
#4
Someone? :)

I'd be grateful if you have a better method to make this work
Reply
#5
sessionsdb is a variable.
'sessionsdb' is a string and doesn't refer to the variable.
Likewise b'sessionsdb[0]' is just a string of bytes that has nothing to do with the sessionsdb variable.

If you want to encrypt the data in sessionsdb (and it has to be in bytes), then you could either read it as bytes directly in the first place (rather than reading it as UTF-8 text), or you could encode it.

So probably:
ciphered_text = cipher_suite.encrypt(sessionsdb[0].encode())
I'd imagine though that it would be better to just open the file in binary and do binary reads. Then you wouldn't have to encode it later.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 191 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Encrypt and decrypt in python using own fixed key SriRajesh 3 4,811 Feb-20-2022, 01:18 PM
Last Post: dboxall123
  Password Protection to the file San 0 1,427 Jun-09-2021, 02:00 PM
Last Post: San
  encrypt entire project mattc 2 2,415 Jul-21-2020, 07:05 AM
Last Post: mattc
  The code to decrypt Caeser Cipher. lazerwolf101 2 3,134 May-26-2020, 04:01 PM
Last Post: DT2000
  Paul Rubin p3.py lightweight encrypt/decrypt - is there a python3 version? mason28 0 1,599 Feb-19-2020, 03:38 AM
Last Post: mason28
  Zeep lib, encrypt soap body miha1234 0 2,841 Sep-12-2019, 07:52 AM
Last Post: miha1234
  How to decrypt Blowfish-encrypted text encrypted with Perl's Crypt::CBC? swechsler 0 2,167 Aug-15-2019, 05:24 PM
Last Post: swechsler
  Need help understanding a couple of functions (encrypt,decrypt, int_to_bytes) xoani 0 1,995 Jun-09-2019, 03:25 PM
Last Post: xoani
  PyPDF2 encrypt Truman 3 5,424 Jan-19-2019, 12:18 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020