public static string FileGost(this byte[] stream) { string result; var hash = new Gost3411CryptoServiceProvider().ComputeHash(stream); result = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower(); return(result); }
private void button2_Click(object sender, EventArgs e) { string plain_text = richTextBox1.Text; byte[] plain_text_bytes = Encoding.ASCII.GetBytes(plain_text); Gost3411CryptoServiceProvider hash = new Gost3411CryptoServiceProvider(); byte[] signature = csp.SignData(plain_text_bytes, hash); BERelement main_seq = new BERelement(0x30); BERelement sign_seq = new BERelement(0x30); //sign_seq.AddItem(new BERelement(0x0c, Encoding.UTF8.GetBytes("sign"))); sign_seq.AddItem(new BERelement(0x02, signature)); sign_seq.AddItem(new BERelement(0x02, plain_text_bytes)); main_seq.AddItem(sign_seq); byte[] test = main_seq.GetEncodedPacket().ToArray(); File.WriteAllBytes("asn2", test); /* * BERelement mSeq = BERelement.DecodePacket(test); * BERelement sSeq = null; * sSeq = mSeq.Items[0]; * var signed2 = sSeq.Items[0].Value; * MessageBox.Show(signed2.Length.ToString()); * var msg = sSeq.Items[1].Value; */ //send data try { Int32 port = 9595; TcpClient client = new TcpClient("127.0.0.1", port); NetworkStream stream = client.GetStream(); stream.Write(mode, 1, 1); // stream.Write(signature, 0, signature.Length); //stream.Write(plain_text_bytes, 0, plain_text_bytes.Length); stream.Write(test, 0, test.Length); stream.Close(); client.Close(); } catch (ArgumentNullException exception) { MessageBox.Show("ArgumentNullException: " + exception); } catch (SocketException exception) { MessageBox.Show("SocketException: " + exception); } }
private static string SignData(string data) { var cert = GetMyX509Certificate(); // String msg, X509Certificate2 cert using (var gost = new Gost3411CryptoServiceProvider()) { var sign = new GostSignatureFormatter(cert.PrivateKey); var buff = Encoding.UTF8.GetBytes(data); var hash = gost.ComputeHash(buff); return(Convert.ToBase64String(sign.CreateSignature(hash))); } }
static void Main(string[] args) { try { socket.Init(); while (true) { byte[] data = socket.Recieve(); BERelement asn = BERelement.DecodePacket(data); int operation; if (asn.Items[0].Value.Length > 1) { operation = BitConverter.ToInt32(asn.Items[0].Value, 0); } else { operation = asn.Items[0].Value[0]; } switch (operation) { case (int)Cmd.certs: SendCertificatesList(); break; case (int)Cmd.cipher: SendCertificatesList(); data = socket.Recieve(); asn = BERelement.DecodePacket(data); string certName = Encoding.ASCII.GetString(asn.Items[0].Value); foreach (var cert in crypter.Certificates) { if (cert.FriendlyName == certName) { crypter.currentCertificate = cert; SendPublicKey(cert); break; } } data = socket.Recieve(); crypter.SetSymmetrKey(Asn1Formatter.GetSymAsn1(data)); crypter.IV = Asn1Formatter.GetIVAsn1(data); byte[] asn1Established = Asn1Formatter.SetCertASN1(Encoding.ASCII.GetBytes("ESTABLISHED")); socket.Send(asn1Established); data = socket.Recieve(); String text = Encoding.ASCII.GetString(crypter.Decrypt(data)); Console.WriteLine("Recieved data from client: " + text); break; case (int)Cmd.sign: asn1Established = Asn1Formatter.SetCertASN1(Encoding.ASCII.GetBytes("ESTABLISHED")); socket.Send(asn1Established); data = socket.Recieve(); asn = BERelement.DecodePacket(data); try { if (asn.Items[0].Value.Length > 1) { operation = BitConverter.ToInt32(asn.Items[0].Value, 0); } else { operation = asn.Items[0].Value[0]; } if (operation == (int)Cmd.error) { Console.WriteLine("Error!"); continue; } } catch (NotSupportedException) { Console.WriteLine("Signed message came!"); } BERelement mSeq = BERelement.DecodePacket(data); BERelement sSeq = mSeq.Items[0]; BERelement fSeq = mSeq.Items[1]; var signature = sSeq.Items[0].Value; var certS = new X509Certificate2(sSeq.Items[1].Value); var time = DateTime.FromBinary(BitConverter.ToInt64(sSeq.Items[2].Value, 0)); var sData = fSeq.Items[0].Value; Gost3410_2012_256CryptoServiceProvider sGost = (Gost3410_2012_256CryptoServiceProvider)certS.PublicKey.Key; Gost3411CryptoServiceProvider hGost = new Gost3411CryptoServiceProvider(); bool correct = sGost.VerifySignature(hGost.ComputeHash(sData), signature); Console.WriteLine($"Friendly name: {certS.FriendlyName}"); Console.WriteLine(certS + "\n"); Console.WriteLine("Signature time..."); Console.WriteLine(time + "\n"); Console.WriteLine("Correct signature?..."); Console.WriteLine(correct + "\n"); Console.WriteLine("Data:"); Console.WriteLine(Encoding.ASCII.GetString(sData) + "\n"); break; default: break; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void button4_Click(object sender, EventArgs e) { if (checkBox2.Checked && !checkBox1.Checked) { string plain_text = richTextBox1.Text; byte[] plain_text_bytes = Encoding.ASCII.GetBytes(plain_text); Gost3411CryptoServiceProvider hash = new Gost3411CryptoServiceProvider(); byte[] signature = csp.SignData(plain_text_bytes, hash); BERelement main_seq = new BERelement(0x30); BERelement sign_seq = new BERelement(0x30); sign_seq.AddItem(new BERelement(0x0C, cert_name)); sign_seq.AddItem(new BERelement(0x04, signature)); sign_seq.AddItem(new BERelement(0x04, plain_text_bytes)); main_seq.AddItem(sign_seq); byte[] test = main_seq.GetEncodedPacket().ToArray(); File.WriteAllBytes("signature", test); try { Int32 port = 9595; TcpClient client = new TcpClient("127.0.0.1", port); NetworkStream stream = client.GetStream(); stream.Write(mode, 1, 1); stream.Write(test, 0, test.Length); stream.Close(); client.Close(); } catch (ArgumentNullException exception) { MessageBox.Show("ArgumentNullException: " + exception); } catch (SocketException exception) { MessageBox.Show("SocketException: " + exception); } } else if (checkBox1.Checked && !checkBox2.Checked) { Gost28147 gost = Gost28147.Create(); Gost3410Parameters public_key = csp.ExportParameters(false); GostSharedSecretAlgorithm agree_key = csp.CreateAgree(sign.ExportParameters(false)); byte[] wrapped_key = agree_key.Wrap(gost, GostKeyWrapMethod.CryptoProKeyWrap); BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, public_key); byte[] public_key_bytes = ms.ToArray(); ms.Close(); MemoryStream memory_stream = new MemoryStream(); CryptoStream cs = new CryptoStream(memory_stream, gost.CreateEncryptor(), CryptoStreamMode.Write); string plain_text = richTextBox1.Text; byte[] plain_text_bytes = Encoding.ASCII.GetBytes(plain_text); cs.Write(plain_text_bytes, 0, plain_text_bytes.Length); cs.FlushFinalBlock(); byte[] cipher_text_bytes = memory_stream.ToArray(); memory_stream.Close(); cs.Close(); BERelement main_seq = new BERelement(0x30); BERelement sign_seq = new BERelement(0x30); sign_seq.AddItem(new BERelement(0x0C, cert_name)); sign_seq.AddItem(new BERelement(0x04, wrapped_key)); sign_seq.AddItem(new BERelement(0x04, gost.IV)); sign_seq.AddItem(new BERelement(0x04, public_key_bytes)); sign_seq.AddItem(new BERelement(0x04, cipher_text_bytes)); main_seq.AddItem(sign_seq); byte[] test = main_seq.GetEncodedPacket().ToArray(); File.WriteAllBytes("encryption", test); try { Int32 port = 9595; TcpClient client = new TcpClient("127.0.0.1", port); NetworkStream stream = client.GetStream(); stream.Write(mode, 0, 1); stream.Write(test, 0, test.Length); stream.Close(); client.Close(); } catch (ArgumentNullException exception) { MessageBox.Show("ArgumentNullException: " + exception); } catch (SocketException exception) { MessageBox.Show("SocketException: " + exception); } } else if (checkBox2.Checked && checkBox1.Checked) { MessageBox.Show("suka!"); string plain_text = richTextBox1.Text; byte[] plain_text_bytes = Encoding.ASCII.GetBytes(plain_text); Gost3411CryptoServiceProvider hash = new Gost3411CryptoServiceProvider(); byte[] signature = csp.SignData(plain_text_bytes, hash); BERelement main_seq = new BERelement(0x30); BERelement sign_seq = new BERelement(0x30); sign_seq.AddItem(new BERelement(0x0C, cert_name)); sign_seq.AddItem(new BERelement(0x04, signature)); sign_seq.AddItem(new BERelement(0x04, plain_text_bytes)); main_seq.AddItem(sign_seq); byte[] test = main_seq.GetEncodedPacket().ToArray(); File.WriteAllBytes("signature_and_encryption", test); Gost28147 gost = Gost28147.Create(); Gost3410Parameters public_key = csp.ExportParameters(false); GostSharedSecretAlgorithm agree_key = csp.CreateAgree(sign.ExportParameters(false)); byte[] wrapped_key = agree_key.Wrap(gost, GostKeyWrapMethod.CryptoProKeyWrap); BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, public_key); byte[] public_key_bytes = ms.ToArray(); ms.Close(); MemoryStream memory_stream = new MemoryStream(); CryptoStream cs = new CryptoStream(memory_stream, gost.CreateEncryptor(), CryptoStreamMode.Write); //string plain_text = richTextBox1.Text; //byte[] plain_text_bytes = Encoding.ASCII.GetBytes(plain_text); byte[] plain_text_bytes2 = test; cs.Write(plain_text_bytes2, 0, plain_text_bytes2.Length); cs.FlushFinalBlock(); byte[] cipher_text_bytes2 = memory_stream.ToArray(); memory_stream.Close(); cs.Close(); BERelement main_seq2 = new BERelement(0x30); BERelement sign_seq2 = new BERelement(0x30); sign_seq2.AddItem(new BERelement(0x0C, cert_name)); sign_seq2.AddItem(new BERelement(0x04, wrapped_key)); sign_seq2.AddItem(new BERelement(0x04, gost.IV)); sign_seq2.AddItem(new BERelement(0x04, public_key_bytes)); sign_seq2.AddItem(new BERelement(0x04, cipher_text_bytes2)); main_seq2.AddItem(sign_seq2); byte[] test2 = main_seq2.GetEncodedPacket().ToArray(); try { Int32 port = 9595; TcpClient client = new TcpClient("127.0.0.1", port); NetworkStream stream = client.GetStream(); stream.Write(mode, 2, 1); stream.Write(test2, 0, test2.Length); stream.Close(); client.Close(); } catch (ArgumentNullException exception) { MessageBox.Show("ArgumentNullException: " + exception); } catch (SocketException exception) { MessageBox.Show("SocketException: " + exception); } } }
private void check_response() { TcpClient client = null; try { TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 9595); listener.Start(); Byte[] bytes = new Byte[256]; String data = null; while (true) { client = listener.AcceptTcpClient(); NetworkStream ns = client.GetStream(); Byte[] mode_bytes = new Byte[1]; int mode = ns.Read(mode_bytes, 0, mode_bytes.Length); if (mode_bytes[0] == 0x01) { richTextBox1.Text += "Received encrypted msg. Try to decrypt: \n"; int bytes_read = ns.Read(buffer, 0, buffer.Length); byte[] message = new byte[bytes_read]; Array.Copy(buffer, message, bytes_read); BERelement mSeq = BERelement.DecodePacket(message); BERelement sSeq = null; sSeq = mSeq.Items[0]; var cert_name = sSeq.Items[0].Value; var wrapped_key2 = sSeq.Items[1].Value; var iv2 = sSeq.Items[2].Value; var public_key_bytes = sSeq.Items[3].Value; var cipher_text_bytes = sSeq.Items[4].Value; MemoryStream ms = new MemoryStream(public_key_bytes); BinaryFormatter bf = new BinaryFormatter(); Gost3410Parameters key_params = (Gost3410Parameters)bf.Deserialize(ms); GostSharedSecretAlgorithm agree_key = csp.CreateAgree(key_params); SymmetricAlgorithm gost = agree_key.Unwrap(wrapped_key2, GostKeyWrapMethod.CryptoProKeyWrap); gost.IV = iv2; MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, gost.CreateDecryptor(), CryptoStreamMode.Write); int bytesRead = cipher_text_bytes.Length; cryptoStream.Write(cipher_text_bytes, 0, bytesRead); cryptoStream.FlushFinalBlock(); byte[] plain_text_bytes = memoryStream.ToArray(); richTextBox1.Text += Encoding.ASCII.GetString(plain_text_bytes, 0, plain_text_bytes.Length) + "\n"; } if (mode_bytes[0] == 0x02) { richTextBox1.Text += "Received sign. Try to check it: \n"; Gost3411CryptoServiceProvider hash = new Gost3411CryptoServiceProvider(); int bytes_read = ns.Read(buffer, 0, buffer.Length); byte[] message = new byte[bytes_read]; Array.Copy(buffer, message, bytes_read); BERelement mSeq = BERelement.DecodePacket(message); BERelement sSeq = null; sSeq = mSeq.Items[0]; var cert_name = sSeq.Items[0].Value; var signed2 = sSeq.Items[1].Value; var msg = sSeq.Items[2].Value; int len = signed2.Length; bool test = csp.VerifyData(msg, hash, signed2); if (test) { richTextBox1.Text += "Подпись корректна.\r\n"; } else { richTextBox1.Text += "Подпись некорректна.\r\n"; } /* * bool test = csp.VerifyData(message, hash, signed); * if (test) richTextBox1.Text += "Подпись корректна.\r\n"; * else richTextBox1.Text += "Подпись некорректна.\r\n"; * */ } if (mode_bytes[0] == 0x03) { richTextBox1.Text += "Received sign and encryption. Making magic: \n"; int bytes_read = ns.Read(buffer, 0, buffer.Length); byte[] message = new byte[bytes_read]; Array.Copy(buffer, message, bytes_read); BERelement mSeq = BERelement.DecodePacket(message); BERelement sSeq = null; sSeq = mSeq.Items[0]; var cert_name = sSeq.Items[0].Value; var wrapped_key2 = sSeq.Items[1].Value; var iv2 = sSeq.Items[2].Value; var public_key_bytes = sSeq.Items[3].Value; var cipher_text_bytes = sSeq.Items[4].Value; MemoryStream ms = new MemoryStream(public_key_bytes); BinaryFormatter bf = new BinaryFormatter(); Gost3410Parameters key_params = (Gost3410Parameters)bf.Deserialize(ms); GostSharedSecretAlgorithm agree_key = csp.CreateAgree(key_params); SymmetricAlgorithm gost = agree_key.Unwrap(wrapped_key2, GostKeyWrapMethod.CryptoProKeyWrap); gost.IV = iv2; MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, gost.CreateDecryptor(), CryptoStreamMode.Write); int bytesRead = cipher_text_bytes.Length; cryptoStream.Write(cipher_text_bytes, 0, bytesRead); cryptoStream.FlushFinalBlock(); byte[] plain_text_bytes = memoryStream.ToArray(); //check signature Gost3411CryptoServiceProvider hash = new Gost3411CryptoServiceProvider(); //Array.Copy(buffer, message, bytes_read); BERelement mSeq2 = BERelement.DecodePacket(plain_text_bytes); BERelement sSeq2 = null; sSeq2 = mSeq2.Items[0]; var cert_name2 = sSeq2.Items[0].Value; var signed3 = sSeq2.Items[1].Value; var msg2 = sSeq2.Items[2].Value; int len2 = signed3.Length; bool test = csp.VerifyData(msg2, hash, signed3); richTextBox1.Text += "Полученное сообщение: " + Encoding.ASCII.GetString(msg2, 0, msg2.Length) + "\r\n"; if (test) { richTextBox1.Text += "Подпись корректна.\r\n"; } else { richTextBox1.Text += "Подпись некорректна.\r\n"; } } int i; while ((i = ns.Read(bytes, 0, bytes.Length)) != 0) { data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); richTextBox1.Text = DateTime.Now.ToString() + "\n"; richTextBox1.Text += data; } client.Close(); } } catch (SocketException exception) { MessageBox.Show("SocketException: " + exception); } }