PyCrypto: Generate RSA key protected with DES3 password

2024/10/6 14:36:25

I have been able to create a RSA key protected by password with DES3 (well... I think because I'm very new to this encryption world) by using the command:

openssl genrsa -out "/tmp/myKey.pem" -passout pass:"f00bar" -des3 2048

Now, I would like to do that inside a Python script, using PyCrypto, if possible. I have seen this message, which seems to discourage the use of PyCrypto to do that. Is it still like that?

Of course I can always call os.execute, and execute the above command, but I'd consider that "cheating" :-). I'm pretty much doing this to learn PyCrypto.

Thank you in advance.

Answer

Starting from PyCrypto 2.5 you can export an RSA private key and have it protected under a passphrase. A Triple DES key is internally derived from the passphrase and used to perform the actual encryption.

For instance:

from Crypto import RSA
from Crypto import Randomrandom_generator = Random.new().read
key = RSA.generate(1024, random_generator)
exportedKey = key.exportKey('PEM', 'my secret', pkcs=1)

The variable exportedKey contains an ASCII version (PEM) of the key, encoded according to PKCS#1 (a cryptographic standard. Another option is pkcs=8 for - guess what - PKCS#8). Since the result is standard, you can use it with several other programs, including openssl. And of course, you can also re-import it back into python via PyCrypto!

The exportKey method is documented here.

https://en.xdnf.cn/q/70355.html

Related Q&A

Normalize/Standardize a numpy recarray

I wonder what the best way of normalizing/standardizing a numpy recarray is. To make it clear, Im not talking about a mathematical matrix, but a record array that also has e.g. textual columns (such as…

How to read /dev/log?

I would like to directly access to syslog messages from Python by reading /dev/log.My (very limited) understanding is that the correct way is to read from there is to bind a datagram socket. import soc…

Determining if a number evenly divides by 25, Python

Im trying to check if each number in a list is evenly divisible by 25 using Python. Im not sure what is the right process. I want to do something like this:n = [100, 101, 102, 125, 355, 275, 435, 134, …

OpenCV SimpleBlobDetector detect() call throws cv2.error: Unknown C++ exception from OpenCV code?

I need to detect semicircles on image and I find follow stuff for this: import cv2 import numpy as npdef get_circle(img_path):im = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)detector = cv2.SimpleBlobDet…

Is it possible to add a global argument for all subcommands in Click based interfaces?

I am using Click under a virtualenv and use the entry_point directive in setuptools to map the root to a function called dispatch.My tool exposes two subcommands serve and config, I am using an option …

Error importing h5py

Ive been trying to import h5py to read this type of file.Here is my code:import h5pyfile_1 = h5py.File("Out_fragment.h5py")print file_1The output is:Traceback (most recent call last):File &qu…

Tensorflow leaks 1280 bytes with each session opened and closed?

It seems that each Tensorflow session I open and close consumes 1280 bytes from the GPU memory, which are not released until the python kernel is terminated. To reproduce, save the following python scr…

Python Pandas -- Forward filling entire rows with value of one previous column

New to pandas development. How do I forward fill a DataFrame with the value contained in one previously seen column?Self-contained example:import pandas as pd import numpy as np O = [1, np.nan, 5, np.…

Microsoft Visual C++ 14.0 is required - error - pip install fbprophet

I am trying pip install fbprophet. I am getting that error: "Microsoft Visual C++ 14.0 is required" It has been discussed many times (e.g. Microsoft Visual C++ 14.0 is required (Unable to fin…

find position of item in for loop over a sequence [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:Accessing the index in Python for loops list = [1,2,2,3,5,5,6,7]for item in mylist:...How can I find the index of the item…