Encoding and Decoding Special Characters using Microsoft ProtectedData Class

The method below is what I use to encrypt passwords. The method GetEntropy() simply returns random text.

The issue I am having, is that if I call the Crypt method with a string that contains a "%", for example  Crypt("te%xt"), and I then decrypt the encoding, it returns "tet". For some reason the %x is being read as something else. I tried to escape the %, for example Crypt("te/%xt"), but that just returns "te/t".

 static string Crypt(string text)
        {
            byte[] entropy = Encoding.ASCII.GetBytes(GetEntropy());

            return Convert.ToBase64String(
                ProtectedData.Protect(
                    Encoding.Unicode.GetBytes(text), entropy, DataProtectionScope.CurrentUser));
        }

Open in new window


Any guidance is much appreciated.
LVL 1
maaknplanSenior DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

louisfrCommented:
The problem is not in your Crypt method. What code do you use to decrypt it?
maaknplanSenior DeveloperAuthor Commented:
Thanks for the reply.

The decrypt code is shown below.

static string Decrypt(string text)
        {
            byte[] entropy = Encoding.ASCII.GetBytes(GetEntropy());
            return Encoding.Unicode.GetString(
                ProtectedData.Unprotect(
                     Convert.FromBase64String(text), null, DataProtectionScope.CurrentUser));
        }

Open in new window

louisfrCommented:
That would get you a CryptographicException. The entropy is missing.
Are you sure that's the code you're using?
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

maaknplanSenior DeveloperAuthor Commented:
Nice catch, the correct code is:

        static string Decrypt(string text)
        {
            byte[] entropy = Encoding.ASCII.GetBytes(GetEntropy());
            return Encoding.Unicode.GetString(
                ProtectedData.Unprotect(
                     Convert.FromBase64String(text), [b]entropy[/b], DataProtectionScope.LocalMachine));
        }

Open in new window

louisfrCommented:
With the same code, I tried crypting "te%xt" and then decrypting and got "te%xt" back.
The only thing that is probably not the same is the GetEntropy method, but that should be irrelevant.
Are you sure the method gets "te%xt"? Did you set a breakpoint in it, or output to the console?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maaknplanSenior DeveloperAuthor Commented:
Thanks for the help and pointers. Indeed the problem is not with the encrypt and decrypt. After "te%xt" has been decrypted, the next step is
System.Windows.Forms.SendKeys.SendWait(decryptedText);

Open in new window


It seems the SendWait is the culprit and is interpreting the % as the ALT key.

The documentation around this is found here - https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.