Guest User

Untitled

a guest
Nov 29th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication2
  7. {
  8. class Program
  9. {
  10. static public byte Xor(byte a, byte b)
  11. {
  12. byte result = 0;
  13. bool bitA, bitB, bitXor;
  14. for (int i = 7; i >= 0; i--)
  15. {
  16. bitA = ((1 << i) & a) != 0;
  17. bitB = ((1 << i) & b) != 0;
  18. bitXor = (!bitA && bitB) || (bitA && !bitB);
  19. result |= Convert.ToByte(bitXor);
  20. if (i > 0)
  21. {
  22. result = (byte)(result << 1);
  23. }
  24. }
  25. return result;
  26. }
  27.  
  28. static void Main(string[] args)
  29. {
  30. var input = Encoding.UTF8.GetBytes("Hello world!");
  31. var key = Encoding.UTF8.GetBytes("i'm supa xor");
  32. var result = new List<byte>();
  33.  
  34. for (int i = 0; i < input.Length; i++)
  35. {
  36. result.Add(Xor(input[i], key[i]));
  37. Console.WriteLine("{0} xor {1} = {2}", input[i], key[i], Xor(input[i], key[i]));
  38. }
  39. Console.WriteLine("Bytes: {0}", result.ConvertAll(b => b.ToString()).Aggregate((a, b) => a + " " + b));
  40. Console.WriteLine("String: {0}", Encoding.UTF8.GetString(result.ToArray()));
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment