A simple tool to generate RSA keys and use them to encrypt/decrypt messages.
Project description
Example Package as library
This is a simple package to encrypt and decrypt strings using RSA keys.
A possible application can be to encrypt a password when parakamiko library is used to make a SSH connection with password. Although is preferable to make a key without password to the SSH connection, sometimes this method does not work, for example if you need to make a connection to localhost
As example:
This code is not recommend due to the password is going in plain text
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='localhost', username='user', password='12345678')
client.exec_command('ls', timeout=5)
This code is better:
- First generate the encrypted password in a file.:
from passwd_encrypt.passwd_encrypt import pw_create_rsa_key, pw_encrypt_msg
# First you generate a couple of RSA keys and then encrypt the password.
pw_create_rsa_key("mykey")
public_key="mykey.pem.pub"
pw_encrypt_msg(public_key, '12345678')
# The password is stored in a cyphered file named "passwd_encrypted.bin"
- Now in the code using paramiko to stablish a SSH connection, use the following commands:
import paramiko
from passwd_encrypt.passwd_encrypt import pw_decrypt_msg
# Get the pass_decrypt
private_key="mykey.pem"
pass_decrypt = pw_decrypt_msg(private_key, "passwd_encrypted.bin")
# SSH connection
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='localhost', username='user', password=pass_decrypt)
client.exec_command('ls', timeout=5)
The important here is to get the generated private_key in a safe place. Using this library you can distibute a source code with SSH connection based on passwords without to reveal the password in the code.
Example Package as program from the CLI
The program can also be use from the CLI (command line interface) as follows:
- Activate the python enviroment (if needed)
- Get the usage or help from the program
python passwd_encrypt.py -u
======== Crypto Password: Start Job ========
#############################################
# PASSWD_ENCRYPT #
# Dr. Javier Ramos #
# Macromolecular Physics Department #
# IEM-CSIC #
# https://github.com/jrdcasa #
#############################################
Usage:
passwd_encrypt [--createkeys|-c] <pattern> # Generate the private and public keys as <pattern>.pem and <pattern>.pem.pub, respectively in the working directory.
passwd_encrypt [--encrypt|-e] <rsa public key file> <message to encrypt> # Encrypt message with the public key.
passwd_encrypt [--decrypt|-d] <rsa private key file> <string or file containing the message to decrypt> # Decrypt message with the public key.
======== Crypto Password: Job Done =========
A example is available in the recipes folder as jupyter notebook.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file passwd_encrypt-0.2.4.tar.gz
.
File metadata
- Download URL: passwd_encrypt-0.2.4.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f8aea70b73ce0bd96ed44ed95feb9abf9097feae335c14f8300fd4bf771d14d |
|
MD5 | 280f83ce3f077f32e8e043beccc23379 |
|
BLAKE2b-256 | 3daba7675f8c620f76db757a853ee86db3b17bf7e7b54091cae50324eb599ce1 |
File details
Details for the file passwd_encrypt-0.2.4-py3-none-any.whl
.
File metadata
- Download URL: passwd_encrypt-0.2.4-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55c3b221f5946aaff9269043f29258688579d5cee66f40fd58ceb39e2d52e7b3 |
|
MD5 | 7e69b840c8f52591d35d014d064f6966 |
|
BLAKE2b-256 | a55767e17d0137b6e0621d2716fd0904c81bc1fc908168c322486e866a1d3802 |