コード例 #1
0
        private void ExchangeKeys(Stream baseStream, ISymmetricAlgorithmDescriptor transportAlgorithmDescriptor, IKeyExchangeAlgorithm keyExchangeAlgorithm, out ICryptoTransform decryptor, out ICryptoTransform encryptor)
        {
            var encryptionAlgorithm = transportAlgorithmDescriptor.Build();
            var key = new byte[encryptionAlgorithm.KeySize / 8];
            var iv  = new byte[encryptionAlgorithm.BlockSize / 8];

            prng.GetBytes(key);
            prng.GetBytes(iv);
            encryptionAlgorithm.Key = key;
            encryptionAlgorithm.IV  = iv;
            var keyPackage = new KeyPackage
            {
                Key = encryptionAlgorithm.Key,
                InitializationVector = encryptionAlgorithm.IV,
            };

            byte[] keyBuffer;
            using (var memoryStream = new MemoryStream())
            {
                Serializer.Serialize(memoryStream, keyPackage);
                keyBuffer = keyExchangeAlgorithm.Encrypt(memoryStream.ToArray());
            }
            var buffer = bitConverter.GetBytes(keyBuffer.Length);

            baseStream.Write(buffer, 0, buffer.Length);
            baseStream.Write(keyBuffer, 0, keyBuffer.Length);
            buffer = new byte[sizeof(int)];
            baseStream.Read(buffer, 0, buffer.Length);
            var length = bitConverter.ToInt32(buffer);

            if (0 > length)
            {
                throw new InvalidDataException();
            }
            buffer = new byte[length];
            baseStream.Read(buffer, 0, buffer.Length);
            buffer = keyExchangeAlgorithm.Decrypt(buffer);
            var        decryptionAlgorithm = transportAlgorithmDescriptor.Build();
            KeyPackage remoteKeyPackage;

            using (var memoryStream = new MemoryStream(buffer))
                remoteKeyPackage = Serializer.Deserialize <KeyPackage>(memoryStream);
            decryptionAlgorithm.Key = remoteKeyPackage.Key;
            decryptionAlgorithm.IV  = remoteKeyPackage.InitializationVector;
            encryptor = encryptionAlgorithm.CreateEncryptor();
            decryptor = decryptionAlgorithm.CreateDecryptor();
        }
コード例 #2
0
        private static void WriteMap(Stream stream, MPack mpack)
        {
            MPackMap map = mpack as MPackMap;

            if (map == null)
            {
                throw new InvalidOperationException("A call to WriteMap can not occur unless type is of MsgPackMap");
            }

            byte b;

            byte[] lenBytes;
            int    len = map.Count;

            if (len <= 15)
            {
                b = (byte)(0x80 + (byte)len);
                stream.WriteByte(b);
            }
            else if (len <= 65535)
            {
                b = 0xDE;
                stream.WriteByte(b);

                lenBytes = _convert.GetBytes((UInt16)len);
                stream.Write(lenBytes, 0, lenBytes.Length);
            }
            else
            {
                b = 0xDF;
                stream.WriteByte(b);
                lenBytes = _convert.GetBytes((UInt32)len);
                stream.Write(lenBytes, 0, lenBytes.Length);
            }

            foreach (KeyValuePair <MPack, MPack> child in map)
            {
                EncodeToStream(stream, child.Key);
                EncodeToStream(stream, child.Value);
            }
        }