Package Crypto :: Package PublicKey :: Module RSA
[hide private]
[frames] | no frames]

Module RSA

RSA public-key cryptography algorithm.
Classes [hide private]
  error
  _RSAobj
Class defining an actual RSA key.
  RSAImplementation
An RSA key factory.
Functions [hide private]
 
generate(bits, randfunc=None, progress_func=None, e=65537)
Randomly generate a fresh, new RSA key object.
 
construct(tup)
Construct an RSA key object from a tuple of valid RSA components.
 
importKey(externKey, passphrase=None)
Import an RSA key (public or private half), encoded in standard form.
Variables [hide private]
  algorithmIdentifier = '0\r\x06\t*\x86H\x86\xf7\r\x01\x01\x01\x...
This is the ASN.1 DER object that qualifies an algorithm as compliant to PKCS#1 (that is, the standard RSA).
  __package__ = 'Crypto.PublicKey'
Function Details [hide private]

generate(bits, randfunc=None, progress_func=None, e=65537)

 

Randomly generate a fresh, new RSA key object.

See RSAImplementation.generate.

Parameters:
  • bits (int) - Key length, or size (in bits) of the RSA modulus.

    It must be a multiple of 256, and no smaller than 1024.

  • randfunc (callable) - Random number generation function; it should accept a single integer N and return a string of random data N bytes long.
  • progress_func (callable) - Optional function that will be called with a short string containing the key parameter currently being generated; it's useful for interactive applications where a user is waiting for a key to be generated.
  • e (int) - Public RSA exponent. It must be an odd positive integer.

    It is typically a small number with very few ones in its binary representation.

    The default value 65537 (= 0b10000000000000001 ) is a safe choice: other common values are 5, 7, 17, and 257.

Raises:
  • ValueError - When bits is too little or not a multiple of 256, or when e is not odd or smaller than 2.
Attention:
  • You should always use a cryptographically secure random number generator, such as the one defined in the Crypto.Random module; don't just use the current time and the random module.
  • Exponent 3 is also widely used, but it requires very special care when padding the message.

construct(tup)

 

Construct an RSA key object from a tuple of valid RSA components.

See RSAImplementation.construct.

Parameters:
  • tup (tuple) - A tuple of long integers, with at least 2 and no more than 6 items. The items come in the following order:

    1. RSA modulus (n).
    2. Public exponent (e).
    3. Private exponent (d). Only required if the key is private.
    4. First factor of n (p). Optional.
    5. Second factor of n (q). Optional.
    6. CRT coefficient, (1/p) mod q (u). Optional.

importKey(externKey, passphrase=None)

 

Import an RSA key (public or private half), encoded in standard form.

See RSAImplementation.importKey.

Parameters:
  • externKey (string) - The RSA key to import, encoded as a string.

    The key can be in any of the following formats:

    • DER + PKCS#1 (binary)
    • PEM + PKCS#1 (textual, according to RFC1421/3)
    • DER + PKCS#8 (binary, private key only)
    • PEM + PKCS#8 (textual, according to RFC5208, private key only)
    • OpenSSH (textual public key only)

    In case of PEM + PKCS#1, the key can be encrypted with DES or 3TDES according to a certain pass phrase. Only OpenSSL-compatible pass phrases are supported.

  • passphrase (string) - In case of an encrypted PEM key, this is the pass phrase from which the encryption key is derived.
Raises:
  • ValueError/IndexError/TypeError - When the given key cannot be parsed (possibly because the pass phrase is wrong).

Variables Details [hide private]

algorithmIdentifier

This is the ASN.1 DER object that qualifies an algorithm as compliant to PKCS#1 (that is, the standard RSA).
Value:
'0\r\x06\t*\x86H\x86\xf7\r\x01\x01\x01\x05\x00'