-
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
The sample code did not get the expected results #46922
Comments
net core 3.0 and above no problem. |
3.0 and below are problematic,How to solve? |
Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq Issue DetailsHello, I didn't get the right result according to the example code.
|
This looks related to, or a duplicate of #30993. As said in the issue:
It would seem that your base64 input has whitespace or new lines in it. Is this the case for your input? As also mentioned, the easiest solution is to use a version of .NET that has issue fixed. |
Can you provide a sample base64 input that reproduces the problem? 2.2 is no longer supported, but perhaps I can determine a work around. |
`
` |
@lx4556332 I believe we need the sample inputs (nothing confidential of course) Also could you please use triple back ticks to format your code to make it a bit easier to read? This has an example |
@danmosemsft
I think we / I have everything now, just haven't had a minute to look, but will soon. |
The issue with 2.2 is the way that it handles the buffer size calculation when given the input array is larger than expected. Even though you are specifying an offset and length, it is not handled correctly in 2.2. As a work around, if you copy the input data to an array that is at most the size of the data being transformed, I believe it will work. Perhaps something like this: static void DecodeFromFile(string inFileName, string outFileName)
{
using (FromBase64Transform myTransform = new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces))
{
const int ReadSize = 4;
byte[] myOutputBytes = new byte[myTransform.OutputBlockSize];
byte[] inputBuffer = new byte[ReadSize];
//Open the input and output files.
using (FileStream myInputFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read))
{
using (FileStream myOutputFile = new FileStream(outFileName, FileMode.Create, FileAccess.Write))
{
//Retrieve the file contents into a byte array.
byte[] myInputBytes = new byte[myInputFile.Length];
myInputFile.Read(myInputBytes, 0, myInputBytes.Length);
//Transform the data in chunks the size of InputBlockSize.
int i = 0;
while (myInputBytes.Length - i > ReadSize/*myTransform.InputBlockSize*/)
{
System.Buffer.BlockCopy(myInputBytes, i, inputBuffer, 0, ReadSize);
int bytesWritten = myTransform.TransformBlock(inputBuffer, 0, inputBuffer.Length, myOutputBytes, 0);
i += 4/*myTransform.InputBlockSize*/;
myOutputFile.Write(myOutputBytes, 0, bytesWritten);
}
//Transform the final block of data.
System.Buffer.BlockCopy(myInputBytes, i, inputBuffer, 0, myInputBytes.Length - i);
myOutputBytes = myTransform.TransformFinalBlock(inputBuffer, 0, myInputBytes.Length - i);
myOutputFile.Write(myOutputBytes, 0, myOutputBytes.Length);
//Free up any used resources.
myTransform.Clear();
}
}
}
} The best solution is to upgrade to .NET Core 3.1 or newer. Then the code can be as simple as: static void DecodeFromFile(string inFileName, string outFileName)
{
using (FromBase64Transform myTransform = new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces))
using (FileStream myInputFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read))
using (FileStream myOutputFile = new FileStream(outFileName, FileMode.Create, FileAccess.Write))
using (CryptoStream cryptoStream = new CryptoStream(myInputFile, myTransform, CryptoStreamMode.Read))
{
cryptoStream.CopyTo(myOutputFile);
}
} |
Thanks for the answer. It works |
Hello, I didn't get the right result according to the example code.
URL : https://docs.microsoft.com/zh-cn/dotnet/api/system.security.cryptography.frombase64transform.-ctor?redirectedfrom=MSDN&view=netcore-2.2#System_Security_Cryptography_FromBase64Transform__ctor_System_Security_Cryptography_FromBase64TransformMode_
Error details:Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
method:DecodeFromFile(string inFileName, string outFileName)
Error line: int bytesWritten = myTransform.TransformBlock(myInputBytes, i, 4/myTransform.InputBlockSize/, myOutputBytes, 0);
The text was updated successfully, but these errors were encountered: