I have a PythonNet C# bytes[]
object and I need to convert it to Python's bytes
.
Is there a way to do this using Python?
Here is what I'm doing to get the bytes. I need to go into the winstore's credential store and export out a client certificate.
import clr, os
import requests
from cryptography.hazmat.primitives.serialization.pkcs12 import (
load_key_and_certificates,
)
from cryptography.hazmat.primitives.serialization import Encoding, NoEncryption
from cryptography.hazmat.backends import default_backend
clr.AddReference("System")
clr.AddReference("System.Security.Cryptography.X509Certificates")
clr.AddReference("System.Security.Cryptography")
from System.Security.Cryptography.X509Certificates import (
X509Store,
StoreName,
StoreLocation,
OpenFlags,
X509CertificateCollection,
X509FindType,
X509Certificate2,
X509ContentType,
)
store = X509Store(StoreName.My, StoreLocation.CurrentUser)
store.Open(OpenFlags.ReadOnly)
serial_number = (
'SUPER SECRET SERIAL NUMBER'
)
collection = store.Certificates.Find(
X509FindType.FindBySerialNumber, serial_number, False
)
cert = collection.get_Item(0)
pkcs12 = cert.Export(X509ContentType.Pkcs12, "<Secret Password>")
# I have System.Bytes[] need python bytes to use with pyopenssl/cryptography
This code is using the pythonnet
library to access the crypto API on windows. It then dumps a client certificate, found by a serial number, to bytes. When the clr
library returns the value, it's a system.Bytes[]
. This is incompatible with other libraries, so I need a way to convert this object to Python bytes.