Link to home
Start Free TrialLog in
Avatar of MooksUK

asked on

c# Basic Encyption/Decryption


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;
Avatar of Om Prakash
Om Prakash
Flag of India image

check the following link for simple encryption/decryption
Avatar of Kalpesh Chhatrala
Avatar of MooksUK


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.
Avatar of Chuck Yetter
Chuck Yetter
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MooksUK


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