Exemplo n.º 1
0
        public void ImportParam(RSAParam parameters)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException("");
            }

            // if missing "mandatory" parameters
            if (parameters.Exponent == null)
            {
                throw new CryptographicException("Missing Exponent");
            }
            if (parameters.Modulus == null)
            {
                throw new CryptographicException("Missing Modulus");
            }

            e = new BigInteger(parameters.Exponent);
            n = new BigInteger(parameters.Modulus);
            // only if the private key is present
            if (parameters.D != null)
            {
                d = new BigInteger(parameters.D);
            }
            if (parameters.DP != null)
            {
                dp = new BigInteger(parameters.DP);
            }
            if (parameters.DQ != null)
            {
                dq = new BigInteger(parameters.DQ);
            }
            if (parameters.InverseQ != null)
            {
                qInv = new BigInteger(parameters.InverseQ);
            }
            if (parameters.P != null)
            {
                p = new BigInteger(parameters.P);
            }
            if (parameters.Q != null)
            {
                q = new BigInteger(parameters.Q);
            }
            //duff add
            if (parameters.qP != null)
            {
                qP = new BigInteger(parameters.qP);
            }
            if (parameters.pQ != null)
            {
                pQ = new BigInteger(parameters.pQ);
            }

            // we now have a keypair
            keypairGenerated = true;
            isCRTpossible    = ((p != null) && (q != null) && (dp != null) && (dq != null) && (qInv != null));
        }
Exemplo n.º 2
0
//duff add
        public RSAParam ExportParam(bool includePrivateParameters)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException("");
            }

            if (!keypairGenerated)
            {
                GenerateKeyPair();
            }

            RSAParam param = new RSAParam();

            param.Exponent = e.GetBytes();
            param.Modulus  = n.GetBytes();
            if (includePrivateParameters)
            {
                // some parameters are required for exporting the private key
                if ((d == null) || (p == null) || (q == null))
                {
                    throw new CryptographicException("Missing private key");
                }
                param.D = d.GetBytes();
                // hack for bugzilla #57941 where D wasn't provided
                if (param.D.Length != param.Modulus.Length)
                {
                    byte[] normalizedD = new byte [param.Modulus.Length];
                    Buffer.BlockCopy(param.D, 0, normalizedD, (normalizedD.Length - param.D.Length), param.D.Length);
                    param.D = normalizedD;
                }
                param.P = p.GetBytes();
                param.Q = q.GetBytes();
                // but CRT parameters are optionals
                if ((dp != null) && (dq != null) && (qInv != null))
                {
                    // and we include them only if we have them all
                    param.DP       = dp.GetBytes();
                    param.DQ       = dq.GetBytes();
                    param.InverseQ = qInv.GetBytes();
                }
                //duff add
                param.qP = qP.GetBytes();
                param.pQ = pQ.GetBytes();
            }
            return(param);
        }
Exemplo n.º 3
0
		public void ImportParam (RSAParam parameters) 
		{
			if (m_disposed)
				throw new ObjectDisposedException ("");

			// if missing "mandatory" parameters
			if (parameters.Exponent == null) 
				throw new CryptographicException ("Missing Exponent");
			if (parameters.Modulus == null)
				throw new CryptographicException ("Missing Modulus");
	
			e = new BigInteger (parameters.Exponent);
			n = new BigInteger (parameters.Modulus);
			// only if the private key is present
			if (parameters.D != null)
				d = new BigInteger (parameters.D);
			if (parameters.DP != null)
				dp = new BigInteger (parameters.DP);
			if (parameters.DQ != null)
				dq = new BigInteger (parameters.DQ);
			if (parameters.InverseQ != null)
				qInv = new BigInteger (parameters.InverseQ);
			if (parameters.P != null)
				p = new BigInteger (parameters.P);
			if (parameters.Q != null)
				q = new BigInteger (parameters.Q);
			//duff add
			if (parameters.qP != null)
				qP = new BigInteger (parameters.qP);
			if (parameters.pQ != null)
				pQ = new BigInteger (parameters.pQ);

			// we now have a keypair
			keypairGenerated = true;
			isCRTpossible = ((p != null) && (q != null) && (dp != null) && (dq != null) && (qInv != null));
		}
Exemplo n.º 4
0
//duff add	
		public RSAParam ExportParam (bool includePrivateParameters) 
		{
			if (m_disposed)
				throw new ObjectDisposedException ("");

			if (!keypairGenerated)
				GenerateKeyPair ();
	
			RSAParam param = new RSAParam ();
			param.Exponent = e.GetBytes ();
			param.Modulus = n.GetBytes ();
			if (includePrivateParameters) 
			{
				// some parameters are required for exporting the private key
				if ((d == null) || (p == null) || (q == null))
					throw new CryptographicException ("Missing private key");
				param.D = d.GetBytes ();
				// hack for bugzilla #57941 where D wasn't provided
				if (param.D.Length != param.Modulus.Length) 
				{
					byte[] normalizedD = new byte [param.Modulus.Length];
					Buffer.BlockCopy (param.D, 0, normalizedD, (normalizedD.Length - param.D.Length), param.D.Length);
					param.D = normalizedD;
				}
				param.P = p.GetBytes ();
				param.Q = q.GetBytes ();
				// but CRT parameters are optionals
				if ((dp != null) && (dq != null) && (qInv != null)) 
				{
					// and we include them only if we have them all
					param.DP = dp.GetBytes ();
					param.DQ = dq.GetBytes ();
					param.InverseQ = qInv.GetBytes ();
				}
				//duff add
				param.qP = qP.GetBytes ();
				param.pQ = pQ.GetBytes ();
			}
			return param;
		}
        private byte[] RSAExport(RSAParam key,eRSAKeyFormat format)
        {
            RSAWriter writer = new RSAWriter();

            /* output key type */
            writer.WriteByte((byte)format);

            /* output modulus  and exponent*/
            writer.WriteBignum(key.Modulus);
            writer.WriteBignum(key.Exponent);

            if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED || format == eRSAKeyFormat.PK_PRIVATE)
            {
                writer.WriteBignum(key.D);
            }

            if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED)
            {
                writer.WriteBignum(key.DQ);
                writer.WriteBignum(key.DP);
                writer.WriteBignum(key.pQ);
                writer.WriteBignum(key.qP);
                writer.WriteBignum(key.P);
                writer.WriteBignum(key.Q);
            }

            return writer.GetBuffer();
        }
        public bool RSAImport(byte[] exportedkey)
        {
            RSAReader reader = new RSAReader(exportedkey);

            eRSAKeyFormat format =(eRSAKeyFormat)reader.ReadByte();
            RSAParam key = new RSAParam();

            /* input modulus  and exponent*/
            key.Modulus = reader.ReadBignum();
            key.Exponent = reader.ReadBignum();

            if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED || format == eRSAKeyFormat.PK_PRIVATE)
            {
                key.D = reader.ReadBignum();
            }

            if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED)
            {
                key.DQ = reader.ReadBignum();
                key.DP = reader.ReadBignum();
                key.pQ = reader.ReadBignum();
                key.qP = reader.ReadBignum();
                key.P = reader.ReadBignum();
                key.Q = reader.ReadBignum();
            }

            //skip version at end of buffer

            this.ImportParam(key);

            return true;
        }