forked from eneshasani1/DataSecurity_pr2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientSide.cs
More file actions
133 lines (110 loc) · 4.31 KB
/
ClientSide.cs
File metadata and controls
133 lines (110 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Cryptography;
using JWT;
using JWT.Algorithms;
using JWT.Serializers;
using System.Security.Cryptography.X509Certificates;
using System.Net.Sockets;
using System.Net;
using DataSecurity_pr2;
using System.Threading;
using DataSecurity_pr2.Repositories;
using DataSecurity_pr2.Models;
using JWT.Builder;
namespace Siguri_Projekti2
{
public class ClientSide
{
public static X509Certificate2 certifikata = new X509Certificate2("../../DataSecurity_pr2.cer");
private DESCryptoServiceProvider des;
private RSACryptoServiceProvider rsa;
static byte[] DesKey;
static byte[] initialVector;
public UdpClient udpClient;
public ClientSide()
{
try
{
udpClient = new UdpClient();
udpClient.Connect("localhost", 8080);
des = new DESCryptoServiceProvider();
rsa = (RSACryptoServiceProvider)certifikata.PublicKey.Key;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public void requestToServer(string request)
{
des.GenerateKey();
DesKey = des.Key;
des.GenerateIV();
initialVector = des.IV;
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.Zeros;
byte[] bytePlainMsg = Encoding.UTF8.GetBytes(request);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(bytePlainMsg, 0, bytePlainMsg.Length);
cs.Close();
byte[] byteCipherMsg = ms.ToArray();
byte[] byteCipherDesKey = rsa.Encrypt(DesKey, true);
byte[] fullMessage = initialVector.Concat(byteCipherDesKey).Concat(byteCipherMsg).ToArray();
string sendData = Convert.ToBase64String(initialVector.Concat(byteCipherDesKey).Concat(byteCipherMsg).ToArray());
udpClient.Send(Convert.FromBase64String(sendData), Convert.FromBase64String(sendData).Length);
//user.Shutdown(SocketShutdown.Both);
//user.Close();
}
public string responseFromServer()
{
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] byteResponse = udpClient.Receive(ref remoteIPEndPoint);
byte[] IV = new byte[8];
Array.Copy(byteResponse, IV, 8);
byte[] enMessage = new byte[byteResponse.Length - 8];
Array.Copy(byteResponse, 8, enMessage, 0, enMessage.Length);
DES des = DES.Create();
des.IV = IV;
des.Key = DesKey;
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.Zeros;
MemoryStream memoryStream = new MemoryStream(enMessage);
byte[] decryptedMessage = new byte[memoryStream.Length];
CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(), CryptoStreamMode.Read);
cryptoStream.Read(decryptedMessage, 0, decryptedMessage.Length);
cryptoStream.Close();
string decryptedData = Encoding.UTF8.GetString(decryptedMessage);
return decryptedData;
// login-...
}
public static string getJwtPayload(string token)
{
IJwtAlgorithm alg = new RS256Algorithm(certifikata);
try
{
var payload = JwtBuilder.Create()
.WithAlgorithm(alg)
.MustVerifySignature()
.Decode(token);
return payload;
}
catch (Exception ex)
{
return "invalidSignature";
}
}
public static string computeHash(string saltedpassword)
{
byte[] byteSaltedPassword = Encoding.UTF8.GetBytes(saltedpassword);
SHA1CryptoServiceProvider obj = new SHA1CryptoServiceProvider();
byte[] saltedHashPassword = obj.ComputeHash(byteSaltedPassword);
return Convert.ToBase64String(saltedHashPassword);
}
}
}