I'm programming an decryption on a file with sign-in information. When I decrypt it, I end up with the info in a string. This is the piece of code:
decrypted_message = encryption_type.decrypt(original)
testing = decrypted_message.decode("utf-8")
print("test: ", testing)
The output is:
test: ip = "192.168.xxx.xxx", username = "xxx", password = "xxx"
How do i get the ip, username and password out off this string?
I can still change the output if it needs to be changed for a certain method. Also I use the decode because otherwise I end up with b' before the output (which I know is bytes but I don't know how to use).
CodePudding user response:
So I changed the plaintext before encryption to a json:
{"ip": "192.168.xx.xx", "username": "xx", "password": "xx"}
Now that it's a json, I was able to print the variables after a parse.
settings = json.loads(testing)
print(settings["ip"])
So I now have a solution for my project.
CodePudding user response:
If your string is literally as shown in your question the:
s = 'ip = "192.168.xxx.xxx", username = "xxx", password = "xxx"'
print(s.split()[2][1:-2])
CodePudding user response:
If your output really is a string, something like this would parse it:
a = 'ip = "192.168.xxx.xxx", username = "xxx", password = "xxx"'
parsed = dict(x.replace('"', "").split(" = ") for x in a.split(", "))
# {'ip': '192.168.xxx.xxx', 'username': 'xxx', 'password': 'xxx'}
But as noted in the comments you are much better off storing the data in json in the first place:
import json
s = json.dumps(dict(ip="192.168.1.1", username="me", pass="xx"))
encode(s) # dummy fn
decoded = json.loads(decode(s))
# decoded is a dict