예제 #1
0
        /// <summary>
        /// Converts a WebKey of type RSA or RSAHSM to a RSA parameter object
        /// </summary>
        /// <param name="parameters"></param>
        /// <param name="includePrivateParameters">Tells if private material
        /// must be included.</param>
        /// <returns>An RSA parameter</returns>
        public static RSAParameters ToRSAParameters(this RsaParams parameters,
                                                    bool includePrivateParameters = true)
        {
            KeyEx.VerifyNonZero(parameters.N);
            KeyEx.VerifyNonZero(parameters.E);
            if (!includePrivateParameters)
            {
                return(new RSAParameters {
                    Modulus = KeyEx.RemoveLeadingZeros(parameters.N),
                    Exponent = KeyEx.ForceLength(parameters.E, 4)
                });
            }
            var num = KeyEx.RemoveLeadingZeros(parameters.N).Length * 8;

            return(new RSAParameters {
                Modulus = KeyEx.RemoveLeadingZeros(parameters.N),
                Exponent = KeyEx.ForceLength(parameters.E, 4),
                D = KeyEx.ForceLength(parameters.D, num / 8),
                DP = KeyEx.ForceLength(parameters.DP, num / 16),
                DQ = KeyEx.ForceLength(parameters.DQ, num / 16),
                InverseQ = KeyEx.ForceLength(parameters.QI, num / 16),
                P = KeyEx.ForceLength(parameters.P, num / 16),
                Q = KeyEx.ForceLength(parameters.Q, num / 16)
            });
        }
예제 #2
0
        /// <summary>
        /// Converts a WebKey of type EC or EC-HSM to an EC parameter object.
        /// </summary>
        /// <param name="ecParameters"></param>
        /// <param name="includePrivateParameters">private material must be
        /// included.</param>
        /// <returns>An EC parameter object</returns>
        public static ECParameters ToECParameters(this EccParams ecParameters,
                                                  bool includePrivateParameters = true)
        {
            KeyEx.VerifyNonZero(ecParameters.X);
            KeyEx.VerifyNonZero(ecParameters.Y);

            var keyParameterSize = ecParameters.Curve.GetKeyParameterSize();

            if (includePrivateParameters && ecParameters.D != null)
            {
                KeyEx.VerifyNonZero(ecParameters.D);
                ecParameters.D = KeyEx.ForceLength(ecParameters.D, keyParameterSize);
            }
            return(new ECParameters {
                Curve = ecParameters.Curve.ToECCurve(),
                D = ecParameters.D,
                Q = new ECPoint {
                    X = KeyEx.ForceLength(ecParameters.X, keyParameterSize),
                    Y = KeyEx.ForceLength(ecParameters.Y, keyParameterSize)
                }
            });
        }