Skip to content

Instantly share code, notes, and snippets.

@binhqd
Created July 5, 2023 04:30
Show Gist options
  • Save binhqd/05e90b9818b57aef4330527bf30ad93e to your computer and use it in GitHub Desktop.
Save binhqd/05e90b9818b57aef4330527bf30ad93e to your computer and use it in GitHub Desktop.
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64
private_key_path = "<fullpath>/.local/.crypto/id_rsa"
public_key_path = "<fullpath>/.local/.crypto/id_rsa.pub"
public_key = open(public_key_path, 'r')
pub_key = RSA.importKey(public_key.read())
cipher = PKCS1_OAEP.new(pub_key)
encrypted_msg = cipher.encrypt('SomePass'.encode('utf-8'))
encoded_encrypted_msg = base64.b64encode(encrypted_msg)
print(encoded_encrypted_msg.decode('utf-8'))
# Decode encrypted password
private_key = open(private_key_path, 'r')
prv_key = RSA.importKey(private_key.read())
cipher = PKCS1_OAEP.new(prv_key)
decoded_encrypted_msg = base64.b64decode(encoded_encrypted_msg)
decrypted_msg = cipher.decrypt(decoded_encrypted_msg)
print(decrypted_msg.decode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment