There's a code sample here: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.tobase64transform
I believe the code sample is wrong.
I tried copy-pasting it in VS2022, in a project with the main cs file named members.cs It says it doesn't find members.cs in the debug folder along with the executable. I copied members.cs there. It created members.enc but it's empty, 0 bytes. I expected some encrypted output. I don't fully understand C# yet but if there's something wrong with the code, please report it to Microsoft. The more I learn, the more confused I am.
Try it yourself and tell me who's wrong here, it's hard to learn when you don't trust the learning material...
This is a perfectly valid question. The linked sample from Microsoft does indeed appear to be incorrect.
The first problem is that the Members.cs file needs to have Copy to Output Directory set to Copy always, which is not stated in the article. This will allow the executing code to see the Members.cs file in the output folder.
The second problem is that ToBase64Transform.CanTransformMultipleBlocks always returns true, so the check if (!base64Transform.CanTransformMultipleBlocks) always fails, meaning that nothing is ever written to the output file (i.e. members.enc always has zero bytes).
If this check is removed or inverted, the code still fails to run correctly. It looks like the length of the input block (always 3 bytes or fewer) is not being passed correctly.
Changing this:
base64Transform.TransformBlock(
inputBytes,
inputOffset,
inputBytes.Length - inputOffset,
outputBytes,
0);
to this:
base64Transform.TransformBlock(
inputBytes,
inputOffset,
Math.Min(3, inputBytes.Length - inputOffset),
outputBytes,
0);
allows the code to run and generate an output file (members.enc) of a sensible size (5 KB).
I cannot vouch for the rest of the code or the validity of this file, but this should be enough to get you closer and allow you to verify the output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With