Advertisement
Guest User

Untitled

a guest
Apr 9th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1.  static void Main(string[] args)
  2.         {
  3.             String dataStr = Console.ReadLine();
  4.             String keyStr = Console.ReadLine();
  5.  
  6.             byte[] dataBytes = GetBytes(dataStr);
  7.             byte[] keyBytes = GetBytes(keyStr);
  8.  
  9.             uint[] uKeyArray = new uint[4];
  10.  
  11.             int i = 0;
  12.  
  13.             foreach(byte element in keyBytes)
  14.             {
  15.                 if (i > 4)
  16.                 {
  17.                     uint uKey = Convert.ToUInt32(element);
  18.                     i++;
  19.                 }
  20.             }
  21.  
  22.             int length = dataBytes.Length;
  23.             byte[] buffer = dataBytes;
  24.  
  25.             bool dataBool = Encrypt(ref buffer, ref length, 0, uKeyArray);
  26.             Console.WriteLine(BitConverter.ToString(buffer));
  27.             Console.Read();
  28.         }
  29.  
  30.         public unsafe static bool Encrypt(ref byte[] buffer, ref int length, int index, uint[] key)
  31.         {
  32.             if (key == null)
  33.                 return false;
  34.  
  35.             int msgSize = length - index;
  36.  
  37.             int pad = msgSize % 8;
  38.             if (pad > 0)
  39.             {
  40.                 msgSize += (8 - pad);
  41.                 length = index + msgSize;
  42.             }
  43.  
  44.             fixed (byte* bufferPtr = buffer)
  45.             {
  46.                 uint* words = (uint*)(bufferPtr + index);
  47.  
  48.                 for (int pos = 0; pos < msgSize / 4; pos += 2)
  49.                 {
  50.                     uint x_sum = 0, x_delta = 0x9e3779b9, x_count = 32;
  51.  
  52.                     while (x_count-- > 0)
  53.                     {
  54.                         words[pos] += (words[pos + 1] << 4 ^ words[pos + 1] >> 5) + words[pos + 1] ^ x_sum + key[x_sum & 3];
  55.                         x_sum += x_delta;
  56.                         words[pos + 1] += (words[pos] << 4 ^ words[pos] >> 5) + words[pos] ^ x_sum + key[x_sum >> 11 & 3];
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             return true;
  62.         }
  63.  
  64.         static byte[] GetBytes(String str)
  65.         {
  66.             byte[] bytes = new byte[str.Length * sizeof(char)];
  67.  
  68.             System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
  69.  
  70.             return bytes;
  71.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement