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
maaknplanAsked:
Who is Participating?
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?
0
maaknplanAuthor 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

0
louisfrCommented:
That would get you a CryptographicException. The entropy is missing.
Are you sure that's the code you're using?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

maaknplanAuthor 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

0
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?
0

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
maaknplanAuthor 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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.