Skip to content

Commit 9102d53

Browse files
JerrySB03IHtDzenda
andauthored
Feat/client (#16)
* feat: client drafy * fix: client * feat: username * feat: client drafy * fix: client * feat: username * fix: messages are sent encrypted using RSA * chore: formating * chore: delete Database class * feat: add username to Client constructor * feat: make username public * fix: copilot nitpicking --------- Co-authored-by: IhtDzenda <[email protected]>
1 parent 40c8530 commit 9102d53

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

sutor-aes/Client.cs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ namespace SutorAes
77
class Client
88
{
99
Guid Id { get; set; }
10-
string Name { get; set; }
10+
public string Name { get; set; }
1111

12-
byte[] RsaPub { get; set; }
12+
public byte[] RsaPub { get; set; }
1313
byte[] RsaPriv { get; set; }
1414
Dictionary<Guid, byte[]> KnownHosts { get; set; } = new();
1515
private event Action<string>? Handler;
1616

1717
RSAEncryptionPadding padding = RSAEncryptionPadding.Pkcs1;
1818

19-
public Client()
19+
public Client(string _name)
2020
{
21+
this.Name = _name;
2122
GenerateRSAKeys();
2223
}
2324

@@ -105,19 +106,21 @@ public void EstablishConnection(Client other)
105106
byte[] key = new byte[16];
106107

107108
System.Security.Cryptography.RandomNumberGenerator.Fill(key);
108-
other.RecieveConnection(this.Id, key);
109+
byte[] encryptedKey = this.EncryptRSA(key, other.RsaPub);
110+
other.RecieveConnection(this.Id, encryptedKey);
109111
this.KnownHosts.Add(other.Id, key);
110112
}
111-
public void RecieveConnection(Guid id, byte[] key)
113+
public void RecieveConnection(Guid id, byte[] encryptedKey)
112114
{
115+
byte[] key = this.DecryptRSA(encryptedKey);
113116
this.KnownHosts.Add(id, key);
114117
}
115118

116-
public byte[] EncryptRSA(byte[] data)
119+
public byte[] EncryptRSA(byte[] data, byte[] pubKey)
117120
{
118121
using (RSA rsa = RSA.Create())
119122
{
120-
rsa.ImportRSAPublicKey(RsaPub, out _);
123+
rsa.ImportRSAPublicKey(pubKey, out _);
121124
return rsa.Encrypt(data, padding);
122125
}
123126
}
@@ -131,24 +134,4 @@ public byte[] DecryptRSA(byte[] encryptedData)
131134
}
132135
}
133136
}
134-
135-
class Database
136-
{
137-
private Dictionary<Guid, byte[]> User2Pubkey { get; set; }
138-
139-
public Database()
140-
{
141-
User2Pubkey = new Dictionary<Guid, byte[]>();
142-
}
143-
144-
public void RegisterUser(Guid id, byte[] rsaPub)
145-
{
146-
User2Pubkey.Add(id, rsaPub);
147-
}
148-
149-
public Dictionary<Guid, byte[]> ListUsers()
150-
{
151-
return User2Pubkey;
152-
}
153-
}
154137
}

0 commit comments

Comments
 (0)