Bug 52880 - System.Security.Cryptography.ProtectedData.Unprotect cannot deal with byte array input that has trailing 0 bytes
Summary: System.Security.Cryptography.ProtectedData.Unprotect cannot deal with byte ar...
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Security ()
Version: 4.8.0 (C9)
Hardware: PC All
: --- normal
Target Milestone: Future Release
Assignee: Bugzilla
Depends on:
Reported: 2017-02-28 02:47 UTC by Tobias Weisserth
Modified: 2017-10-13 18:48 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 52880 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Tobias Weisserth 2017-02-28 02:47:47 UTC
In .NET, I can call System.Security.Cryptography.ProtectedData.Unprotect(data, null, scope) where data is a byte array that has trailing 0 byte values. The data is correctly decrypted and the decrypted byte array returned truncated to the actual length of the data decrypted.

In Mono, calling System.Security.Cryptography.ProtectedData.Unprotect(data, null, scope) where data is a byte array that has trailing 0 byte values, the method will raise a CryptographicException for "invalid data".

My expectation is that Mono's implementation is similar to .NET allowing for similar input rendering similar output and behavior.

A workaround is to use call Array.Resize(ref data, Array.FindLastIndex (data, b => b != 0) + 1) before calling Mono's Unprotect method (won't hurt on .NET either as the return byte array is truncated anyway).
Comment 1 Marek Safar 2017-10-13 18:47:53 UTC

using System;
using System.Security.Cryptography;

public class Program
	public static int Main (string [] args)
		var data = new byte [] { 1, 2, 3, 4 };

		var res = ProtectedData.Protect (data, new byte[0], DataProtectionScope.CurrentUser);

		var test = new byte [res.Length + 1];
		Array.Copy (res, test, res.Length);

		ProtectedData.Unprotect (test, null, System.Security.Cryptography.DataProtectionScope.CurrentUser);

		return 0;