-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.Security.Cryptography AES Decryption returns wrong results in latest builds #55527
Comments
Could you please make a program with Main method that demonstrates the problem? I am not sure what exact arguments to call the methods you have shared with to hit the problem. This is likely a regression that we have picked up from dotnet/runtime. |
@jkotas I have created a project with full code here. you can check. |
Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq, @GrabYourPitchforks Issue DetailsHi, I have few projects that use AES encryption to decrypt data sent from webserver, with builds from 6.0.0-preview.5.21269.1 and earlier it works fine, but when updating to latest builds i get wrong decrypt result. OS: Windows 10 20H2 Original text: Decrypt text: Is this a problem caused by the updates? I'm rolling back to the
|
I suspect the problem is in this code, where the caller assumes MemoryStream ms = new(cipher);
CryptoStream cs = new(ms, decryptor, CryptoStreamMode.Read);
byte[] plain = new byte[cipher.Length];
int decryptcount = cs.Read(plain, 0, plain.Length);
ms.Close();
cs.Close(); Will investigate further. |
Yes, this is exactly what happened. In earlier versions of .NET, the I am tempted to resolve this issue by design unless somebody has thoughts otherwise. |
At most it might mean we want to call it out in the release notes (maybe a breaking change notice?). But, yeah, the error here is in the calling code for assuming that Read reads complete. |
Release notes seems appropriate. We can also mention the CanTransformMultipleBlocks optimization that Steve Toub checked in. |
This silent breaking change is not great. Just from a brief look I see plenty of code where Read() is called without looping, including a lot of Microsoft's own code. I have a strong feeling that a lot of code will mysteriously break in .NET 6. Should there be a code analysis out of the box that looks for Stream.Read() being used correctly? |
I've looked more into this, and I don't really understand why this behavior needed to change. It seems that CryptoStream.Read() works in chunks of 16 bytes (EDIT: the chunk/block size is based on whatever transform decryptor uses), so if you give it 1440 bytes, it returns as expected. Adding one extra byte will require a second Read() call to get that single byte. This just feels like CryptoStream is missing a handler for the remaining <16 byte chunk. Is there any good reason to not do that, to match the behavior of previous versions? There's this code: https://github.com/stephentoub/runtime/blob/194ffff6de345631ad471417a3fe414891dc23a8/src/libraries/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs#L425-L429 which reads one block (when there's only one block to read), is that perhaps just not being used when it read multiple blocks above? To me it looks like the loop doesn't run because of I understand that this behavior was changed to be non blocking, but why not read until the end, if the data is there? (e.g. backed by a MemoryStream) |
Because it's operating over an arbitrary Stream and has no idea whether the next call to Read{Async} will succeed immediately or not. Streams in general are supposed to return from Read once data is available; CryptoStream was failing to do that. The breaking change is documented here: |
Looks like Steve already got the breaking change doc up. Closing. |
Repro: Run the following program on current .NET 6 preview7 (preview.7.21362.5-win-x64)
Actual result: FAILED
Expected result: PASSED
Hi, I have few projects that use AES encryption to decrypt data sent from webserver, with builds from 6.0.0-preview.5.21269.1 and earlier it works fine, but when updating to latest builds i get wrong decrypt result.
OS: Windows 10 20H2
OS Build: 19042.1083
SDK: .Net 5.0.3
ILCompiler version: Latest builds
Original text:
{"status":true,"key":"808ffc0a-0cda-4358-9f01-a28d2e3490db","token":"1QcSB5sqXv1AiBjduD8WV1o57MI4YpHnAKf3KTmlzPuumwUMyyXFg63hqohWyJEZ"}
Decrypt text:
{"status":true,"key":"808ffc0a-0cda-4358-9f01-a28d2e3490db","token":"1QcSB5sqXv1AiBjduD8WV1o57MI4YpHnAKf3KTmlzPuumwUMyyXFg63hqohWyJEZ
decryption result always missing characters after
"
Encryption code:

Decryption code:

Is this a problem caused by the updates? I'm rolling back to the
6.0.0-preview.5.21269.1
build now so the app can work fine.Any update on this, thanks
The text was updated successfully, but these errors were encountered: