Closed kogoel closed 2 months ago
I don't think there is enough information here to fully diagnose the problem. Particularly, NaeRijndaelKey
appears to be a custom implementation of SymmetricAlgorithm
(or one of its derivatives). bufferLen
is also an undefined variable.
A couple of suggestions.
CryptoStream
after .NET 6 was released, such as #82148.This issue has been automatically marked no-recent-activity
because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity
.
This issue will now be closed since it had been marked no-recent-activity
but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.
Description
Problem Statement: While trying to decrypt the encrypted bytes in chunks, getting exception in cryptoTransform.TransformFinalBlock() as "Padding is invalid and cannot be removed"
Encryption: Have added some custom code to prepend 4 bytes on initial block only for some information, no other blocks have this 4 byte header information.
Decryption: When passing the data in chunks (buffer of 4096 as Memory Stream or FileStream) I am removing the top 4 bytes, then passing the remaining data as below to tranform block.
_cryptoTransform.TransformBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
while after all the chunks are decrypted the last bock of 4 bytes are passed to TransformFinalBlock() as below:
_cryptoTransform.TransformFinalBlock(inputBuffer,0,0);
Throws Padding exception .However when data is passed as memory stream or file stream without chunking the last 4 bytes are decrypted as expected and there is no exception.
Note: Also linking an existing https://github.com/dotnet/runtime/issues/83381
Reproduction Steps
Encryption is done similarly just 4 byte header is prepended in the encrypted bytes in our custom code. Decryption Code block:
Expected behavior
There should not be exception. It should process the encrypted as when data is not passed in chunks.
Actual behavior
Getting exception: Cryptographic error "Padding is invalid and cannot be removed"
Regression?
No response
Known Workarounds
No response
Configuration
Sample is in .Net 6.0, Library is on .netstandard 2.1
Other information
https://github.com/dotnet/runtime/issues/83381