Encryption / license issues when upgrading from .Net 1.1 to .Net 2
Posted by Fernando Felman on April 1, 2007
I’ve been called to help in the migration of a .Net 1.1 product to .Net 2. Most of the functionality worked just fine after upgrading but we had an issue with the licensing module: some of licenses we issued failed to validate after the upgrade, throwing a CryptographicException: “Length of the data to decrypt is invalid”. The issue we were having had to do with the changes that the .Net 2 introduced to the
GetBytes() method in order to be compliance with the Unicode standard (read full details here):
Encoding.GetBytes() may not emit unpaired high or low surrogate characters for certain encodings (e.g. UTF-8 Encoding and UnicodeEncoding).
The solution was quite simple once the problem was identified: instead of using Unicode.GetBytes to transform the cipher bytes to string we use Convert.ToBase64String (and its symmetric opposite Convert.FromBase64String). The original problem was identified by reading shawnfa’s post from the .Net Security team blog. More details of this problem and how to solve it using the Base64 conversion can be found in the CodeClimber blog.