예제 #1
0
        public override ElGamalParameters ExportParameters(bool p_include_private_params)
        {
            if (NeedToGenerateKey())
            {
                CreateKeyPair(KeySizeValue);
            }

            // create the parameter set and set the public values of the parameters
            var x_params = new ElGamalParameters
            {
                P       = o_key_struct.P.ToByteArray(),
                G       = o_key_struct.G.ToByteArray(),
                Y       = o_key_struct.Y.ToByteArray(),
                Padding = o_key_struct.Padding
            };

            // if required, include the private value, X
            if (p_include_private_params)
            {
                x_params.X = o_key_struct.X.ToByteArray();
            }
            else
            {
                // ensure that we zero the value
                x_params.X = new byte[1];
            }

            return(x_params);
        }
예제 #2
0
        public override ElGamalParameters ExportParameters(bool p_include_private_params)
        {
            if (NeedToGenerateKey())
            {
                // we need to create a new key before we can export
                CreateKeyPair(KeySizeValue);
            }

            // create the parameter set
            var x_params = new ElGamalParameters();

            // set the public values of the parameters
            x_params.P       = o_key_struct.P.getBytes();
            x_params.G       = o_key_struct.G.getBytes();
            x_params.Y       = o_key_struct.Y.getBytes();
            x_params.Padding = o_key_struct.Padding;

            // if required, include the private value, X
            if (p_include_private_params)
            {
                x_params.X = o_key_struct.X.getBytes();
            }
            else
            {
                // ensure that we zero the value
                x_params.X = new byte[1];
            }

            return(x_params);
        }
예제 #3
0
        public override void FromXmlString(string p_string)
        {
            var x_params = new ElGamalParameters();

            var keyValues = XDocument.Parse(p_string).Element("ElGamalKeyValue");

            x_params.P       = Convert.FromBase64String((String)keyValues.Element("P") ?? "");
            x_params.G       = Convert.FromBase64String((String)keyValues.Element("G") ?? "");
            x_params.Y       = Convert.FromBase64String((String)keyValues.Element("Y") ?? "");
            x_params.Padding = (ElGamalPaddingMode)Enum.Parse(typeof(ElGamalPaddingMode), (String)keyValues.Element("Padding") ?? "");
            x_params.X       = Convert.FromBase64String((String)keyValues.Element("X") ?? "");

            ImportParameters(x_params);
        }
예제 #4
0
        public override void FromXmlString(String p_string)
        {
            // create the params that we will use as the result
            ElGamalParameters x_params = new ElGamalParameters();
            // create a text reader using a string reader
            XmlTextReader x_reader =
                new XmlTextReader(new System.IO.StringReader(p_string));

            // run through the elements in the xml string
            while (x_reader.Read())
            {
                // we are only interested in processing start nodes
                if (true || x_reader.IsStartElement())
                {
                    switch (x_reader.Name)
                    {
                    case "P":
                        // set the value for P
                        x_params.P = Convert.FromBase64String(x_reader.ReadString());
                        break;

                    case "G":
                        // set the value for G
                        x_params.G = Convert.FromBase64String(x_reader.ReadString());
                        break;

                    case "Y":
                        // set the value for Y
                        x_params.Y = Convert.FromBase64String(x_reader.ReadString());
                        break;

                    case "Padding":
                        // set the padding mode
                        x_params.Padding = (ElGamalPaddingMode)Enum.Parse(typeof(ElGamalPaddingMode), x_reader.ReadString());
                        break;

                    case "X":
                        // set the value for X (this would not be found in a
                        // string that was generated by excluding the private
                        // elements.
                        x_params.X = Convert.FromBase64String(x_reader.ReadString());
                        break;
                    }
                }
            }
            // Import the result
            ImportParameters(x_params);
        }
예제 #5
0
        public override void ImportParameters(ElGamalParameters p_parameters)
        {
            // obtain the  big integer values from the byte parameter values
            o_key_struct.P       = new BigInteger(p_parameters.P);
            o_key_struct.G       = new BigInteger(p_parameters.G);
            o_key_struct.Y       = new BigInteger(p_parameters.Y);
            o_key_struct.Padding = p_parameters.Padding;

            if (p_parameters.X != null && p_parameters.X.Length > 0)
            {
                o_key_struct.X = new BigInteger(p_parameters.X);
            }

            // set the length of the key based on the import
            KeySizeValue = o_key_struct.P.bitCount();
        }
예제 #6
0
        public override string ToXmlString(bool p_include_private)
        {
            ElGamalParameters x_params = ExportParameters(p_include_private);
            // create a new string builder
            StringBuilder x_sb = new StringBuilder();

            // add the header
            x_sb.Append("<ElGamalKeyValue>");
            // add the public elements from the parameters
            x_sb.Append("<P>" + Convert.ToBase64String(x_params.P) + "</P>");
            x_sb.Append("<G>" + Convert.ToBase64String(x_params.G) + "</G>");
            x_sb.Append("<Y>" + Convert.ToBase64String(x_params.Y) + "</Y>");
            x_sb.Append("<Padding>" + x_params.Padding.ToString() + "</Padding>");
            if (p_include_private)
            {
                // we need to include X, which is the part of private key
                x_sb.Append("<X>" + Convert.ToBase64String(x_params.X) + "</X>");
            }
            // add the final element
            x_sb.Append("</ElGamalKeyValue>");
            return(x_sb.ToString());
        }
예제 #7
0
 public abstract void ImportParameters(ElGamalParameters p_parameters);