首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Bytes[]将PythonNet转换为PythonNet

使用Bytes[]将PythonNet转换为PythonNet
EN

Stack Overflow用户
提问于 2021-07-07 14:15:33
回答 1查看 430关注 0票数 0

我有一个PythonNet C# bytes[]对象,我需要将它转换为Python的bytes

有什么方法可以使用Python来完成这个任务吗?

下面是我要做的来获取字节。我需要进入酿酒厂的凭证商店并导出客户证书。

代码语言:javascript
复制
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字节的方法。

EN

回答 1

Stack Overflow用户

发布于 2022-01-21 11:25:36

您应该能够使用bytes函数(正如@JabberJabber所提到的那样)。以下内容适用于我(c_sharp_bytes实际上是C#函数的结果)和Python3.8:

代码语言:javascript
复制
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'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68287750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档