예제 #1
0
        public static void EncryptMessage(Internal.CommunicationStream stream, int payloadStart, int blockSize)
        {
            blockSize       = Math.Min(blockSize, (int)stream.Length - payloadStart);
            stream.Position = payloadStart + blockSize;

            int length = (int)(Math.Floor((blockSize + RSABlockSize - 1D) / RSABlockSize) * RSABlockSize);

            if (length > blockSize)
            {
                var tmp = new byte[length - blockSize];
                s_Random.NextBytes(tmp);
                stream.Write(tmp, 0, tmp.Length);
                blockSize = length;
            }

            stream.Position = payloadStart;
            var bytes = ProcessBlock(stream, payloadStart, RSABlockSize);

            stream.Write(bytes, 0, bytes.Length);
        }
예제 #2
0
        public static void EncryptMessage(Internal.CommunicationStream stream, int payloadStart, int blockSize)
        {
            blockSize       = Mathf.Min(blockSize, (int)stream.Length - payloadStart);
            stream.Position = payloadStart + blockSize;

            int length = (int)(Mathf.Floor((blockSize + RSABlockSize - 1f) / RSABlockSize) * RSABlockSize);

            if (length > blockSize)
            {
                var tmp = new byte[length - blockSize];
                for (int i = 0; i < tmp.Length; i++)
                {
                    tmp[i] = (byte)Random.Range(0, 255);
                }
                stream.Write(tmp, 0, tmp.Length);
                blockSize = length;
            }

            stream.Position = payloadStart;
            var bytes = ProcessBlock(stream, payloadStart, RSABlockSize);

            stream.Write(bytes, 0, bytes.Length);
        }
예제 #3
0
        public int Encrypt(Internal.CommunicationStream message, int offset = 0, int length = int.MaxValue)
        {
            length           = Mathf.Min(length, (int)message.Length - offset);
            message.Position = offset + length;

            int encryptedLength = (int)(Mathf.Floor((length + BlockSize - 1f) / BlockSize) * BlockSize);

            if (encryptedLength > length)
            {
                byte[] tmp = new byte[encryptedLength - length];
                for (int i = 0; i < tmp.Length; i++)
                {
                    tmp[i] = (byte)Random.Range(0, 255);
                }

                message.Write(tmp, 0, tmp.Length);
                length = encryptedLength;
            }

            int s = offset;

            while (s < offset + length)
            {
                message.Position = s;
                uint v0    = message.ReadUnsignedInt();
                uint v1    = message.ReadUnsignedInt();
                uint delta = 0x61C88647;
                uint sum   = 0;
                for (int r = 0; r < 32; r++)
                {
                    v0  += (v1 << 4 ^ v1 >> 5) + v1 ^ sum + _key[sum & 3];
                    sum -= delta;
                    v1  += (v0 << 4 ^ v0 >> 5) + v0 ^ sum + _key[sum >> 11 & 3];
                }

                message.Position -= BlockSize;
                message.WriteUnsignedInt(v0);
                message.WriteUnsignedInt(v1);

                s += BlockSize;
            }

            return(length);
        }