Ejemplo n.º 1
0
        public void Negotiate()
        {
            RSAKeys rsaKeys = new RSAKeys();

            BaseStream.Write(rsaKeys.PublicKey);
            RSAParameters localPrivateKey = rsaKeys.PrivateKey;

            BaseStream.Flush();
            RSAParameters remotePublicKey = BaseStream.ReadRSAParameters();

            Random rnd     = Processor.CreateRandom();
            int    nbTries = 0;

            byte[] id;
            do
            {
                if (nbTries > 100)
                {
                    throw new IOException("Can't find a different ID to use.");
                }

                this.LocalID = rnd.Next();
                id           = this.LocalID.GetBytes();

                BaseStream.WriteWrapped(RSA.Encrypt(id, remotePublicKey));
                this.RemoteID = RSA.Decrypt(BaseStream.ReadWrapped(), localPrivateKey).ToInt32();

                ++nbTries;
            } while(this.LocalID == this.RemoteID);

            byte[] key;
            byte[] iv;

            if (this.RemoteID < LocalID)
            {
                AES.ProduceKeyIV(out key, out iv);

                BaseStream.WriteWrapped(RSA.Encrypt(key, remotePublicKey));
                BaseStream.WriteWrapped(RSA.Encrypt(iv, remotePublicKey));
                BaseStream.Flush();
            }
            else
            {
                key = RSA.Decrypt(BaseStream.ReadWrapped(), localPrivateKey);
                iv  = RSA.Decrypt(BaseStream.ReadWrapped(), localPrivateKey);
            }

            this.EncryptedStream = new OverAESStream(BaseStream, key, iv, NbIterations);

            this.WriteWrapped(id);
            this.Flush();

            if (this.ReadWrapped().ToInt32() != RemoteID)
            {
                throw new IOException("Can't connect to remote point.");
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Decrypt data with the private part of the key provided.
 /// </summary>
 public static byte[] Decrypt(byte[] data, RSAKeys keys, bool padding = true)
 {
     return(Decrypt(data, keys.PrivateKey, padding));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Encrypt data with the public part of the key provided.
 /// </summary>
 public static byte[] Encrypt(byte[] data, RSAKeys keys, bool padding = true)
 {
     return(Encrypt(data, keys.PublicKey, padding));
 }