我有一个PythonNet C# bytes[]对象,我需要将它转换为Python的bytes。
有什么方法可以使用Python来完成这个任务吗?
下面是我要做的来获取字节。我需要进入酿酒厂的凭证商店并导出客户证书。
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此代码使用pythonnet库访问windows上的密码API。然后,它将由序列号找到的客户端证书转储为字节。当clr库返回值时,它就是一个system.Bytes[]。这与其他库不兼容,因此我需要一种将该对象转换为Python字节的方法。
发布于 2022-01-21 11:25:36
您应该能够使用bytes函数(正如@JabberJabber所提到的那样)。以下内容适用于我(c_sharp_bytes实际上是C#函数的结果)和Python3.8:
from System import Byte, Array
python_bytes = b'Some bytes'
# Python -> C#
c_sharp_bytes = Array[Byte](python_bytes) # <System.Byte[] object at 0x000001A33CC2A3D0>
# C# -> Python
python_bytes = bytes(c_sharp_bytes) # b'Some bytes'https://stackoverflow.com/questions/68287750
复制相似问题