Encryption has many issues. It can be very simple or very complex. I personally don’t have that many secrets, but when the company I worked for wanted to encrypt something I used the following : XOR !
What is nice about xor is that you can use the same method to encrypt and to decipher your message.
message xor key = secret
secret xor key = message
If you xor a string with one key-value, you always xoring the same char with the same key, so it is relatively easy to decipher the encrypted message. I made use of a key which is say 7 chars long (the longer the better), which I repeatedly xored over the messagestring. That way, the same chars where xored by different other chars. To decipher that is much harder. Look at the code snippet for more details.
Data encryption in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace StringEncryption
{
class Program
{
static void Main(string[] args)
{
// the message to encrypt
string messageStr = "Oh what a beautifull morning, oh what a beautifull day!";
// the secret key used, can be anything make it at least 2 chars long
string keyStr = "ABCDEFG";
Console.WriteLine("Encrypted string:");
StringBuilder OutStr = StringEncrypt(messageStr, keyStr);
Console.WriteLine(OutStr.ToString());
Console.WriteLine("Deciphered string:");
StringBuilder OutStr2 = StringEncrypt(OutStr.ToString(), keyStr);
Console.WriteLine(OutStr2.ToString());
Console.ReadKey();
}
private static StringBuilder StringEncrypt(string messageStr, string keyStr)
{
const int EOS = -1; //end of string condition
StringReader Msr = new StringReader(messageStr);
StringBuilder OutStr = new StringBuilder();
// alternatively BinaryReader and BinaryWriter could be used here
int keyLength = keyStr.Length - 1;
int index = 0;
char ch;
int x;
do
{
x = Msr.Read() ^ keyStr[index]; //XOR
ch = Convert.ToChar(x);
OutStr.Append(ch);
index++;
if (index > keyLength) index = 0; //start all over with the key
} while (Msr.Peek() != EOS);
return OutStr;
}
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.