I'm trying to encrypt and decrypt Id with MachineKey.
Here is my code that calls the encrypt and decrypt functions:
var encryptedId = Encryption.Protect(profileId.ToString(), UserId);
var decryptedId = Encryption.UnProtect(encryptedId, UserId);
Here is the functions:
public static string Protect(string text, string purpose)
{
if(string.IsNullOrEmpty(text))
{
return string.Empty;
}
byte[] stream = Encoding.Unicode.GetBytes(text);
byte[] encodedValues = MachineKey.Protect(stream, purpose);
return HttpServerUtility.UrlTokenEncode(encodedValues);
}
public static string UnProtect(string text, string purpose)
{
if(string.IsNullOrEmpty(text))
{
return string.Empty;
}
byte[] stream = HttpServerUtility.UrlTokenDecode(text);
byte[] decodedValues = MachineKey.Unprotect(stream, purpose);
return Encoding.UTF8.GetString(decodedValues);
}
The input to the Protect
method is 15. This results that the encryptedId variable holds the following string: 6wOttbtJoVBV7PxhVWXGz4AQVYcuyHvTyJhAoPu4Okd2aKhhCGbKlK_T4q3GgirotfOZYZXke0pMdgwSmC5vxg2
To encrypt this, I send this string as a parameter to the UnProtect
method.
The result of the decryption should be 15, but is instead: 1\05\0
I can't understand why. I have tried to use only integers in this function, but I still have the same problem. The output of the decrypt differs.
\0
, no? – Womanhaterstring
, I suspect somewhere in the code it interprets (returns) the result as char (probably with UTF-8 format) thus your1
is represented as1\0
and5
as5\0
(because of the Little Endian). If that is the case, you simply need to replace all\0
to empty string before returning it. – Womanhater