Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- String dataStr = Console.ReadLine();
- String keyStr = Console.ReadLine();
- byte[] dataBytes = GetBytes(dataStr);
- byte[] keyBytes = GetBytes(keyStr);
- uint[] uKeyArray = new uint[4];
- int i = 0;
- foreach(byte element in keyBytes)
- {
- if (i > 4)
- {
- uint uKey = Convert.ToUInt32(element);
- i++;
- }
- }
- int length = dataBytes.Length;
- byte[] buffer = dataBytes;
- bool dataBool = Encrypt(ref buffer, ref length, 0, uKeyArray);
- Console.WriteLine(BitConverter.ToString(buffer));
- Console.Read();
- }
- public unsafe static bool Encrypt(ref byte[] buffer, ref int length, int index, uint[] key)
- {
- if (key == null)
- return false;
- int msgSize = length - index;
- int pad = msgSize % 8;
- if (pad > 0)
- {
- msgSize += (8 - pad);
- length = index + msgSize;
- }
- fixed (byte* bufferPtr = buffer)
- {
- uint* words = (uint*)(bufferPtr + index);
- for (int pos = 0; pos < msgSize / 4; pos += 2)
- {
- uint x_sum = 0, x_delta = 0x9e3779b9, x_count = 32;
- while (x_count-- > 0)
- {
- words[pos] += (words[pos + 1] << 4 ^ words[pos + 1] >> 5) + words[pos + 1] ^ x_sum + key[x_sum & 3];
- x_sum += x_delta;
- words[pos + 1] += (words[pos] << 4 ^ words[pos] >> 5) + words[pos] ^ x_sum + key[x_sum >> 11 & 3];
- }
- }
- }
- return true;
- }
- static byte[] GetBytes(String str)
- {
- byte[] bytes = new byte[str.Length * sizeof(char)];
- System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
- return bytes;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement