Example #1
0
        private void CreateCrypto(string pyt, ref XmlDocument doc, TextBox textBoxName)
        {
            XmlTextReader gf   = new XmlTextReader(pyt);
            string        buff = doc.OuterXml;
            int           j    = 0;

            //Проверка есть ли уже криптография
            for (int i = 0; i < buff.Length; i++)
            {
                string sig = "Signature";
                char   a   = buff[i];
                char   b   = sig[j];
                if (buff[i] == sig[j])
                {
                    string str = "";
                    for (int k = 0; k < sig.Length; k++)
                    {
                        str += buff[i + k];
                    }
                    //j++;
                    if (str == sig)
                    {
                        MessageBox.Show("Ключ уже создан");
                        goto BB;
                    }
                }
            }
            DSA        Key        = DSA.Create();
            string     xmls       = Key.ToXmlString(false);
            XmlElement xmlElement = doc.CreateElement("KEy");

            xmlElement.InnerText = xmls;
            doc.DocumentElement.AppendChild(xmlElement);
            dris.Key = Key;
            SignedXml signedXml = new SignedXml(doc);

            signedXml.SigningKey = Key;
            Reference reference = new Reference();

            reference.Uri = "";
            XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();

            reference.AddTransform(env);
            signedXml.AddReference(reference);
            signedXml.ComputeSignature();
            XmlElement xmlDigitalSignature = signedXml.GetXml();

            doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));

            if (doc.FirstChild is XmlDeclaration)
            {
                doc.RemoveChild(doc.FirstChild);
            }
            gf.Close();
            XmlTextWriter xmltw = new XmlTextWriter(pyt, System.Text.Encoding.UTF8);

            doc.WriteTo(xmltw);
            xmltw.Close();
            BB :;
            if (gf.ReadState != ReadState.Closed)
            {
                gf.Close();
            }

            //XmlDocument doc = new XmlDocument();
            //doc.PreserveWhitespace = false;
            //XmlTextReader gf = new XmlTextReader(pyt);
            //doc.Load(gf);
            //string buff = doc.OuterXml;
            //int j = 0;
            ////Проверка есть ли уже криптография
            //for(int i =0;i<buff.Length;i++)
            //{
            //    string sig = "Signature";
            //    char a = buff[i];
            //    char b = sig[j];
            //    if(buff[i]==sig[j])
            //    {
            //        string str = "";
            //        for(int k=0;k<sig.Length;k++)
            //        {
            //            str += buff[i + k];
            //        }
            //        //j++;
            //        if(str == sig)
            //        {
            //            MessageBox.Show("Ключ уже создан");
            //            goto BB;
            //        }
            //    }
            //}
            //DSA Key = DSA.Create();
            //dris.Key = Key;
            //SignedXml signedXml = new SignedXml(doc);
            //signedXml.SigningKey = Key;
            //Signature XMLSignature = signedXml.Signature;
            //Reference reference = new Reference("");
            //XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
            //reference.AddTransform(env);
            //XMLSignature.SignedInfo.AddReference(reference);
            //KeyInfo keyInfo = new KeyInfo();
            //keyInfo.AddClause(new DSAKeyValue((DSA)Key));
            //XMLSignature.KeyInfo = keyInfo;
            //signedXml.ComputeSignature();
            //XmlElement xmlDigitalSignature = signedXml.GetXml();
            //doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
            ////DSA dfs = DSA.Create();
            ////dfs.FromXmlString("");
            //XmlElement ssdw = doc.CreateElement("Key", Key.SignatureAlgorithm);
            //doc.DocumentElement.AppendChild(ssdw);
            //if (doc.FirstChild is XmlDeclaration)
            //{
            //    doc.RemoveChild(doc.FirstChild);
            //}
            //gf.Close();
            ////doc.Save(pyt);
            //XmlTextWriter xmltw = new XmlTextWriter(pyt, System.Text.Encoding.UTF8);
            //doc.WriteTo(xmltw);
            //xmltw.Close();
            //BB:;
            ////
        }
Example #2
0
        static public DSA FromCapiPrivateKeyBlobDSA(byte[] blob, int offset)
        {
            if (blob == null)
            {
                throw new ArgumentNullException("blob");
            }
            if (offset >= blob.Length)
            {
                throw new ArgumentException("blob is too small.");
            }

            DSAParameters dsap = new DSAParameters();

            try {
                if ((blob [offset] != 0x07) ||                                          // PRIVATEKEYBLOB (0x07)
                    (blob [offset + 1] != 0x02) ||                                      // Version (0x02)
                    (blob [offset + 2] != 0x00) ||                                      // Reserved (word)
                    (blob [offset + 3] != 0x00) ||
                    (ToUInt32LE(blob, offset + 8) != 0x32535344))                       // DWORD magic
                {
                    throw new CryptographicException("Invalid blob header");
                }

                int bitlen  = ToInt32LE(blob, offset + 12);
                int bytelen = bitlen >> 3;
                int pos     = offset + 16;

                dsap.P = new byte [bytelen];
                Buffer.BlockCopy(blob, pos, dsap.P, 0, bytelen);
                Array.Reverse(dsap.P);
                pos += bytelen;

                dsap.Q = new byte [20];
                Buffer.BlockCopy(blob, pos, dsap.Q, 0, 20);
                Array.Reverse(dsap.Q);
                pos += 20;

                dsap.G = new byte [bytelen];
                Buffer.BlockCopy(blob, pos, dsap.G, 0, bytelen);
                Array.Reverse(dsap.G);
                pos += bytelen;

                dsap.X = new byte [20];
                Buffer.BlockCopy(blob, pos, dsap.X, 0, 20);
                Array.Reverse(dsap.X);
                pos += 20;

                dsap.Counter = ToInt32LE(blob, pos);
                pos         += 4;

                dsap.Seed = new byte [20];
                Buffer.BlockCopy(blob, pos, dsap.Seed, 0, 20);
                Array.Reverse(dsap.Seed);
                pos += 20;
            }
            catch (Exception e) {
                throw new CryptographicException("Invalid blob.", e);
            }

            DSA dsa = null;

            try
            {
                dsa = (DSA)DSA.Create();
                dsa.ImportParameters(dsap);
            }
            catch (CryptographicException ce)
            {
                // this may cause problem when this code is run under
                // the SYSTEM identity on Windows (e.g. ASP.NET). See
                // http://bugzilla.ximian.com/show_bug.cgi?id=77559
                try
                {
                    CspParameters csp = new CspParameters();
                    csp.Flags = CspProviderFlags.UseMachineKeyStore;
                    dsa       = new DSACryptoServiceProvider(csp);
                    dsa.ImportParameters(dsap);
                }
                catch
                {
                    // rethrow original, not the later, exception if this fails
                    throw ce;
                }
            }
            return(dsa);
        }
Example #3
0
 /// <summary>DsaFactory</summary>
 /// <returns>DSA</returns>
 public static DSA DsaFactory()
 {
     // ハンドリングをDSA(ベースの型に)に変更したので行けるハズ
     return(DSA.Create());
 }
Example #4
0
        //
        // public constructors
        //

        public DSAKeyValue()
        {
            m_key = DSA.Create();
        }
Example #5
0
 public DSA Create()
 {
     return(DSA.Create());
 }
Example #6
0
 public void ExchangeDSAKey()
 {
     DSA dsa = DSA.Create();
     AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter(dsa);
 }
 internal DSAOther()
 {
     _impl = DSA.Create(1024);
 }