Esempio n. 1
0
        public byte[] Encrypt(byte[] uncryptedSignature, uint msSinceStart)
        {
            var rnd = new Rand(msSinceStart);

            object[] key = TwoFish.MakeKey(KEY);

            var xor_byte = new byte[TwoFish.BLOCK_SIZE];

            for (int i = 0; i < TwoFish.BLOCK_SIZE; ++i)
            {
                xor_byte[i] = (byte)rnd.Next();
            }

            int block_count = (uncryptedSignature.Length + 256) / 256;
            int output_size = 4 + (block_count * 256) + 1;
            var output      = new byte[output_size];

            output[0] = (byte)(msSinceStart >> 24);
            output[1] = (byte)(msSinceStart >> 16);
            output[2] = (byte)(msSinceStart >> 8);
            output[3] = (byte)msSinceStart;


            Array.Copy(uncryptedSignature, 0, output, 4, uncryptedSignature.Length);

            output[output_size - 2] = (byte)(256 - uncryptedSignature.Length % 256);


            for (int offset = 0; offset < block_count * 256; offset += TwoFish.BLOCK_SIZE)
            {
                for (int i = 0; i < TwoFish.BLOCK_SIZE; i++)
                {
                    output[4 + offset + i] ^= xor_byte[i];
                }

                byte[] block = TwoFish.BlockEncrypt(output, offset + 4, key);
                Array.Copy(block, 0, output, offset + 4, block.Length);
                Array.Copy(output, 4 + offset, xor_byte, 0, TwoFish.BLOCK_SIZE);
            }

            // -------------------------------------  NEW CODE START HERE GUYS

            // Integrity byte is 0x23 since 05/01/2017.
            output[output_size - 1] = 0x23;

            // This new "on top" encryption is there since 05/01/2017.
            encrypt_cipher(output, output_size);

            return(output);
        }
Esempio n. 2
0
        /**
         * Encrypts the given signature
         *
         * @param input input data
         * @param msSinceStart time since start
         * @return encrypted signature
         */
        public byte[] Encrypt(byte[] input, uint msSinceStart)
        {
            try
            {
                object[] key = TwoFish.MakeKey(KEY);

                Rand   rand       = new Rand(msSinceStart);
                byte[] iv         = MakeIv(rand);
                int    blockCount = (input.Length + 256) / 256;
                int    outputSize = (blockCount * 256) + 5;
                byte[] output     = new byte[outputSize];

                output[0] = (byte)(msSinceStart >> 24);
                output[1] = (byte)(msSinceStart >> 16);
                output[2] = (byte)(msSinceStart >> 8);
                output[3] = (byte)msSinceStart;

                Array.Copy(input, 0, output, 4, input.Length);
                output[outputSize - 2] = (byte)(256 - input.Length % 256);

                for (int offset = 0; offset < blockCount * 256; offset += TwoFish.BLOCK_SIZE)
                {
                    for (int i = 0; i < TwoFish.BLOCK_SIZE; i++)
                    {
                        output[4 + offset + i] ^= iv[i];
                    }

                    byte[] block = TwoFish.BlockEncrypt(output, offset + 4, key);
                    Array.Copy(block, 0, output, offset + 4, block.Length);
                    Array.Copy(output, 4 + offset, iv, 0, TwoFish.BLOCK_SIZE);
                }

                output[outputSize - 1] = MakeIntegrityByte(rand);
                return(output);
            }
            catch (Exception)
            {
                return(null);
            }
        }