gpt4 book ai didi

.net - 如何完全序列化/反序列化 RSAParameters 对象

转载 作者:行者123 更新时间:2023-12-04 19:04:34 26 4
gpt4 key购买 nike

RSAParameters对象不支持私钥数据的序列化。如何完全序列化和反序列化私钥?

最佳答案

以下代码可用于序列化和反序列化 RSAParameters对象,使用您自己选择的序列化程序。

using System;
using System.Runtime.Serialization;
using System.Security.Cryptography;

[Serializable]
public class RSAParametersSerializable : ISerializable
{
private RSAParameters _rsaParameters;

public RSAParameters RSAParameters
{
get
{
return _rsaParameters;
}
}

public RSAParametersSerializable(RSAParameters rsaParameters)
{
_rsaParameters = rsaParameters;
}

private RSAParametersSerializable()
{
}

public byte[] D { get { return _rsaParameters.D; } set { _rsaParameters.D = value; } }

public byte[] DP { get { return _rsaParameters.DP; } set { _rsaParameters.DP = value; } }

public byte[] DQ { get { return _rsaParameters.DQ; } set { _rsaParameters.DQ = value; } }

public byte[] Exponent { get { return _rsaParameters.Exponent; } set { _rsaParameters.Exponent = value; } }

public byte[] InverseQ { get { return _rsaParameters.InverseQ; } set { _rsaParameters.InverseQ = value; } }

public byte[] Modulus { get { return _rsaParameters.Modulus; } set { _rsaParameters.Modulus = value; } }

public byte[] P { get { return _rsaParameters.P; } set { _rsaParameters.P = value; } }

public byte[] Q { get { return _rsaParameters.Q; } set { _rsaParameters.Q = value; } }

public RSAParametersSerializable(SerializationInfo information, StreamingContext context)
{
_rsaParameters = new RSAParameters()
{
D = (byte[])information.GetValue("D", typeof(byte[])),
DP = (byte[])information.GetValue("DP", typeof(byte[])),
DQ = (byte[])information.GetValue("DQ", typeof(byte[])),
Exponent = (byte[])information.GetValue("Exponent", typeof(byte[])),
InverseQ = (byte[])information.GetValue("InverseQ", typeof(byte[])),
Modulus = (byte[])information.GetValue("Modulus", typeof(byte[])),
P = (byte[])information.GetValue("P", typeof(byte[])),
Q = (byte[])information.GetValue("Q", typeof(byte[]))
};
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("D", _rsaParameters.D);
info.AddValue("DP", _rsaParameters.DP);
info.AddValue("DQ", _rsaParameters.DQ);
info.AddValue("Exponent", _rsaParameters.Exponent);
info.AddValue("InverseQ", _rsaParameters.InverseQ);
info.AddValue("Modulus", _rsaParameters.Modulus);
info.AddValue("P", _rsaParameters.P);
info.AddValue("Q", _rsaParameters.Q);
}
}

关于.net - 如何完全序列化/反序列化 RSAParameters 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28573694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com