Home
Manage Your Code
Snippet: simple XOR encryption (C#)
Title: simple XOR encryption Language: C#
Description: a simple encryptor/decryptor Views: 118
Author: sabareesh krishnamoorthy Date Added: 6/17/2008
Copy Code  
1 /// <summary>
2    /// Class to encrypt and decrypt the password
3    /// </summary>
4    public static class EncryptorDecryptor
5    {
6        public static int key = 3;
7
8        /// <summary>
9        /// Encrypts and decrypts a given string.
10        /// </summary>
11        /// <param name="textToEncrypt">The text to encrypt or decrypt.</param>
12        /// <returns></returns>
13        public static string EncryptDecrypt(string textToEncrypt)
14        {
15            StringBuilder inSb = new StringBuilder(textToEncrypt);
16            StringBuilder outSb = new StringBuilder(textToEncrypt.Length);
17            char c;
18            for (int i = 0; i < textToEncrypt.Length; i++)
19            {
20                c = inSb[i];
21                c = (char)(c ^ key);
22                outSb.Append(c);
23            }
24            return outSb.ToString();
25        }
26    }
Usage
EncryptorDecryptor.EncryptDecrypt("teststring");
Notes
call the same method with the same key to encrypt and decrypt a string