From 50d6598b02e82134d943d1e5dbab2bf2c5254a2e Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Tue, 8 Apr 2025 12:25:29 +0200 Subject: [PATCH] fix reading of cryptostream. The implementation issue became a bug in .net8. Closes #2816 --- src/keepass2android-app/KeeChallenge.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/keepass2android-app/KeeChallenge.cs b/src/keepass2android-app/KeeChallenge.cs index 880103e7..3d986028 100644 --- a/src/keepass2android-app/KeeChallenge.cs +++ b/src/keepass2android-app/KeeChallenge.cs @@ -146,7 +146,20 @@ namespace KeeChallenge { using (CryptoStream csDecrypt = (CryptoStream)aes.DecryptStream(msDecrypt, key, inf.IV)) { - csDecrypt.Read(secret, 0, secret.Length); + //read the secret from the stream + int totalBytesRead = 0; + byte[] buffer = new byte[secret.Length]; + var secretOutputStream = new MemoryStream(secret); + + int bytesRead = csDecrypt.Read(buffer, totalBytesRead, secret.Length - totalBytesRead); + while (bytesRead > 0 && totalBytesRead < secret.Length) + { + secretOutputStream.Write(buffer, 0, bytesRead); + totalBytesRead += bytesRead; + bytesRead = csDecrypt.Read(buffer, totalBytesRead, secret.Length - totalBytesRead); + } + secretOutputStream.Close(); + csDecrypt.Close(); } msDecrypt.Close();