Home › Forums › ActiveX components › Xceed Encryption for ActiveX › Decrypting String using HexToBinary
-
AuthorPosts
-
#42988 |
Hello,
I am currently working on converting a method we have written using Classic ASP into a C# .Net DLL and I’ve come across a stumbling block.
What we have is an encrypted string stored in a registry setting that we retrieve and decrypt using a HexToBinary function. I have managed to duplicate the Hex string that is created by the ASP code via C# using a modified HexToBinary function and receive a variant byte array back when I call FromString method of the XceedEncryption class:
<code>
public static object HexToBinary(string strHexValue, XceedEncryption xDecrypter)
{
int i = 0;
long lngVal;
string tmpHexValue = “”;
string strBinaryValue = “”;
object varBinaryValue = new object();
StringBuilder sb = new StringBuilder();
try
{
while (i < strHexValue.Length)
{
tmpHexValue = strHexValue.Substring(i, 2);
lngVal =
long.Parse(tmpHexValue, NumberStyles.AllowHexSpecifier);
sb.Append(
Convert.ToChar(lngVal).ToString());
i += 2;
}
strBinaryValue = sb.ToString();
varBinaryValue = xDecrypter.FromString(strBinaryValue);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return varBinaryValue;
}
</code>
From the ASP code I saw that the result of the call to the Decrypt method is simply cast to a string and there you get your resulting decrypted string. This won’t work in C#, at least I can’t get it to so I just have the byte array:
<code>
varDataToDecrypt = HexToBinary(strEncrypt, xDecrypter);
varDataDecypted = (
byte[])xDecrypter.Decrypt(ref varDataToDecrypt, true);
</code>
I am getting a variant byte array back from the Decrypt method in C# so it looks like it good until I need to return the actual decrypted string. I’m just not sure how to get the resulting string from the byte array. I’m not all that great with C# yet (if you can’t tell) so I apologize for any sloppy code.
Thanks in advance,
Tony
Imported from legacy forums. Posted by Tony (had 3995 views)
Hi Tony,
To get the resulting string, data, from your byte array, varDataDecypted, you will need to use a Text Encoding as follow:
string data = System.Text.Encoding.Unicode.GetString( ( byte[] )varDataDecypted );
Regards,
Imported from legacy forums. Posted by Mohamed [Xceed] (had 514 views)
Thanks for the reply Mohamed.
It returned part of the string I was looking for but most of it was just garbled, like special character boxes. I must have not set things up right.
Imported from legacy forums. Posted by Tony (had 5744 views)
-
AuthorPosts
- You must be logged in to reply to this topic.