Esempio n. 1
0
 public byte[] Sign(byte[] data, int certIndex)
 {
     try
     {
         if (hash == null)
         {
             throw new NullReferenceException("Compute hash first");
         }
         CspParameters cp                     = new CspParameters();
         var           privateKey             = ClientCertificates[certIndex].PrivateKey as Gost3410_2012_256CryptoServiceProvider;
         var           uniqueKeyContainerName = privateKey.CspKeyContainerInfo.UniqueKeyContainerName;
         cp.KeyContainerName = uniqueKeyContainerName;
         cp.ProviderType     = 75;
         cp.ProviderName     = null;
         Gost3410_2012_256 gkey = new Gost3410_2012_256CryptoServiceProvider(cp);
         Gost3410_2012_256CryptoServiceProvider srcContainer = new Gost3410_2012_256CryptoServiceProvider(cp);
         Gost3410Parameters srcPublicKeyParameters           = srcContainer.ExportParameters(false);
         if (srcContainer == null)
         {
             throw new Exception("У сертификата нет приватного ключа");
         }
         signature = srcContainer.CreateSignature(hasher.Hash);
         sCert     = ClientCertificates[certIndex].Export(X509ContentType.Cert);
         sTime     = DateTime.Now;
         return(Asn1Formatter.CreateSignature(signature, sCert, sTime, data));
     }
     catch (CryptographicException ex)
     {
         Console.WriteLine(ex.Message);
         return(null);
     }
 }
Esempio n. 2
0
 private void EncryptOperationTo()
 {
     try
     {
         crypter = new Crypter();
         var    operation     = DetermineOpearation();
         var    operationAsn1 = Asn1Formatter.SetCommandAsn1((int)Cmd.cipher);
         byte[] buffer;
         handler.Send(operationAsn1);
         data = handler.Recieve();
         var text     = (string)CerificatesBox.Invoke(new Func <string>(() => CerificatesBox.Text));
         var asn1Cert = Asn1Formatter.SetCertNameAsn1(Encoding.ASCII.GetBytes(text));
         handler.Send(asn1Cert);
         data = handler.Recieve();
         byte[] cert = Asn1Formatter.GetCertAsn1(data);
         crypter.FromBytesToCert(cert);
         byte[] symivBytes = Asn1Formatter.SetSymKeyAndIVAsn1(crypter.GetEncryptedSymKey(), crypter.IV);
         handler.Send(symivBytes);
         data = handler.Recieve();
         if (Encoding.ASCII.GetString(Asn1Formatter.GetCertAsn1(data)) == "ESTABLISHED")
         {
             text   = (string)textBox1.Invoke(new Func <string>(() => textBox1.Text));
             buffer = crypter.Encrypt(Encoding.ASCII.GetBytes(text));
             handler.Send(buffer);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Esempio n. 3
0
 private void SignOperationTo()
 {
     try
     {
         var operation     = DetermineOpearation();
         var operationAsn1 = Asn1Formatter.SetCommandAsn1((int)Cmd.sign);
         handler.Send(operationAsn1);
         data = handler.Recieve();
         if (Encoding.ASCII.GetString(Asn1Formatter.GetCertAsn1(data)) == "ESTABLISHED")
         {
             int certIndex = (int)CerificatesBox.Invoke(new Func <int>(() => CerificatesBox.SelectedIndex));
             handler.Send(crypter.Sign(Encoding.ASCII.GetBytes(textBox1.Text), certIndex));
         }
         else
         {
             throw new Exception("Error while send");
         }
     }
     catch (NullReferenceException ne)
     {
         var errorCode = Asn1Formatter.SetCommandAsn1((int)Cmd.error);
         handler.Send(errorCode);
         MessageBox.Show(ne.Message);
         Console.WriteLine(ne.Message);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Esempio n. 4
0
 private void Cipher_CheckedChanged(object sender, EventArgs e)
 {
     if (Cipher.Checked)
     {
         var asn1Cmd = Asn1Formatter.SetCommandAsn1((int)Cmd.certs);
         handler.Send(asn1Cmd);
         data = handler.Recieve();
         BERelement certsNames = BERelement.DecodePacket(data);
         CerificatesBox.DataSource = null;
         CerificatesBox.Items.Clear();
         foreach (var cert in certsNames.Items)
         {
             CerificatesBox.Items.Add(Encoding.ASCII.GetString(cert.Value));
         }
         CerificatesBox.SelectedIndex = 0;
     }
 }
Esempio n. 5
0
        private void GetCertsFromServer(object sender, EventArgs e)
        {
            var asn1Cmd = Asn1Formatter.SetCommandAsn1((int)Cmd.certs);

            int.TryParse(textBox3.Text, out int port);
            handler = new ClientSocket(textBox2.Text, port);
            handler.Init();
            handler.Send(asn1Cmd);
            data = handler.Recieve();
            BERelement certsNames = BERelement.DecodePacket(data);

            CerificatesBox.DataSource = null;
            CerificatesBox.Items.Clear();
            foreach (var cert in certsNames.Items)
            {
                CerificatesBox.Items.Add(Encoding.ASCII.GetString(cert.Value));
            }
            CerificatesBox.SelectedIndex = 0;
        }