コード例 #1
0
ファイル: Handshaker.cs プロジェクト: ilynx/ilynx.common
        /// <summary>
        /// Gets an <see cref="EncryptionProvider"/> constructed with the Key and IV contained in the specified packet
        /// <para/>
        /// This method is the counterpart to <see cref="HandshakeHelper.WriteEncryptor(RSAHelper, EncryptionProvider)"/>
        /// </summary>
        /// <param name="rsa">The "local" <see cref="RSAHelper"/> to use for decrypting the Key and IV</param>
        /// <param name="pkt">The received packet</param>
        /// <returns></returns>
        public static EncryptionProvider GetDecryptor(RSAHelper rsa, Packet pkt)
        {
            MemoryStream dataStream = new MemoryStream(pkt.Data); // Creating a stream around the packet data for reading
            StreamReader dataReader = new StreamReader(dataStream);

            byte[] iv  = rsa.DecryptBase64String(dataReader.ReadLine());
            byte[] key = rsa.DecryptBase64String(dataReader.ReadLine());

            return(new EncryptionProvider(key, iv));
        }
コード例 #2
0
ファイル: Handshaker.cs プロジェクト: ilynx/ilynx.common
        private bool NegotiateSessionKeys(Stream netStream, out EncryptionProvider decryptor, out EncryptionProvider encryptor, RSAHelper pubRSA, RSAHelper privRSA)
        {
            StreamReader reader = new StreamReader(netStream);
            StreamWriter writer = new StreamWriter(netStream);

            encryptor = new EncryptionProvider();
            try
            {
                Packet packet           = WriteEncryptor(pubRSA, encryptor);
                byte[] serializedPacket = ToySerializer.Serialize(packet);
                string sendData         = pubRSA.EncryptToBase64String(serializedPacket);
                writer.WriteLine(sendData); // Writing the packet as a Base64 encoded string to the network stream in the current instance
                writer.Flush();

                string read = reader.ReadLine(); // Getting response
                packet    = ToySerializer.Deserialize <Packet>(privRSA.DecryptBase64String(read));
                decryptor = GetDecryptor(privRSA, packet);
            }
            catch { decryptor = null; return(false); }
            return(true);
        }