Data encryption in C#

Please support our C# advertiser: Intel Parallel Studio Home
ddanbe ddanbe is online now Online Nov 25th, 2008, 8:08 am |
0
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.
Quick reply to this message  
C# Syntax
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace StringEncryption
  8. {
  9. class Program
  10. {
  11.  
  12. static void Main(string[] args)
  13. {
  14. // the message to encrypt
  15. string messageStr = "Oh what a beautifull morning, oh what a beautifull day!";
  16. // the secret key used, can be anything make it at least 2 chars long
  17. string keyStr = "ABCDEFG";
  18.  
  19. Console.WriteLine("Encrypted string:");
  20. StringBuilder OutStr = StringEncrypt(messageStr, keyStr);
  21. Console.WriteLine(OutStr.ToString());
  22.  
  23. Console.WriteLine("Deciphered string:");
  24. StringBuilder OutStr2 = StringEncrypt(OutStr.ToString(), keyStr);
  25. Console.WriteLine(OutStr2.ToString());
  26. Console.ReadKey();
  27. }
  28.  
  29. private static StringBuilder StringEncrypt(string messageStr, string keyStr)
  30. {
  31. const int EOS = -1; //end of string condition
  32.  
  33. StringReader Msr = new StringReader(messageStr);
  34. StringBuilder OutStr = new StringBuilder();
  35. // alternatively BinaryReader and BinaryWriter could be used here
  36. int keyLength = keyStr.Length - 1;
  37. int index = 0;
  38. char ch;
  39. int x;
  40.  
  41. do
  42. {
  43. x = Msr.Read() ^ keyStr[index]; //XOR
  44. ch = Convert.ToChar(x);
  45. OutStr.Append(ch);
  46. index++;
  47. if (index > keyLength) index = 0; //start all over with the key
  48. } while (Msr.Peek() != EOS);
  49.  
  50. return OutStr;
  51. }
  52. }
  53. }
0
MosaicFuneral MosaicFuneral is offline Offline | Nov 25th, 2008
The char string method is cool, I just used the current position of the string as the XOR value, after some other ciphering, then inflating the string with gibberish to avoid them from knowing the size to reverse it, et al.; but I might try this technique out, since it's more compact, and not as analyzable by finding value averages.
Only bad part is that the XOR trick can be deciphered with loops(and other tricks), in seconds.

Good work, five stars!
 
0
Ramy Mahrous Ramy Mahrous is offline Offline | Jan 9th, 2009
Five stars (Y)
 
 

Message:


Similar Threads
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC