Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to completely serialize/deserialize RSAParameters object

The RSAParameters object does not support serialization of private key data. How can I serialize and deserialize a private key completely?

like image 630
Richard Collette Avatar asked Feb 18 '15 00:02

Richard Collette


People also ask

What is serializing and Deserializing?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

What is serializable attribute in C#?

This attribute is used in the class definition to indicate that a class can be serialized. By default, all fields in the class are serialized except for the fields that are marked with a NonSerializedAttribute . It is not necessary to use this attribute if a given type implements the System.


1 Answers

The following code may be used to serialize and deserialize RSAParameters objects, using the serializer of your own choosing.

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);
    }
}
like image 174
Richard Collette Avatar answered Nov 15 '22 08:11

Richard Collette