我需要将证书对象序列化为JSON格式,以下代码用于二进制转换。
static void Main()
{
string fileName = "Certificate.json";
IFormatter formatter = new BinaryFormatter();
Test.SerializeItem(fileName, formatter); // Serialize an instance of the class.
Test.DeserializeItem(fileName, formatter); // Deserialize the instance.
Console.WriteLine("Done");
}
public static void SerializeItem(string fileName, IFormatter formatter)
{
string Certificate = (@"\Certificate.cer");
// Create an instance of the type and serialize it.
X509Certificate2 x509 = new X509Certificate2(Certificate);
byte[] rawData = x509.RawData;
x509.Import(rawData);
FileStream s = new FileStream(fileName, FileMode.Create);
formatter.Serialize(s, x509);
s.Close();
}