Skip to content

Instantly share code, notes, and snippets.

@princebot
Created November 6, 2016 03:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save princebot/a8fbe289843a268d13ff2f20fc3606e7 to your computer and use it in GitHub Desktop.
Save princebot/a8fbe289843a268d13ff2f20fc3606e7 to your computer and use it in GitHub Desktop.
Convert private keys saved in LastPass as SSH Key Secure Notes back to a usable format
#!/usr/bin/env python
"""Convert private keys saved in LastPass back to a usable format.
1. Copy-paste key from Secure Note to a local file.
2. Run this script passing the file as an argument.
3. Formatted key prints to stdout.
Example:
python rebuild_key.py ~/.ssh/keys/why_lastpass_why > ~/.ssh/keys/fixed
"""
import re
import sys
keyfile = sys.argv[1]
regex = re.compile(r'(-{5}|,ENCRYPTED|DEK-Info: \S+|\S{64})( +)')
with open(keyfile) as f:
text = regex.sub(r'\1\n', f.read())
lines = []
for line in text.split('\n'):
if not line.strip():
continue
if line.startswith('DEK-Info'):
line = line + '\n'
lines.append(line)
key = '\n'.join(lines)
print(key)
@dancwilliams
Copy link

Works great! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment