Exemplo n.º 1
0
 /// <summary>
 /// Update the decryption keys
 /// </summary>
 /// <param name="ch">Character to update the keys with</param>
 protected void UpdateKeys(byte ch)
 {
     keys[0] = Crc32.ComputeCrc32(keys[0], ch);
     keys[1] = keys[1] + (byte)keys[0];
     keys[1] = keys[1] * 134775813 + 1;
     keys[2] = Crc32.ComputeCrc32(keys[2], (byte)(keys[1] >> 24));
 }
Exemplo n.º 2
0
        /// <summary>
        /// Generates new encryption keys based on given seed
        /// </summary>
        /// <param name="seed">The seed value to initialise keys with.</param>
        /// <returns>A new key value.</returns>
        static public byte[] GenerateKeys(byte[] seed)
        {
            if (seed == null)
            {
                throw new ArgumentNullException("seed");
            }

            if (seed.Length == 0)
            {
                throw new ArgumentException("Length is zero", "seed");
            }

            uint[] newKeys = new uint[] {
                0x12345678,
                0x23456789,
                0x34567890
            };

            for (int i = 0; i < seed.Length; ++i)
            {
                newKeys[0] = Crc32.ComputeCrc32(newKeys[0], seed[i]);
                newKeys[1] = newKeys[1] + (byte)newKeys[0];
                newKeys[1] = newKeys[1] * 134775813 + 1;
                newKeys[2] = Crc32.ComputeCrc32(newKeys[2], (byte)(newKeys[1] >> 24));
            }

            byte[] result = new byte[12];
            result[0]  = (byte)(newKeys[0] & 0xff);
            result[1]  = (byte)((newKeys[0] >> 8) & 0xff);
            result[2]  = (byte)((newKeys[0] >> 16) & 0xff);
            result[3]  = (byte)((newKeys[0] >> 24) & 0xff);
            result[4]  = (byte)(newKeys[1] & 0xff);
            result[5]  = (byte)((newKeys[1] >> 8) & 0xff);
            result[6]  = (byte)((newKeys[1] >> 16) & 0xff);
            result[7]  = (byte)((newKeys[1] >> 24) & 0xff);
            result[8]  = (byte)(newKeys[2] & 0xff);
            result[9]  = (byte)((newKeys[2] >> 8) & 0xff);
            result[10] = (byte)((newKeys[2] >> 16) & 0xff);
            result[11] = (byte)((newKeys[2] >> 24) & 0xff);
            return(result);
        }