From bb8e2c133a620831400df4c47343f94d0887aa5d Mon Sep 17 00:00:00 2001 From: Rutherther Date: Mon, 13 Feb 2023 18:18:17 +0100 Subject: [PATCH] fix(crypto): correctly parse encryption key from unknown server packet --- .../CryptographyManager.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Core/NosSmooth.Cryptography/CryptographyManager.cs b/Core/NosSmooth.Cryptography/CryptographyManager.cs index d96d701..5d955a4 100644 --- a/Core/NosSmooth.Cryptography/CryptographyManager.cs +++ b/Core/NosSmooth.Cryptography/CryptographyManager.cs @@ -119,18 +119,24 @@ public class CryptographyManager } } - var encryptionKey = EncryptionKey; - var decrypted = ServerWorld.Decrypt(data, encoding); + var decryptedString = ServerWorld.Decrypt(data, encoding); if (EncryptionKey == 0) { // we are not in a session, so the packet may be the session id. // or we are in an initialized session and won't know the encryption key... - if (int.TryParse(decrypted, out var obtainedEncryptionKey)) + var decryptedSpan = (ReadOnlySpan)decryptedString; + + var firstSpaceIndex = decryptedSpan.IndexOf(' '); + if (firstSpaceIndex != -1) { - EncryptionKey = obtainedEncryptionKey; + if (int.TryParse(decryptedSpan.Slice(firstSpaceIndex + 1), out var obtainedEncryptionKey)) + { + EncryptionKey = obtainedEncryptionKey; + } } + } - return decrypted; + return decryptedString; } } \ No newline at end of file -- 2.48.1