c# Basic Encyption/Decryption

MooksUK used Ask the Experts™

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;
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010

check the following link for simple encryption/decryption


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.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

I tried your function in a simple program and it appeared to work fine for all characters.  Do you have an example of a character which fails to decrypt properly?  

I have used a similar encryption/decryption method before and ran into a problem where sometimes my encryption resulted in a '\0' character in the encrypted string, which caused problems when trying to save the string for later use.  I overcame that problem by converting my encrypted string to hex before storage, and then back from hex before decrypting.  I was using xor and bit shifting, so this may not be the problem you are seeing.


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?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial