Miscellaneous crypto High-Level Classes

230 cipher.updatedata return cipher.final def decryptpassword, data, alg: cipher = EVP.Cipheralg, password, None, 0, 1, sha1 cipher.updatedata return cipher.final password = any password will do plaintext = Hello, world ciphertext = encryptpassword, plaintext, bf-cbc print Decrypted message text: s decryptpassword, ciphertext, bf-cbc The EVP module also provides a PKey class that is intended to be a wrapper around the OpenSSL EVP interface for digital signatures and data encryption; however, it is incomplete, providing only limited support for creating digital signatures. No mechanism exists for verifying digital signatures or data encryption in this class. The digital signature support is also nonfunctional. The class is essentially useless in its current form, and so we will not discuss it in any more depth here.

9.2.2.4 Miscellaneous crypto

from M2Crypto import DH, DSA, RSA, RC4 The DH, DSA, and RSA modules provide access to the three supported low-level, public key cryptographic algorithms known by the same names. The RC4 module provides direct access to the symmetric cipher by the same name. Its curious that RC4 is the only symmetric cipher that is supported directly with a class of its own, particularly since the EVP interface is exposed. We recommend that you avoid using it in favor of the EVP modules Cipher class. The DH module provides a class by the same name that is generally instantiated by using one of the four functions provided by the module. The function DH.gen_params can be used to create a new DH object with randomly generated parameters. The functions DH.load_params and DH.load_params_bio can be used to create a DH object created from parameters stored in a file. DH.load_params accepts a filename from which the parameters will be loaded, and DH.load_params_bio accepts a BIO object from which the parameters will be loaded. Finally, DH.set_params allows you to create a DH object and specify the parameters yourself. The DSA module provides a class by the same name that is generally instantiated by one of several module functions. The function DSA.gen_params can be used to create a new DSA object with randomly generated parameters. DSA.load_params and DSA.load_params_bio create a DSA object from a file or a BIO object. DSA.load_key and DSA.load_key_bio create a DSA object loaded from a file or BIO object containing a PEM representation of a private key. There is no mechanism to load public DSA keys. The RSA module provides two classes: RSA and RSA_pub . The classes should be instantiated using one of the modules functions. RSA.gen_key returns an RSA object after generating a new key pair. RSA.load_key and RSA.load_key_bio both create an RSA object from a private key stored in PEM format from a file or BIO object. RSA.load_pub_key and RSA.load_pub_key_bio create an RSA_pub object from a public key stored in PEM representation from a file or BIO object. Finally, RSA.new_pub_key will instantiate an RSA_pub object from the public exponent and composite of the primes that make up a private key. The RC4 module provides an RC4 class as an interface to the RC4 symmetric cipher algorithm. This class is intended to be instantiated directly. It can be instantiated with or without a key, and 231 the key can be changed with a call to its set_key method. Calling the update method with data to be encrypted will return the encrypted data.

9.2.3 Python Module Extensions