consolidate all repos to one for archive
This commit is contained in:
112
semester_3/racunalniska_omrezja/naloga_2/Odjemalnik/Client.cs
Normal file
112
semester_3/racunalniska_omrezja/naloga_2/Odjemalnik/Client.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user