コード例 #1
0
ファイル: EncryptedSocket.cs プロジェクト: zjklee/monotorrent
        /// <summary>
        /// Sets CryptoSelect and initializes the stream encryptor and decryptor based on the selected method.
        /// </summary>
        /// <param name="remoteCryptoBytes">The cryptographic methods supported/wanted by the remote client in CryptoProvide format. The highest order one available will be selected</param>
        /// <param name="replace">True if the existing Encryptor/Decryptor object should be replaced with a new instance</param>
        protected virtual int SelectCrypto(byte[] remoteCryptoBytes, bool replace)
        {
            CryptoSelect = new byte[remoteCryptoBytes.Length];

            // '2' corresponds to RC4Full
            if ((remoteCryptoBytes[3] & 2) == 2 && allowedEncryption.HasFlag(EncryptionTypes.RC4Full))
            {
                CryptoSelect[3] |= 2;
                if (replace)
                {
                    Encryptor = encryptor;
                    Decryptor = decryptor;
                }
                return(2);
            }

            // '1' corresponds to RC4Header
            if ((remoteCryptoBytes[3] & 1) == 1 && allowedEncryption.HasFlag(EncryptionTypes.RC4Header))
            {
                CryptoSelect[3] |= 1;
                if (replace)
                {
                    Encryptor = new RC4Header();
                    Decryptor = new RC4Header();
                }
                return(1);
            }

            throw new EncryptionException("No valid encryption method detected");
        }
コード例 #2
0
        /// <summary>
        /// Sets CryptoSelect and initializes the stream encryptor and decryptor based on the selected method.
        /// </summary>
        /// <param name="remoteCryptoBytes">The cryptographic methods supported/wanted by the remote client in CryptoProvide format. The highest order one available will be selected</param>
        /// <param name="replace">True if the existing Encryptor/Decryptor object should be replaced with a new instance</param>
        protected virtual int SelectCrypto(byte[] remoteCryptoBytes, bool replace)
        {
            CryptoSelect = new byte[remoteCryptoBytes.Length];

            // '2' corresponds to RC4Full
            EncryptionType selectedEncryption;
            bool           remoteSupportsFull   = (remoteCryptoBytes[3] & 2) == 2;
            bool           remoteSupportsHeader = (remoteCryptoBytes[3] & 1) == 1;

            if (EncryptionTypes.PreferredRC4(allowedEncryption) == EncryptionType.RC4Full)
            {
                if (remoteSupportsFull && allowedEncryption.Contains(EncryptionType.RC4Full))
                {
                    selectedEncryption = EncryptionType.RC4Full;
                }
                else if (remoteSupportsHeader && allowedEncryption.Contains(EncryptionType.RC4Header))
                {
                    selectedEncryption = EncryptionType.RC4Header;
                }
                else
                {
                    throw new NotSupportedException("No supported crypto method supported");
                }
            }
            else
            {
                if (remoteSupportsHeader && allowedEncryption.Contains(EncryptionType.RC4Header))
                {
                    selectedEncryption = EncryptionType.RC4Header;
                }
                else if (remoteSupportsFull && allowedEncryption.Contains(EncryptionType.RC4Full))
                {
                    selectedEncryption = EncryptionType.RC4Full;
                }
                else
                {
                    throw new NotSupportedException("No supported crypto method supported");
                }
            }

            if (selectedEncryption == EncryptionType.RC4Full)
            {
                CryptoSelect[3] |= 2;
                if (replace)
                {
                    Encryptor = encryptor;
                    Decryptor = decryptor;
                }
                return(2);
            }

            // '1' corresponds to RC4Header
            if (selectedEncryption == EncryptionType.RC4Header)
            {
                CryptoSelect[3] |= 1;
                if (replace)
                {
                    Encryptor = new RC4Header();
                    Decryptor = new RC4Header();
                }
                return(1);
            }

            throw new EncryptionException("No valid encryption method detected");
        }
コード例 #3
0
        /// <summary>
        ///     Sets CryptoSelect and initializes the stream encryptor and decryptor based on the selected method.
        /// </summary>
        /// <param name="remoteCryptoBytes">
        ///     The cryptographic methods supported/wanted by the remote client in CryptoProvide
        ///     format. The highest order one available will be selected
        /// </param>
        protected virtual int SelectCrypto(byte[] remoteCryptoBytes, bool replace)
        {
            CryptoSelect = new byte[remoteCryptoBytes.Length];

            // '2' corresponds to RC4Full
            if ((remoteCryptoBytes[3] & 2) == 2 && Toolbox.HasEncryption(allowedEncryption, EncryptionTypes.RC4Full))
            {
                CryptoSelect[3] |= 2;
                if (replace)
                {
                    Encryptor = encryptor;
                    Decryptor = decryptor;
                }
                return 2;
            }

            // '1' corresponds to RC4Header
            if ((remoteCryptoBytes[3] & 1) == 1 && Toolbox.HasEncryption(allowedEncryption, EncryptionTypes.RC4Header))
            {
                CryptoSelect[3] |= 1;
                if (replace)
                {
                    Encryptor = new RC4Header();
                    Decryptor = new RC4Header();
                }
                return 1;
            }

            throw new EncryptionException("No valid encryption method detected");
        }