Generate an RSA key¶ The following code generates a new RSA key pair (secret) and saves it into a file, protected by a password. We use the scrypt key derivation function to thwart dictionary attacks. At the end, the code prints our the RSA public key in ASCII/PEM format. The following are code examples for showing how to use Crypto.PublicKey.RSA.importKey.They are from open source Python projects. You can vote up the examples you like.
RSA is the most widespread and used public key algorithm. Its security isbased on the difficulty of factoring large integers. The algorithm haswithstood attacks for more than 30 years, and it is therefore consideredreasonably secure for new designs.
The algorithm can be used for both confidentiality (encryption) andauthentication (digital signature). It is worth noting that signing anddecryption are significantly slower than verification and encryption.
The cryptographic strength is primarily linked to the length of the RSA modulus n.In 2017, a sufficient length is deemed to be 2048 bits. For more information,see the most recent ECRYPT report.
Both RSA ciphertexts and RSA signatures are as large as the RSA modulus n (256bytes if n is 2048 bit long).
May 29, 2016 The most effective and fastest way is to use command line tools: codeopenssl genrsa -out mykey.pem 4096 openssl rsa -in mykey.pem -pubout mykey.pub /codeIt’ll generate RSA key pair in code mykey.pem/code and code mykey.pub/code. So we # call 'RSA.generate(bits)' which works on both pycrypto and pycryptodome # and then wrap it into a paramiko.RSAKey rsa = RSA.generate(bits) key = paramiko.RSAKey(vals=(rsa.e, rsa.n)) key.d = rsa.d key.p = rsa.p key.q = rsa.q return key.
The module Crypto.PublicKey.RSA provides facilities for generating new RSA keys,reconstructing them from known components, exporting them, and importing them.
As an example, this is how you generate a new RSA key pair, save it in a filecalled mykey.pem, and then read it back:
The algorithm closely follows NIST FIPS 186-4 in itssections B.3.1 and B.3.3. The modulus is the product oftwo non-strong probable primes.Each prime passes a suitable number of Miller-Rabin testswith random bases and a single Lucas test.
Parameters:
bits (integer) – Key length, or size (in bits) of the RSA modulus.It must be at least 1024, but 2048 is recommended.The FIPS standard only defines 1024, 2048 and 3072.
randfunc (callable) – Function that returns random bytes.The default is Crypto.Random.get_random_bytes().
e (integer) – Public RSA exponent. It must be an odd positive integer.It is typically a small number with very few ones in itsbinary representation.The FIPS standard requires the public exponent to beat least 65537 (the default).
Returns: an RSA key object (RsaKey, with private key).
’PEM’. (Default) Text encoding, done according to RFC1421/RFC1423.
’DER’. Binary encoding.
’OpenSSH’. Textual encoding, done according to OpenSSH specification.Only suitable for public keys (not private keys).
passphrase (string) – (For private keys only) The pass phrase used for protecting the output.
pkcs (integer) –
(For private keys only) The ASN.1 structure to use forserializing the key. Note that even in case of PEMencoding, there is an inner ASN.1 DER structure.
With pkcs=1 (default), the private key is encoded in asimple PKCS#1 structure (RSAPrivateKey).
With pkcs=8, the private key is encoded in a PKCS#8 structure(PrivateKeyInfo).
Note
This parameter is ignored for a public key.For DER and PEM, an ASN.1 DER SubjectPublicKeyInfostructure is always used.
protection (string) –
(For private keys only)The encryption scheme to use for protecting the private key.
If None (default), the behavior depends on format:
For ‘DER’, the PBKDF2WithHMAC-SHA1AndDES-EDE3-CBCscheme is used. The following operations are performed:
A 16 byte Triple DES key is derived from the passphraseusing Crypto.Protocol.KDF.PBKDF2() with 8 bytes salt,and 1 000 iterations of Crypto.Hash.HMAC.
The private key is encrypted using CBC.
The encrypted key is encoded according to PKCS#8.
For ‘PEM’, the obsolete PEM encryption scheme is used.It is based on MD5 for key derivation, and Triple DES for encryption.
Specifying a value for protection is only meaningful for PKCS#8(that is, pkcs=8) and only if a pass phrase is present too.
The supported schemes for PKCS#8 are listed in theCrypto.IO.PKCS8 module (see wrap_algo parameter).
randfunc (callable) – A function that provides random bytes. Only used for PEM encoding.The default is Crypto.Random.get_random_bytes().
Returns:
the encoded key
Return type:
byte string
Raises:
ValueError – when the format is unknown or when you try to encrypt a privatekey with DER format and PKCS#1.
Warning
If you don’t provide a pass phrase, the private key will beexported in the clear!
’PEM’. (Default) Text encoding, done according to RFC1421/RFC1423.
’DER’. Binary encoding.
’OpenSSH’. Textual encoding, done according to OpenSSH specification.Only suitable for public keys (not private keys).
passphrase (string) – (For private keys only) The pass phrase used for protecting the output.
pkcs (integer) –
(For private keys only) The ASN.1 structure to use forserializing the key. Note that even in case of PEMencoding, there is an inner ASN.1 DER structure.
With pkcs=1 (default), the private key is encoded in asimple PKCS#1 structure (RSAPrivateKey).
With pkcs=8, the private key is encoded in a PKCS#8 structure(PrivateKeyInfo).
Object ID for the RSA encryption algorithm. This OID often indicatesa generic RSA key, even when such key will be actually used for digitalsignatures.
-->
Hi all,
The other day a colleague of mine asked me if I had a .NETversion of the C++ sample in How to generate key pairs, encrypt and decrypt data with CryptoAPI post. C++ sample calls CryptoAPI directly (and you know we can do the same thing in .NET through P/Invoke), but the idea was to use System.Security classes in order to get a pure .NET solution. The answer is yes, I have such sample, and here it is:
If you compare both samples you will see that .NET simplifies the task a lot. But sometimes we won't be able to do with System.Security classes exactly the same we can do with CryptoAPI. So don't forget about the API just yet!