112 lines
3.3 KiB
C#
112 lines
3.3 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Client
|
|
{
|
|
class Program
|
|
{
|
|
const int STD_PORT = 1234;
|
|
const string STD_IP = "127.0.0.1";
|
|
const int STD_MSG_SIZE = 1024;
|
|
const int STD_HEAD_LEN = 1;
|
|
const string STD_CRYPTOKEY = "mojKluc";
|
|
|
|
|
|
|
|
static string Recive(NetworkStream networkStream)
|
|
{
|
|
try
|
|
{
|
|
byte[] reciveMessage = new byte[STD_MSG_SIZE];
|
|
int lenght = networkStream.Read(reciveMessage, 0, reciveMessage.Length);
|
|
return Encoding.UTF8.GetString(reciveMessage, 0, lenght);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message + e.StackTrace);
|
|
return "";
|
|
}
|
|
}
|
|
|
|
static void Send(NetworkStream networkStream, string Message)
|
|
{
|
|
try
|
|
{
|
|
byte[] bytes = Encoding.UTF8.GetBytes(Message);
|
|
networkStream.Write(bytes, 0, bytes.Length);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
|
|
}
|
|
|
|
static string Decrypt(string encrypt, string key)
|
|
{
|
|
try
|
|
{
|
|
|
|
using (MD5CryptoServiceProvider hashMD5Provider = new MD5CryptoServiceProvider())
|
|
{
|
|
using (var tripleDes = TripleDES.Create())
|
|
{
|
|
tripleDes.Mode = CipherMode.ECB;
|
|
tripleDes.Padding = PaddingMode.PKCS7;
|
|
tripleDes.Key = hashMD5Provider.ComputeHash(Encoding.UTF8.GetBytes(key));
|
|
byte[] byteBuff = Convert.FromBase64String(encrypt);
|
|
return Encoding.Unicode.GetString(tripleDes.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
return "";
|
|
}
|
|
}
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
Console.WriteLine("Client\n");
|
|
while (true)
|
|
{
|
|
Console.Write("Vnesi ukaz: ");
|
|
string cmd = "";
|
|
do
|
|
{
|
|
cmd = Console.ReadLine();
|
|
}
|
|
while (cmd == null || cmd == "");
|
|
|
|
try
|
|
{
|
|
TcpClient tcpClient = new TcpClient();
|
|
tcpClient.Connect(STD_IP, STD_PORT);
|
|
NetworkStream networkStream = tcpClient.GetStream();
|
|
Send(networkStream, cmd);
|
|
|
|
string response = Recive(networkStream);
|
|
Console.WriteLine(response + "\n");
|
|
|
|
if (cmd[0] == 'G')
|
|
{
|
|
Console.WriteLine(Decrypt(response,STD_CRYPTOKEY));
|
|
}
|
|
|
|
tcpClient.Close();
|
|
if(response == "Q") return;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
return;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
} |