Ejemplo n.º 1
0
        /// <summary>
        /// Encrypts bytes with the specified key.
        /// </summary>
        public static Byte[] Encrypt(Byte[] key, Byte[] bytes)
        {
            Check(key, bytes);
            UInt32[] k = UTILS.Pack(key);
            UInt32[] v = UTILS.Pack(bytes);
            if (v.Length <= 1)
            {
                v[1] = 0;
            }
            Int32  n = v.Length; Int32 q = 6 + 52 / n;
            UInt32 z = v[n - 1], m, y = v[0], d = 0x9E3779B9, s = 0, e;

            while (q-- > 0)
            {
                s = unchecked (s + d);
                e = s >> 2 & 3;
                for (Int32 i = 0; i < n; i++)
                {
                    y = v[(i + 1) % n];
                    m = unchecked ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (s ^ y) + (k[i & 3 ^ e] ^ z));
                    z = v[i] = unchecked (v[i] + m);
                }
            }
            return(UTILS.Unpack(v));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Decrypts bytes with the specified key.
        /// </summary>
        public static Byte[] Decrypt(Byte[] key, Byte[] bytes)
        {
            Check(key, bytes);
            UInt32[] k = UTILS.Pack(key);
            UInt32[] v = UTILS.Pack(bytes);
            Int32    n = v.Length, q = 6 + 52 / n;
            UInt32   z = v[n - 1], y = v[0], d = 0x9E3779B9, e;
            UInt32   m, s = unchecked ((UInt32)(q * d));

            while (s != 0)
            {
                e = s >> 2 & 3;
                for (Int32 i = n - 1; i >= 0; i--)
                {
                    z = v[i > 0 ? i - 1 : n - 1];
                    m = unchecked ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (s ^ y) + (k[i & 3 ^ e] ^ z));
                    y = v[i] = unchecked (v[i] - m);
                }
                s = unchecked (s - d);
            }
            return(UTILS.Unpack(v));
        }