Skip to content

Commit 379aa78

Browse files
authored
Merge pull request #29 from YassinLokhat/28-improve-asymetric-encryption
Improve Asymmetric Encryption
2 parents 9c67158 + f61233c commit 379aa78

1 file changed

Lines changed: 13 additions & 19 deletions

File tree

Core/Public/Utils/CryptographyCenter.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Security.Cryptography;
22
using System.Text;
3+
using System.Text.Json;
34
using Upsilon.Apps.PassKey.Core.Public.Interfaces;
45

56
namespace Upsilon.Apps.PassKey.Core.Public.Utils
@@ -105,18 +106,15 @@ public string EncryptAsymmetrically(string source, string key)
105106
RSAParameters pubKey = (RSAParameters?)xs.Deserialize(sr) ?? throw new WrongPasswordException(0);
106107

107108
csp.ImportParameters(pubKey);
108-
StringBuilder sb = new();
109109

110-
while (source.Length != 0)
111-
{
112-
int size = source.Length < 100 ? source.Length : 100;
113-
114-
_ = sb.Append(_encryptRsa(source[..size], csp) + "|");
115-
116-
source = source[size..];
117-
}
118-
119-
source = sb.ToString().TrimEnd('|');
110+
Random random = new((int)DateTime.Now.Ticks);
111+
byte[] randomBytes = new byte[100];
112+
random.NextBytes(randomBytes);
113+
string aesKey = Encoding.UTF8.GetString(randomBytes);
114+
source = EncryptSymmetrically(source, [aesKey]);
115+
aesKey = _encryptRsa(aesKey, csp);
116+
var s = new KeyValuePair<string, string>(aesKey, source);
117+
source = JsonSerializer.Serialize(s);
120118

121119
Sign(ref source);
122120

@@ -139,15 +137,11 @@ public string DecryptAsymmetrically(string source, string key)
139137

140138
csp.ImportParameters(privKey);
141139

142-
string[] sourecs = source.Split('|');
143-
StringBuilder sb = new();
140+
var s = JsonSerializer.Deserialize<KeyValuePair<string, string>>(source);
141+
string aesKey = _decryptRsa(s.Key, 0, csp);
142+
source = DecryptSymmetrically(s.Value, [aesKey]);
144143

145-
for (int i = 0; i < sourecs.Length; i++)
146-
{
147-
_ = sb.Append(_decryptRsa(sourecs[i], i, csp));
148-
}
149-
150-
return sb.ToString();
144+
return source;
151145
}
152146

153147
private string _cipherAes(string plainText, string key)

0 commit comments

Comments
 (0)