Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stdout output #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions BitwardenDecrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,11 @@ def decryptBitwardenJSON(options):
if(decryptedEntries.get('sends')):
decryptedEntries.move_to_end('sends')

return(json.dumps(decryptedEntries, indent=2, ensure_ascii=False))
return decryptedEntries


def write_json(decryptedEntries, file):
json.dump(decryptedEntries, file, indent=2, ensure_ascii=False)


def main(options):
Expand All @@ -588,24 +592,32 @@ def main(options):
else:
print(f"Saving Output To: {options.outputfile}\n")

decryptedJSON = decryptBitwardenJSON(options)
decryptedEntries = decryptBitwardenJSON(options)

encoding = options.encoding
# Force UTF-8 on Windows unless otherwise configured
if encoding is None and sys.platform == 'win32' and os.getenv('PYTHONIOENCODING') is None:
encoding = 'utf-8'

if (options.outputfile):
try:
with open(options.outputfile, "w", encoding="utf-8") as file:
file.write(decryptedJSON)
with open(options.outputfile, "w", encoding=encoding) as file:
write_json(decryptedEntries, file)
except Exception as e:
print(f"ERROR: Writing to {options.outputfile}")

else:
print(decryptedJSON.encode("utf-8"))
if encoding is not None:
sys.stdout.reconfigure(encoding=encoding)
write_json(decryptedEntries, sys.stdout)


if __name__ == "__main__":
parser = argparse.ArgumentParser(allow_abbrev=False, description='Decrypts an encrypted Bitwarden data.json file.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("inputfile", nargs='?', default="data.json", help='INPUTFILE')
parser.add_argument("--includesends", help="Include Sends in the output.", action="store_true", default=False)
parser.add_argument("--output", metavar='OUTPUTFILE', action="store", dest='outputfile', help='Saves decrypted output to OUTPUTFILE')
parser.add_argument("--output-encoding", metavar='ENCODING', action="store", dest='encoding', help='Specify encoding to use for --output')
args = parser.parse_args()

main(args)
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Options:
--includesends Include Sends in the output.
--output OUTPUTFILE Write decrypted output to file.
Will overwrite contents if file exists.
--output-encoding ENCODING
Specify encoding to use for --output.
(defaults to the current locale encoding)
```
On Windows:
```
Expand All @@ -50,6 +53,9 @@ Options:
--includesends Include Sends in the output.
--output OUTPUTFILE Write decrypted output to file.
Will overwrite contents if file exists.
--output-encoding ENCODING
Specify encoding to use for --output
(defaults to 'utf-8')
```
*Note: This script depends on the 'cryptography' package
pip install cryptography*
Expand Down