What am I doing wrong here? I am trying to encrypt usernames and passwords but when I put them into json I get this error. Any help will be welcome! Sorry for any trouble! Python 3.7.0 I don't know the cryptography verson.
def editjson(location, value):
with open("config.json") as f:
data = f.read()
d = json.loads(data)
d[location] = [value]
with open("config.json", 'w') as f:
f.write(json.dumps(d))
def firstruntrue():
key = Fernet.generate_key()
f = Fernet(key)
userog = input("What is your Username? ").encode()
encrypteduser = f.encrypt(userog).decode()
passwordog = input("What is your Password? ").encode()
encryptedpassword = f.encrypt(passwordog).decode()
editjson("firstrun", "False")
editjson("user", encrypteduser)
editjson("password", encryptedpassword)
editjson("key", key)
with open("./config.json", "r") as handler:
info = json.load(handler)
user = info["user"]
password = info["password"]
firstrun = info["firstrun"]
C:\Users\USER\Desktop\Code>python -u C:\Users\USER\Desktop\Code\OpenGradebook.py
What is your Username? test
What is your Password? test
Traceback (most recent call last):
File "C:\Users\USER\Desktop\Code\OpenGradebook.py", line 36, in <module>
firstruntrue()
File "C:\Users\USER\Desktop\Code\OpenGradebook.py", line 27, in firstruntrue
editjson("key", key)
File "C:\Users\USER\Desktop\Code\OpenGradebook.py", line 15, in editjson
f.write(json.dumps(d))
File "C:\Program Files\Python37\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Program Files\Python37\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Program Files\Python37\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Program Files\Python37\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
From cryptography docs:
key
is in bytes and you serializing bytes objecteditjson("key", key)
.try changing to
editjson("key", key.decode())