MooksUK
asked on
c# Basic Encyption/Decryption
Hi,
I have created a function based upon something from VB6 which allows me to encrypt/decrypt some basic information. The encryption works fine but when I attempt to use the same function to decrypt not all characters are converted correctly.
I can not change the encryption method from this Xor method as the data which is encrypted is used by many programs written in different languages. Hence I have adopted it from VB6.
Any ideas why it would encrypt OK but have problems decrypting?? Any help greatly appreciated.
public static string EncryptPW(string p)
{
//Encrypt password
string strResult = "";
char testChar;
for (int i = 0; i < p.Length; i++)
{
testChar = Convert.ToChar(p.Substring (i, 1));
strResult += (char)(testChar ^ 255);
}
return strResult;
}
I have created a function based upon something from VB6 which allows me to encrypt/decrypt some basic information. The encryption works fine but when I attempt to use the same function to decrypt not all characters are converted correctly.
I can not change the encryption method from this Xor method as the data which is encrypted is used by many programs written in different languages. Hence I have adopted it from VB6.
Any ideas why it would encrypt OK but have problems decrypting?? Any help greatly appreciated.
public static string EncryptPW(string p)
{
//Encrypt password
string strResult = "";
char testChar;
for (int i = 0; i < p.Length; i++)
{
testChar = Convert.ToChar(p.Substring
strResult += (char)(testChar ^ 255);
}
return strResult;
}
Check the following thread for Encrypt / Decrypt
http://stackoverflow.com/questions/212510/c-what-is-the-easiest-way-to-encrypt-a-password-when-i-save-it-to-the-registry
http://stackoverflow.com/questions/212510/c-what-is-the-easiest-way-to-encrypt-a-password-when-i-save-it-to-the-registry
ASKER
I am afraid I must use the same encryption/decryption method. In VB6 when supplying an encrypted password the function decrypts it and vice versa. I understand that this is very basic encryption and that there are better methods but this is only for user signin to a network app and I have no choice but to use this method.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Axhun for the investigation.
For example, the password of "pass1" is encrypted OK. I know this because I compare the store encrypted value against an encryption of a value entered by the user and they match. However, if I attempt to decrypt the password then is becoms "pÀÀÀ1".
Any further ideas?
For example, the password of "pass1" is encrypted OK. I know this because I compare the store encrypted value against an encryption of a value entered by the user and they match. However, if I attempt to decrypt the password then is becoms "pÀÀÀ1".
Any further ideas?
I suspect it has something to do with how you store and retrieve the encrypted passwords. You may be altering your characters with the wrong encoding? Set some breakpoints where you store or retrieve the password and see if the characters match what is stored.
Any luck with this yet?
http://www.codeproject.com/KB/security/DotNetCrypto.aspx