public void ClientToServerMessage() { NetMQMessage plainMessage = new NetMQMessage(); plainMessage.Append("Hello"); NetMQMessage cipherMessage = m_clientSecureChannel.EncryptApplicationMessage(plainMessage); NetMQMessage decryptedMessage = m_serverSecureChannel.DecryptApplicationMessage(cipherMessage); Assert.AreEqual(decryptedMessage[0].ConvertToString(), plainMessage[0].ConvertToString()); Assert.AreEqual(decryptedMessage[0].ConvertToString(), "Hello"); }
public void Do() { // we are using dealer here, but we can use router as well, we just have to manager // SecureChannel for each identity using (var socket = new DealerSocket()) { socket.Bind("tcp://*:9696"); using (SecureChannel secureChannel = SecureChannel.CreateServerSecureChannel(m_configuration)) { // we need to set X509Certificate with a private key for the server X509Certificate2 certificate = new X509Certificate2( System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "server.pfx"), "1234"); secureChannel.Certificate = certificate; List <NetMQMessage> outgoingMessages = new List <NetMQMessage>(); // waiting for message from client NetMQMessage incomingMessage = socket.ReceiveMultipartMessage(); //SplitInMessage // calling ProcessMessage until ProcessMessage return true // and the SecureChannel is ready to encrypt and decrypt messages while (!secureChannel.ProcessMessage(incomingMessage, outgoingMessages)) { //SendMessages(socket,outgoingMessages); foreach (NetMQMessage message in outgoingMessages) { socket.SendMultipartMessage(message); } outgoingMessages.Clear(); incomingMessage = socket.ReceiveMultipartMessage(); } //SendMessages(socket, outgoingMessages); foreach (NetMQMessage message in outgoingMessages) { socket.SendMultipartMessage(message); } outgoingMessages.Clear(); // this message is now encrypted NetMQMessage cipherMessage = socket.ReceiveMultipartMessage(); // decrypting the message NetMQMessage plainMessage = secureChannel.DecryptApplicationMessage(cipherMessage); System.Console.WriteLine(plainMessage.First.ConvertToString()); plainMessage = new NetMQMessage(); plainMessage.Append("Worldddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"); // encrypting the message and sending it over the socket socket.SendMultipartMessage(secureChannel.EncryptApplicationMessage(plainMessage)); } } }
public void BigBytesData() { NetMQMessage plainMessage = new NetMQMessage(); plainMessage.Append(new byte[255 * 255]); NetMQMessage cipherMessage = m_serverSecureChannel.EncryptApplicationMessage(plainMessage); var combineBytes = ToBytes(cipherMessage); int offset = 0; List <NetMQMessage> sslMessages; bool result = m_clientSecureChannel.ResolveRecordLayer(combineBytes, out offset, out sslMessages); Assert.AreEqual(result, true); List <NetMQMessage> plainMessageList = new List <NetMQMessage>(); foreach (var sslMessage in sslMessages) { plainMessageList.Add(m_clientSecureChannel.DecryptApplicationMessage(sslMessage)); } Assert.AreEqual(plainMessage.Last.BufferSize, plainMessageList.Sum(b => b.Last.BufferSize)); }
public void ClientToServerMessage() { NetMQMessage plainMessage = new NetMQMessage(); plainMessage.Append("Hello"); NetMQMessage cipherMessage = m_clientSecureChannel.EncryptApplicationMessage(plainMessage); bool changeCipherSepc = false; int offset; List <NetMQMessage> sslMessages; bool result = m_serverSecureChannel.ResolveRecordLayer(cipherMessage.First.Buffer, out offset, out sslMessages); Assert.AreEqual(sslMessages.Count, 1); cipherMessage = sslMessages[0]; NetMQMessage decryptedMessage = m_serverSecureChannel.DecryptApplicationMessage(cipherMessage); Assert.AreEqual(decryptedMessage[0].ConvertToString(), plainMessage[0].ConvertToString()); Assert.AreEqual(decryptedMessage[0].ConvertToString(), "Hello"); }
public void Do() { // we are using dealer here, but we can use router as well, we just have to manager // SecureChannel for each identity using (var socket = new StreamSocket()) { socket.Connect("tcp://127.0.0.1:9696"); using (SecureChannel secureChannel = SecureChannel.CreateClientSecureChannel(null, m_configuration)) { secureChannel.AllowedCipherSuites = new [] { CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA }; // we need to set X509Certificate with a private key for the server X509Certificate2 certificate = new X509Certificate2( System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "server.pfx"), "1234"); secureChannel.Certificate = certificate; List <NetMQMessage> outgoingMessages = new List <NetMQMessage>(); bool clientComplete = secureChannel.ProcessMessage(null, outgoingMessages); SendMessages(socket, outgoingMessages); bool done = false; // waiting for message from client byte[] cache = null; do { outgoingMessages.Clear(); NetMQMessage incomingMessage = socket.ReceiveMultipartMessage(); if (cache == null || cache.Length <= 0) { cache = incomingMessage.Last.Buffer; } else { cache = CombineV2(cache, incomingMessage.Last.Buffer); } //SplitInMessage int offset; List <NetMQMessage> sslMessages; secureChannel.ResolveRecordLayer(cache, out offset, out sslMessages); if (cache.Length == offset) { cache = null; } else if (cache.Length > offset) { byte[] temp = new byte[cache.Length - offset]; Buffer.BlockCopy(cache, offset, temp, 0, temp.Length); cache = temp; } foreach (var sslMessage in sslMessages) { // calling ProcessMessage until ProcessMessage return true // and the SecureChannel is ready to encrypt and decrypt messages done = secureChannel.ProcessMessage(sslMessage, outgoingMessages); SendMessages(socket, outgoingMessages); } } while (!done); SendMessages(socket, outgoingMessages); for (int i = 0; i < 10; i++) { outgoingMessages.Clear(); NetMQMessage plainMessage = new NetMQMessage(); byte[] data = Encoding.GetEncoding("GBK").GetBytes("10009<Root><Head><CommandCode>10009</CommandCode><TransSeqID>2020051514384165</TransSeqID><VerifyCode>MbzZvbTp9Cnw9iqvRjJ3in6wNry59ZB1ubSCpWxeRiov9eU0c8MCGTE+u+7ED7NlU4EA8mf+OATBvS6OlgYzggKmsEt6CoPhQB3V/xzMZzlLGwym7r1arrNYIUjW6oJKXWNe84SYTe8Mqfw1+gmzEcj72QpadujHdDTJ9WNEsmg=</VerifyCode><ZipType></ZipType><CorpBankCode>103</CorpBankCode><FGCommandCode>11111</FGCommandCode><EnterpriseNum>AS330106</EnterpriseNum><TransKeyEncryptFlag>0</TransKeyEncryptFlag><FGVerifyCode>nQuCJ41Gp1wuankSkCvscwFVISkdI0XoGUJwKTB9IS7dbg+OgxpHe/zdSQkIZQjZbS5rzkFlmx31mrR8cmZa/jXJ+r4xeBfncS6qKJdYEH4jJra4/JyFkcb2mE8yolxN3v1C/M/Kq2+d532oXuQfiBqkEAv3gSb30zjurtVs3+I=</FGVerifyCode></Head><RealTimeSingleTransReq><MoneyWay>2</MoneyWay><TransDate>20200515</TransDate><Trans><TransNo>testClwTLS20200515003</TransNo><ProtocolCode></ProtocolCode><EnterpriseAccNum>19030101040014391</EnterpriseAccNum><CustBankCode>103</CustBankCode><CustAccNum>12312312</CustAccNum><CustAccName>陈大帅逼</CustAccName><AreaCode></AreaCode><BankLocationCode></BankLocationCode><BankLocationName></BankLocationName><CardType></CardType><IsPrivate>0</IsPrivate><IsUrgent></IsUrgent><Amount>232.00</Amount><Currency>CNY</Currency><CertType>0</CertType><CertNum></CertNum><Mobile></Mobile><Purpose></Purpose><Memo></Memo><PolicyNumber></PolicyNumber><Extent1></Extent1><Extent2></Extent2><SourceTransNo>testClwTLS20200515003</SourceTransNo></Trans></RealTimeSingleTransReq></Root>"); string length = data.Length.ToString().PadLeft(8, ' '); plainMessage.Append(length); plainMessage.Append(data); socket.SendMoreFrame(socket.Options.Identity); socket.SendFrame(secureChannel.EncryptApplicationMessage(plainMessage)[0].Buffer); // this message is now encrypted NetMQMessage cipherMessage = socket.ReceiveMultipartMessage(); int offset2; List <NetMQMessage> sslMessages2; secureChannel.ResolveRecordLayer(cipherMessage.Last.Buffer, out offset2, out sslMessages2); // decrypting the message plainMessage = secureChannel.DecryptApplicationMessage(sslMessages2[0]); System.Console.WriteLine(plainMessage.First.ConvertToString()); } // encrypting the message and sending it over the socket } } }
public void Do() { // we are using dealer here, but we can use router as well, we just have to manager // SecureChannel for each identity using (var socket = new StreamSocket()) { socket.Bind("tcp://*:9696"); using (SecureChannel secureChannel = SecureChannel.CreateServerSecureChannel(m_configuration)) { // we need to set X509Certificate with a private key for the server X509Certificate2 certificate = new X509Certificate2( System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "server.pfx"), "1234"); secureChannel.Certificate = certificate; List <NetMQMessage> outgoingMessages = new List <NetMQMessage>(); bool done = false; // waiting for message from client byte[] cache = null; do { outgoingMessages.Clear(); NetMQMessage incomingMessage = socket.ReceiveMultipartMessage(); if (cache == null || cache.Length <= 0) { cache = incomingMessage.Last.Buffer; } else { cache = CombineV2(cache, incomingMessage.Last.Buffer); } //SplitInMessage int offset; List <NetMQMessage> sslMessages; secureChannel.ResolveRecordLayer(cache, out offset, out sslMessages); if (cache.Length == offset) { cache = null; } else if (cache.Length > offset) { byte[] temp = new byte[cache.Length - offset]; Buffer.BlockCopy(cache, offset, temp, 0, temp.Length); cache = temp; } foreach (var sslMessage in sslMessages) { // calling ProcessMessage until ProcessMessage return true // and the SecureChannel is ready to encrypt and decrypt messages done = secureChannel.ProcessMessage(sslMessage, outgoingMessages); SendMessages(socket, outgoingMessages); } } while (!done); SendMessages(socket, outgoingMessages); outgoingMessages.Clear(); cache = null; while (true) { // this message is now encrypted NetMQMessage cipherMessage = socket.ReceiveMultipartMessage(); if (cache == null || cache.Length <= 0) { cache = cipherMessage.Last.Buffer; } else { cache = CombineV2(cache, cipherMessage.Last.Buffer); } int offset2; List <NetMQMessage> sslMessages2; secureChannel.ResolveRecordLayer(cache, out offset2, out sslMessages2); if (cache.Length == offset2) { cache = null; } else if (offset2 == 0) { //长度不够,等下一次读取在解析 continue; } else if (cache.Length > offset2) { byte[] temp = new byte[cache.Length - offset2]; Buffer.BlockCopy(cache, offset2, temp, 0, temp.Length); cache = temp; } if (sslMessages2.Count <= 0) { continue; } // decrypting the message NetMQMessage plainMessage = secureChannel.DecryptApplicationMessage(sslMessages2[0]); System.Console.WriteLine(plainMessage.First.ConvertToString()); plainMessage = new NetMQMessage(); plainMessage.Append("00000021<Root>TestResp</Root>"); socket.SendMoreFrame(socket.Options.Identity); socket.SendFrame(secureChannel.EncryptApplicationMessage(plainMessage)[0].Buffer); } // encrypting the message and sending it over the socket } } }
public void Do() { using (var socket = new DealerSocket()) { socket.Connect("tcp://127.0.0.1:5556"); SecureChannel secureChannel = SecureChannel.CreateClientSecureChannel(null, m_configuration); // we are not using signed certificate so we need to validate // the certificate of the server, by default the secure channel // is checking that the source of the // certitiface is a root certificate authority //secureChannel.SetVerifyCertificate(c => true); // we need to set X509Certificate with a private key for the server X509Certificate2 certificate = new X509Certificate2( System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "server.crt"), "1234"); secureChannel.Certificate = certificate; List <NetMQMessage> outgoingMessages = new List <NetMQMessage>(); // call the process message with null as the incoming message // because the client is initiating the connection secureChannel.ProcessMessage(null, outgoingMessages); //Server.SendMessages(socket, outgoingMessages); foreach (NetMQMessage message in outgoingMessages) { socket.SendMultipartMessage(message); } outgoingMessages.Clear(); // waiting for a message from the server NetMQMessage incomingMessage = socket.ReceiveMultipartMessage(); // calling ProcessMessage until ProcessMessage return true // and the SecureChannel is ready to encrypt and decrypt messages while (!secureChannel.ProcessMessage(incomingMessage, outgoingMessages)) { foreach (NetMQMessage message in outgoingMessages) { socket.SendMultipartMessage(message); } outgoingMessages.Clear(); //Server.SendMessages(socket, outgoingMessages); incomingMessage = socket.ReceiveMultipartMessage(); } foreach (NetMQMessage message in outgoingMessages) { socket.SendMultipartMessage(message); } outgoingMessages.Clear(); //Server.SendMessages(socket, outgoingMessages); // you can now use the secure channel to encrypt messages NetMQMessage plainMessage = new NetMQMessage(); plainMessage.Append("Hello"); // encrypting the message and sending it over the socket socket.SendMultipartMessage(secureChannel.EncryptApplicationMessage(plainMessage)); NetMQMessage cipherMessage = socket.ReceiveMultipartMessage(); // decrypting the message plainMessage = secureChannel.DecryptApplicationMessage(cipherMessage); System.Console.WriteLine(plainMessage.First.ConvertToString()); } }