9. windows.crypto – CryptoAPI¶
The windows.crypto module offers some wrappers arround the CryptoAPI.
The main goal of this module (for now) is providing simple encryption/decryption methods.
9.1. Encryption¶
Note
See sample Encryption demo
9.1.1. encrypt¶
-
windows.crypto.encrypt(cert_or_certlist, msg, algo=szOID_RSA_DES_EDE3_CBC('1.2.840.113549.3.7'), initvector=GenerateInitVector())[source]¶ Encrypt
msgwith the certificate(s) incert_or_certlistusingalgowith the initial vectorinitvector.If
geninitvectoris left as it is, it will generate a random one.Algorithms supported by
GenerateInitVectorare:szOID_OIWSEC_desCBCszOID_RSA_DES_EDE3_CBCszOID_NIST_AES128_CBCszOID_NIST_AES192_CBCszOID_NIST_AES256_CBC
Returns: bytearray: The encrypted message
9.1.2. decrypt¶
9.1.3. import_pfx¶
-
windows.crypto.import_pfx(pfx, password=None, flags=36864L)[source]¶ Import the file
pfxwith thepassword.default flags = PKCS12_NO_PERSIST_KEY | CRYPT_USER_KEYSET.PKCS12_NO_PERSIST_KEYtellsCryptoAPIto NOT save the keys in a on-disk container.Returns: EHCERTSTORE
9.2. Certificate¶
Warning
The classes described here are under heavy test and try.
The classes, methods and properties described here will problably change as I haven’t yet had the time to try it out in real cases and figure out the final look of the API I want to do.
Note
See sample Certificate demo
9.2.1. CertificateContext¶
-
class
windows.crypto.CertificateContext[source]¶ Represent a Certificate.
note: It is a pointer ctypes structure (
PCCERT_CONTEXT)-
chains¶ The list of chain context available for this certificate. Each elements of this list is a list of
CertificateContextthat should go from theselfcertificate to a trusted certificate.Type: [[ CertificateContext]] – A list of chain (list) ofCertificateContext
-
contents¶ the object this pointer points to (read-write)
-
duplicate()[source]¶ Duplicate the certificate by incrementing the internal refcount. (see
CertDuplicateCertificateContext)note: The object returned is
selfReturns: CertificateContext
-
classmethod
from_buffer(data)[source]¶ Create a
CertificateContextfrom the bufferdataReturns: CertificateContext
-
classmethod
from_file(filename)[source]¶ Create a
CertificateContextfrom the filefilenameReturns: CertificateContext
-
get_name(nametype=CERT_NAME_SIMPLE_DISPLAY_TYPE(0x4L), flags=0)[source]¶ Retrieve the subject or issuer name of the certificate. See
CertGetNameStringAReturns: str
-
store¶ The certificate store that contains the certificate
Type: EHCERTSTORE
-
version¶ TODO: doc
-
9.2.2. EHCERTSTORE¶
-
class
windows.crypto.EHCERTSTORE[source]¶ A certificate store
-
certs¶ The certificates in the store
Type: [ CertificateContext] – A list of Certificate
-
find(issuer, serialnumber)[source]¶ Return the certificate that match issuer and serialnumber
Returns: CertificateContext
-
classmethod
from_file(filename)[source]¶ Create a new
EHCERTSTOREfromfilename
-
classmethod
from_system_store(store_name)[source]¶ Create a new
EHCERTSTOREfrom system store``store_name`` (see https://msdn.microsoft.com/en-us/library/windows/desktop/aa388136(v=vs.85).aspx)
-
classmethod
new_in_memory()[source]¶ Create a new temporary
EHCERTSTOREin memory
-
value¶ current value
-
9.2.3. CryptObject¶
9.3. Generating componants¶
This module is used to generate selfsigned-certificates / keypair and pfx file.
Note
See genkeys() in the sample Encryption demo
9.3.1. generate_selfsigned_certificate¶
-
windows.crypto.generation.generate_selfsigned_certificate(name='CN=DEFAULT', prov=None, key_info=None, flags=0, signature_algo=None)[source]¶ Generate a selfsigned certificate.
See https://msdn.microsoft.com/en-us/library/windows/desktop/aa376039(v=vs.85).aspx
Returns: windows.crypto.CertificateContext