RSA¶
- class pyflocker.ciphers.backends.cryptography_.RSA.RSAPrivateKey(n: int | None, e: int = 65537, _key: RSAPrivateKey | None = None)[source]¶
Bases:
BaseRSAPrivateKey
- public_key() RSAPublicKey [source]¶
Creates a public key from the private key.
- Returns:
The RSA public key.
- decryptor(padding: BaseAsymmetricPadding | None = None) DecryptorContext [source]¶
Creates a decryption context.
- Parameters:
padding – The padding to use. Default is OAEP.
- Returns:
object for decrypting ciphertexts.
- signer(padding: BaseAsymmetricPadding | None = None) SignerContext [source]¶
Create a signer context.
- Parameters:
padding – The padding to use. Default is PSS.
- Returns:
Signer object for signing messages.
Note
If the padding is PSS and
salt_length
is None, the salt length will be maximized, as in OpenSSL.
- serialize(encoding: str = 'PEM', format: str = 'PKCS8', passphrase: bytes | None = None) bytes [source]¶
Serialize the private key.
- Parameters:
encoding – PEM or DER (defaults to PEM).
format –
The formats can be:
PKCS8 (default)
TraditionalOpenSSL
OpenSSH (available from pyca/cryptography version >=3.X)
PKCS1 (alias to TraditionalOpenSSL for Cryptodome compat)
passphrase – A bytes-like object to protect the private key. If
passphrase
is None, the private key will be exported in the clear!
- Returns:
The private key as a bytes object.
- Raises:
ValueError – if the format or encoding is invalid or not supported.
- classmethod load(data: bytes, passphrase: bytes | None = None) RSAPrivateKey [source]¶
Loads the private key as bytes object and returns the Key interface.
- Parameters:
data – The key as bytes object.
passphrase – The passphrase that deserializes the private key. It must be a bytes-like object if the key was encrypted while serialization, otherwise
None
.
- Returns:
RSA private key.
- Raises:
ValueError – if the key could not be deserialized.
- class pyflocker.ciphers.backends.cryptography_.RSA.RSAPublicKey(key: RSAPublicKey)[source]¶
Bases:
BaseRSAPublicKey
- encryptor(padding: BaseAsymmetricPadding | None = None) EncryptorContext [source]¶
Creates a encryption context.
- Parameters:
padding – The padding to use. Defaults to OAEP.
- Returns:
object for encrypting plaintexts.
- verifier(padding: BaseAsymmetricPadding | None = None) VerifierContext [source]¶
Creates a verifier context.
- Parameters:
padding – The padding to use. Defaults to PSS.
- Returns:
verifier object for verification.
- serialize(encoding: str = 'PEM', format: str = 'SubjectPublicKeyInfo') bytes [source]¶
Serialize the public key.
- Parameters:
encoding – PEM, DER or OpenSSH (defaults to PEM).
format –
The supported formats are:
SubjectPublicKeyInfo (default)
PKCS1
OpenSSH
- Returns:
Serialized public key as bytes object.
- Raises:
ValueError – if the encoding or format is incorrect or unsupported.
- classmethod load(data: bytes) RSAPublicKey [source]¶
Loads the public key as
bytes
object and returns the Key interface.- Parameters:
data – The key as bytes object.
- Returns:
The RSA public key.
- Raises:
ValueError – if the key could not be deserialized.
- class pyflocker.ciphers.backends.cryptography_.RSA.EncryptorContext(key: rsa.RSAPublicKey, padding: _padding.AsymmetricPadding)[source]¶
Bases:
BaseEncryptorContext
- class pyflocker.ciphers.backends.cryptography_.RSA.DecryptorContext(key: rsa.RSAPrivateKey, padding: _padding.AsymmetricPadding)[source]¶
Bases:
BaseDecryptorContext
- decrypt(ciphertext: bytes) bytes [source]¶
Decrypts the ciphertext and returns the plaintext.
- Parameters:
ciphertext – The ciphertext to decrypt.
- Returns:
The plaintext.
- Raises:
DecryptionError – if the decryption was not successful.
- class pyflocker.ciphers.backends.cryptography_.RSA.SignerContext(key: rsa.RSAPrivateKey, padding: _padding.AsymmetricPadding)[source]¶
Bases:
BaseSignerContext
- class pyflocker.ciphers.backends.cryptography_.RSA.VerifierContext(key: rsa.RSAPublicKey, padding: _padding.AsymmetricPadding)[source]¶
Bases:
BaseVerifierContext
- pyflocker.ciphers.backends.cryptography_.RSA.generate(bits: int, e: int = 65537) RSAPrivateKey [source]¶
Generate a private key with given key modulus
bits
and public exponente
(default 65537). Recommended size ofbits
> 1024.- Parameters:
bits – The bit length of the RSA key.
e – The public exponent value. Default is 65537.
- Returns:
The RSA private key.
- Return type:
- pyflocker.ciphers.backends.cryptography_.RSA.load_public_key(data: bytes) RSAPublicKey [source]¶
Loads the public key and returns a Key interface.
- Parameters:
data – The public key (a bytes-like object) to deserialize.
- Returns:
The RSA public key.
- Return type:
- pyflocker.ciphers.backends.cryptography_.RSA.load_private_key(data: bytes, passphrase: bytes | None = None) RSAPrivateKey [source]¶
Loads the private key and returns a Key interface.
- Parameters:
data – The private key (a bytes-like object) to deserialize.
passphrase – The passphrase that was used to encrypt the private key.
None
if the private key is not encrypted.
- Returns:
The RSA private key.
- Return type: