Example #1
0
        //public static void Decrypt(byte[] indexBuffer)
        //{
        //    CryptState cryptState = new CryptState(indexBuffer);
        //    while (cryptState.Offset < cryptState.Size)
        //    {
        //        cryptState.Data[cryptState.Offset] -= _GetCryptByte(cryptState);
        //        cryptState.Offset++;
        //    }
        //}
        public static void Encrypt(byte[] indexBuffer)
        {
            CryptState cryptState = new CryptState(indexBuffer);

            while (cryptState.Offset < cryptState.Size)
            {
                cryptState.Data[cryptState.Offset] += _GetCryptByte(cryptState);
                cryptState.Offset++;
            }
        }
Example #2
0
        //public static void Decrypt(byte[] indexBuffer)
        //{
        //    CryptState cryptState = new CryptState(indexBuffer);

        //    while (cryptState.Offset < cryptState.Size)
        //    {
        //        cryptState.Data[cryptState.Offset] -= _GetCryptByte(cryptState);
        //        cryptState.Offset++;
        //    }
        //}

        public static void Encrypt(byte[] indexBuffer)
        {
            CryptState cryptState = new CryptState(indexBuffer);

            while (cryptState.Offset < cryptState.Size)
            {
                cryptState.Data[cryptState.Offset] += _GetCryptByte(cryptState);
                cryptState.Offset++;
            }
        }
Example #3
0
        private static byte _GetCryptByte(CryptState cryptState)
        {
            UInt32 value = cryptState.Offset / (CryptState.BlockSize * sizeof(Int32)) * (CryptState.BlockSize * sizeof(Int32));

            if (cryptState.BlockIndex != value)
            {
                cryptState.BlockIndex = value;
                value += CryptState.Key3;
                for (int i = 0; i < CryptState.BlockSize; i++)
                {
                    value = (value * CryptState.Key1) + CryptState.Key2;
                    byte[] bytes = BitConverter.GetBytes(value);
                    Buffer.BlockCopy(bytes, 0, cryptState.Table, i * sizeof(UInt32), bytes.Length);
                }
            }

            return(cryptState.Table[cryptState.Offset - cryptState.BlockIndex]);
        }
Example #4
0
    public void TestCanEncryptAndDecrypt()
    {
        CryptState encoderState = new CryptState();
        CryptState decoderState = new CryptState();
        CryptSetup encoderSetup = new CryptSetup();
        CryptSetup decoderSetup = new CryptSetup();

        // Make the key and nonces random
        encoderSetup.Key         = GetRandomArray(16);
        encoderSetup.ClientNonce = GetRandomArray(16);
        encoderSetup.ServerNonce = GetRandomArray(16);

        // The decoder uses the same stuff, but with client/server nonce exchanged with one another
        decoderSetup.Key         = encoderSetup.Key;
        decoderSetup.ClientNonce = (byte[])encoderSetup.ServerNonce.Clone();
        decoderSetup.ServerNonce = (byte[])encoderSetup.ClientNonce.Clone();

        encoderState.CryptSetup = encoderSetup;
        decoderState.CryptSetup = decoderSetup;

        byte[] buffer = GetRandomArray(buffer_length);

        for (int trial = 0; trial < num_trials; trial++)
        {
            Randomize(buffer);
            byte[] encrypted = encoderState.Encrypt(buffer, buffer_length);
            byte[] decrypted = decoderState.Decrypt(encrypted, encrypted.Length);

            if (encrypted == null)
            {
                Debug.Log("Nothing decrypted?");
            }
            if (decrypted == null)
            {
                Debug.Log("Nothing decrypted?");
            }

            for (int i = 0; i < buffer_length; i++)
            {
                Assert.AreEqual(buffer[i], decrypted[i]);
            }
        }
        Debug.Log("Done");
    }
Example #5
0
        private static byte _GetCryptByte(CryptState cryptState)
        {
            UInt32 value = cryptState.Offset / (CryptState.BlockSize * sizeof(Int32)) * (CryptState.BlockSize * sizeof(Int32));

            if (cryptState.BlockIndex != value)
            {
                cryptState.BlockIndex = value;
                value += CryptState.Key3;
                for (int i = 0; i < CryptState.BlockSize; i++)
                {
                    value = (value * CryptState.Key1) + CryptState.Key2;
                    byte[] bytes = BitConverter.GetBytes(value);
                    Buffer.BlockCopy(bytes, 0, cryptState.Table, i * sizeof(UInt32), bytes.Length);
                }
            }

            return cryptState.Table[cryptState.Offset - cryptState.BlockIndex];
        }