-
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
CipherMode.CFB does not Encrypt the same in .NET 5.0 RC1 as in .NET 4.7 #43234
Comments
Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq, @jeffhandley |
It looks like there are two things:
If you explicitly set the |
@bartonjs assuming you want something fixed here, how likely is it that it could make it in to RTM? |
Ah, I see. I think that this is just an unfortunate circumstance of the type hierarchy for the crypto types here. The only change to consider would be making the TripleDESImplementation class default to FeedbackSize = 8 so that @danmosemsft How would you feel about taking a change to 5.0 that changed the default value of |
@bartonjs another issue is padding. It looks like for CFB8, we are on padding to the feedback size (so 1 byte of padding is added). In .NET Framework we are padding to the whole block (nearest 8 for 3DES). I don't think that will impact padding removal and the data will interop as long as normal padding removal is used. |
Yeah, I guess my willingness to take the FeedbackSize default change is predicated on it meaning that a value encrypted in .NET Framework can be decrypted in .NET 5, and vice-versa (even though they compute different padding). |
I'll work on this over the weekend and you can decide if you want to port to release/5.0. |
Thanks, @vcsjones. I so owe you a cup of coffee for the help you've given while I've been juggling multiple projects. |
@bartonjs so if I understand right
If so this sounds like worth taking to tactics for approval, but note @vcsjones we won't know their decision before Monday, in case that influences whether you want to spend time on it 😃 |
@danmosemsft Yeah, that's an accurate summary. @vcsjones My psychic diagnostic powers believe the whole change should be to add public TripleDESImplementation()
{
// Default CFB to CFB8 to match .NET Framework's default for TripleDES.Create()
FeedbackSizeValue = 8;
} around hereish Lines 12 to 14 in d984ab7
and a "set the minimum number of properties and make sure we can read data from netfx, even though we produce different data in the 'ignore me' bytes" test. Hopefully yours agrees. I'm eager to find out how close my predictive powers were to reality. |
I will guess 80% chance they take it.. |
Sold! |
@bartonjs You were right about the feedback size. Padding remains an interesting case. The .NET Framework expects the ciphertext to be padded to the block size, even with CFB8. Otherwise it will blow up. As the way things are today, .NET 5 pads CFB8 data in a way that .NET Framework cannot de-pad (unless you get lucky). This will work in .NET 5 and fail in .NET Framework: byte[] ciphertext = Convert.FromBase64String("Vx8GlhTi");
using var tripleDes = TripleDES.Create();
tripleDes.FeedbackSize = 8;
tripleDes.Mode = CipherMode.CFB;
tripleDes.Key = Convert.FromBase64String("aaHRNTLQh++TK3lhShiicCPLQ6IO0YVH");
tripleDes.IV = Convert.FromBase64String("OQHTwYDPlMo=");
using var transform = tripleDes.CreateDecryptor();
Console.WriteLine(Encoding.UTF8.GetString(transform.TransformFinalBlock(ciphertext, 0, ciphertext.Length))); The reverse is not a problem: we can de-pad .NET Framework encrypted data in .NET 5. |
@bartonjs The padding size thing is a bit thorny if we want to start encrypting with full blocks of padding. We might break something for someone going from RC to RTM. Here are the options, as I figure.
@danmosemsft there are two potential .NET Framework compatibility breaks here. The first one that was originally discussed is fairly simple and aligns with your initial assessment. That one I still think is worth taking and discussing with "tactics" on Monday. That is fixed in #43259 and that is the only thing in that PR. |
Then I think at this point we stick with "We do nothing." (for padding, vs feedback size). Upgrading succeeds (old data can be read), but side-by-side is hampered (since old programs can't read new program data). Callers who need the padding to be there can do their encrypts with no padding and manually append the padding data. It's not great, but the workaround is required in the newer application, so it's at least not something that requires a time machine. |
cc @jeffhandley as it's 5.0. |
When using CipherMode.CFB as Mode in the System.Security.Cryptography.TripleDES to Encrypt e.g. a password you don't get the same encryption string back in .NET 5.0 RC1 as you get in .NET 4.7.
We have used code like this since .net 4.5 and it's worked exactly the same since, i should work exactly the same in .NET 5.0 as in 4.x or all apps using this for e.g. saving password encrypted will stop working after upgrade to
.NET 5.
I did a test console app to reproduce this, it's the exact the same code for both .NET 5.0 RC1 as for .NET 4.7.
Both apps where created in VS 2019 Preview (16..8.0 Preview 3.2).
And they do not generate the same result.
The text was updated successfully, but these errors were encountered: