Esempio n. 1
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>
        /// Returns a binary array of encrypted data for the given parameters.
        /// </summary>
        /// <param name="source">Binary array of data to encrypt.</param>
        /// <param name="startIndex">Offset into <paramref name="source"/> buffer.</param>
        /// <param name="length">Number of bytes in <paramref name="source"/> buffer to encrypt starting from <paramref name="startIndex"/> offset.</param>
        /// <param name="password">User password used for key lookup.</param>
        /// <param name="strength">Cryptographic strength to use when encrypting data.</param>
        /// <returns>An encrypted version of the source data.</returns>
        public static byte[] Encrypt(this byte[] source, int startIndex, int length, string password, CipherStrength strength)
        {
            if (strength == CipherStrength.None)
                return source;

            if (string.IsNullOrEmpty(password))
                throw new ArgumentNullException("password");

            byte[][] keyIV = GetCryptoKeyIV(password, (int)strength);

            return source.Encrypt(startIndex, length, keyIV[0], keyIV[1], strength);
        }
Esempio n. 2
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>Returns a Base64 encoded string of the returned binary array of the encrypted data, generated with
        /// the given parameters.</summary>
        public static string Encrypt(this string source, string encryptionKey, CipherStrength strength)
        {
            if (string.IsNullOrEmpty(source))
                return null;

            if (string.IsNullOrEmpty(encryptionKey))
                encryptionKey = StandardKey;

            byte[] rgbKey = Encoding.ASCII.GetBytes(encryptionKey);
            byte[] rgbIV = Encoding.ASCII.GetBytes(encryptionKey);

            return Convert.ToBase64String(Encoding.Unicode.GetBytes(source).Encrypt(rgbKey, rgbIV, strength));
        }
Esempio n. 3
0
        /// <summary>
        /// Performs the necessary compression and encryption on the data contained in the <paramref name="buffer"/>.
        /// </summary>
        /// <param name="buffer">The buffer containing the data to be processed.</param>
        /// <param name="offset">The offset in the <paramref name="buffer"/> from where data is to be processed.</param>
        /// <param name="length">The length of data in <paramref name="buffer"/> starting at the <paramref name="offset"/>.</param>
        /// <param name="cryptoLevel">One of the <see cref="CipherStrength"/> values.</param>
        /// <param name="cryptoKey">The key to be used for encrypting the data in the <paramref name="buffer"/>.</param>
        /// <param name="compressLevel">One of the <see cref="CompressionStrength"/> values.</param>
        public static void ProcessTransmit(ref byte[] buffer, ref int offset, ref int length, CipherStrength cryptoLevel, string cryptoKey, CompressionStrength compressLevel)
        {
            if (compressLevel != CompressionStrength.NoCompression)
            {
                // Compress the data.
                buffer = new MemoryStream(buffer, offset, length).Compress(compressLevel).ToArray();
                offset = 0;
                length = buffer.Length;
            }

            if (cryptoLevel != CipherStrength.None)
            {
                // Encrypt the data.
                buffer = buffer.Encrypt(offset, length, Encoding.ASCII.GetBytes(cryptoKey), cryptoLevel);
                offset = 0;
                length = buffer.Length;
            }

            //if (cryptoLevel == CipherStrength.None && compressLevel == CompressionStrength.NoCompression)
            //{
            //    if (length - offset == data.Length)
            //        return data;
            //    else
            //        return data.BlockCopy(offset, length);
            //}
            //else
            //{
            //    if (compressLevel != CompressionStrength.NoCompression)
            //    {
            //        // Compress the data.
            //        data = new MemoryStream(data, offset, length).Compress(compressLevel).ToArray();
            //        offset = 0;
            //        length = data.Length;
            //    }

            //    if (cryptoLevel != CipherStrength.None)
            //    {
            //        // Encrypt the data.
            //        data = data.Encrypt(offset, length, Encoding.ASCII.GetBytes(cryptoKey), cryptoLevel);
            //    }

            //    return data;
            //}
        }
Esempio n. 4
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>Decrypts input stream onto output stream for the given parameters.</summary>
        public static void Decrypt(this Stream source, Stream destination, byte[] key, byte[] IV, CipherStrength strength, Action<ProcessProgress<long>> progressHandler)
        {
            ProcessProgress<long>.Handler progress = null;
            byte[] inBuffer, outBuffer;
            byte[] lengthBuffer = BitConverter.GetBytes(0);
            long total = 0;
            long length = -1;
            int size, read;

            // Sends initial progress event.
            if (progressHandler != null)
            {
                try
                {
                    if (source.CanSeek)
                        length = source.Length;
                }
                catch
                {
                    length = -1;
                }

                // Create a new progress handler to track decryption progress
                progress = new ProcessProgress<long>.Handler(progressHandler, "Decrypt", length);
                progress.Complete = 0;
            }

            // When the source stream was encrypted, it was known that the encrypted stream length did not have to be same as
            // the input stream length. We prepended the final size of the each encrypted buffer onto the destination
            // ouput stream (now the input stream to this function), so that we could safely decrypt the stream in a
            // "chunked" fashion, hence the following:

            // Reads the size of the next buffer from the stream.
            read = source.Read(lengthBuffer, 0, lengthBuffer.Length);

            while (read > 0)
            {
                // Converts the byte array containing the buffer size into an integer.
                size = BitConverter.ToInt32(lengthBuffer, 0);

                if (size > 0)
                {
                    // Creates and reads the next buffer.
                    inBuffer = new byte[size];
                    read = source.Read(inBuffer, 0, size);

                    if (read > 0)
                    {
                        // Decrypts buffer.
                        outBuffer = inBuffer.Decrypt(key, IV, strength);
                        destination.Write(outBuffer, 0, outBuffer.Length);

                        // Updates decryption progress.
                        if (progressHandler != null)
                        {
                            total += (read + lengthBuffer.Length);
                            progress.Complete = total;
                        }
                    }
                }

                // Reads the size of the next buffer from the stream.
                read = source.Read(lengthBuffer, 0, lengthBuffer.Length);
            }
        }
Esempio n. 5
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>Creates a decrypted file from source file data.</summary>
        public static void DecryptFile(string sourceFilename, string destinationFilename, string encryptionKey, CipherStrength strength, Action<ProcessProgress<long>> progressHandler)
        {
            if (string.IsNullOrEmpty(encryptionKey)) encryptionKey = StandardKey;

            FileStream sourceFileStream = File.Open(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
            FileStream destFileStream = File.Create(destinationFilename);
            byte[] rgbKey = Encoding.ASCII.GetBytes(encryptionKey);
            byte[] rgbIV = Encoding.ASCII.GetBytes(encryptionKey);

            sourceFileStream.Decrypt(destFileStream, rgbKey, rgbIV, strength, progressHandler);

            destFileStream.Flush();
            destFileStream.Close();
            sourceFileStream.Close();
        }
Esempio n. 6
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>Encrypts input stream onto output stream for the given parameters.</summary>
        public static void Encrypt(this Stream source, Stream destination, byte[] key, byte[] IV, CipherStrength strength, Action<ProcessProgress<long>> progressHandler)
        {
            ProcessProgress<long>.Handler progress = null;
            byte[] inBuffer = new byte[BufferSize];
            byte[] outBuffer, lengthBuffer;
            long total = 0;
            long length = -1;
            int read;

            // Sends initial progress event.
            if (progressHandler != null)
            {
                try
                {
                    if (source.CanSeek)
                        length = source.Length;
                }
                catch
                {
                    length = -1;
                }

                // Create a new progress handler to track encryption progress
                progress = new ProcessProgress<long>.Handler(progressHandler, "Encrypt", length);
                progress.Complete = 0;
            }

            // Reads initial buffer.
            read = source.Read(inBuffer, 0, BufferSize);

            while (read > 0)
            {
                // Encrypts buffer.
                outBuffer = inBuffer.CopyBuffer(0, read).Encrypt(key, IV, strength);

                // The destination encryption stream length does not have to be same as the input stream length, so we
                // prepend the final size of each encrypted buffer onto the destination ouput stream so that we can
                // safely decrypt the stream in a "chunked" fashion later.
                lengthBuffer = BitConverter.GetBytes(outBuffer.Length);
                destination.Write(lengthBuffer, 0, lengthBuffer.Length);
                destination.Write(outBuffer, 0, outBuffer.Length);

                // Updates encryption progress.
                if (progressHandler != null)
                {
                    total += read;
                    progress.Complete = total;
                }

                // Reads next buffer.
                read = source.Read(inBuffer, 0, BufferSize);
            }
        }
Esempio n. 7
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>Returns a binary array of decrypted data for the given parameters.</summary>
        public static byte[] Decrypt(this byte[] source, int startIndex, int length, byte[] key, byte[] IV, CipherStrength strength)
        {
            if (strength == CipherStrength.None)
                return source;

            // Performs requested levels of decryption.
            if (strength >= CipherStrength.Level5)
            {
                source = Deobfuscate(source, startIndex, length, key);
                startIndex = 0;
                length = source.Length;
            }

            if (strength >= CipherStrength.Level4)
            {
                source = new RijndaelManaged().Decrypt(source, startIndex, length, key, IV);
                startIndex = 0;
                length = source.Length;
            }

            if (strength >= CipherStrength.Level3)
            {
                source = new RC2CryptoServiceProvider().Decrypt(source, startIndex, length, key, IV);
                startIndex = 0;
                length = source.Length;
            }

            if (strength >= CipherStrength.Level2)
            {
                source = new TripleDESCryptoServiceProvider().Decrypt(source, startIndex, length, key, IV);
                startIndex = 0;
                length = source.Length;
            }

            return Crypt(source, startIndex, length, key);
        }
Esempio n. 8
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>
        /// Returns a decrypted string from a Base64 encoded string of binary encrypted data from the given
        /// parameters.
        /// </summary>
        public static string Decrypt(this string source, string encryptionKey, CipherStrength strength)
        {
            if (string.IsNullOrEmpty(source))
                return null;

            byte[] key = GetBinaryKeyFromString(encryptionKey);

            return Encoding.Unicode.GetString(Convert.FromBase64String(source).Decrypt(key, key, strength));
        }
Esempio n. 9
0
File: Cipher.cs Progetto: avs009/gsf
 /// <summary>
 /// Returns a binary array of decrypted data for the given parameters.
 /// </summary>
 public static byte[] Decrypt(this byte[] source, int startIndex, int length, byte[] key, CipherStrength strength)
 {
     return source.Decrypt(startIndex, length, key, key, strength);
 }
Esempio n. 10
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>
        /// Returns a Base64 encoded string of the returned binary array of the encrypted data, generated with
        /// the given parameters.
        /// </summary>
        public static string Encrypt(this string source, string encryptionKey, CipherStrength strength)
        {
            if (string.IsNullOrEmpty(source))
                return null;

            if (string.IsNullOrEmpty(encryptionKey))
                encryptionKey = StandardKey;

            byte[] key = GetBinaryKeyFromString(encryptionKey);

            return Convert.ToBase64String(Encoding.Unicode.GetBytes(source).Encrypt(key, key, strength));
        }
Esempio n. 11
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>
        /// Returns a binary array of encrypted data for the given parameters.
        /// </summary>
        public static byte[] Encrypt(this byte[] source, int startIndex, int length, byte[] key, byte[] IV, CipherStrength strength)
        {
            if (strength == CipherStrength.None)
                return source;

            // Performs requested levels of encryption.
            source = Crypt(source, startIndex, length, key);
            if (strength >= CipherStrength.Level2)
            {
                source = new TripleDESCryptoServiceProvider().Encrypt(source, 0, source.Length, key, IV);
                if (strength >= CipherStrength.Level3)
                {
                    source = new RC2CryptoServiceProvider().Encrypt(source, 0, source.Length, key, IV);
                    if (strength >= CipherStrength.Level4)
                    {
                        source = new RijndaelManaged().Encrypt(source, 0, source.Length, key, IV);
                        if (strength >= CipherStrength.Level5)
                        {
                            source = Obfuscate(source, 0, source.Length, key);
                            if (strength >= CipherStrength.Level6)
                            {
                                source = new Pkzip.PkzipClassicManaged().Encrypt(source, 0, source.Length, key, IV);
                            }
                        }
                    }
                }
            }

            return source;
        }
Esempio n. 12
0
 /// <summary>
 /// Initializes a new instance of the server.
 /// </summary>
 protected ServerBase()
     : base()
 {
     m_serverID = Guid.NewGuid();
     m_clientIDs = new List<Guid>();
     m_textEncoding = Encoding.ASCII;
     m_currentState = ServerState.NotRunning;
     m_maxClientConnections = DefaultMaxClientConnections;
     m_handshake = DefaultHandshake;
     m_handshakeTimeout = DefaultHandshakeTimeout;
     m_sharedSecret = DefaultSharedSecret;
     m_encryption = DefaultEncryption;
     m_secureSession = DefaultSecureSession;
     m_receiveTimeout = DefaultReceiveTimeout;
     m_receiveBufferSize = DefaultReceiveBufferSize;
     m_compression = DefaultCompression;
     m_persistSettings = DefaultPersistSettings;
     m_settingsCategory = DefaultSettingsCategory;
 }
Esempio n. 13
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>
        /// Creates a decrypted file from source file data.
        /// </summary>
        /// <param name="sourceFileName">Source file name.</param>
        /// <param name="destinationFileName">Destination file name.</param>
        /// <param name="password">User password used for key lookup.</param>
        /// <param name="strength">Cryptographic strength to use when decrypting file.</param>
        /// <param name="progressHandler">Optional delegate to handle progress updates for decrypting large files.</param>
        public static void DecryptFile(string sourceFileName, string destinationFileName, string password, CipherStrength strength, Action<ProcessProgress<long>> progressHandler)
        {
            FileStream sourceFileStream = File.Open(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            FileStream destFileStream = File.Create(destinationFileName);

            if (string.IsNullOrEmpty(password))
                throw new ArgumentNullException("password");

            byte[][] keyIV = GetCryptoKeyIV(password, (int)strength);

            sourceFileStream.Decrypt(destFileStream, keyIV[0], keyIV[1], strength, progressHandler);

            destFileStream.Flush();
            destFileStream.Close();
            sourceFileStream.Close();
        }
Esempio n. 14
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>
        /// Returns a binary array of decrypted data for the given parameters.
        /// </summary>
        /// <param name="source">Binary array of data to decrypt.</param>
        /// <param name="startIndex">Offset into <paramref name="source"/> buffer.</param>
        /// <param name="length">Number of bytes in <paramref name="source"/> buffer to decrypt starting from <paramref name="startIndex"/> offset.</param>
        /// <param name="key">Encryption key to use to decrypt data.</param>
        /// <param name="iv">Initialization vector to use to decrypt data.</param>
        /// <param name="strength">Cryptographic strength to use when decrypting data.</param>
        /// <returns>A decrypted version of the source data.</returns>
        public static byte[] Decrypt(this byte[] source, int startIndex, int length, byte[] key, byte[] iv, CipherStrength strength)
        {
            if (strength == CipherStrength.None)
                return source;

            AesManaged symmetricAlgorithm = new AesManaged();

            symmetricAlgorithm.KeySize = (int)strength;

            return symmetricAlgorithm.Decrypt(source, startIndex, length, key, iv);
        }
Esempio n. 15
0
        /// <summary>
        /// Returns a binary array of decrypted data for the given parameters.
        /// </summary>
        /// <param name="source">Binary array of data to decrypt.</param>
        /// <param name="startIndex">Offset into <paramref name="source"/> buffer.</param>
        /// <param name="length">Number of bytes in <paramref name="source"/> buffer to decrypt starting from <paramref name="startIndex"/> offset.</param>
        /// <param name="password">User password used for key lookup.</param>
        /// <param name="strength">Cryptographic strength to use when decrypting data.</param>
        /// <returns>A decrypted version of the source data.</returns>
        public static byte[] Decrypt(this byte[] source, int startIndex, int length, string password, CipherStrength strength)
        {
            if (strength == CipherStrength.None)
                return source;

            if (string.IsNullOrEmpty(password))
                throw new ArgumentNullException(nameof(password));

            byte[][] keyIV = s_keyIVCache.GetCryptoKeyIV(password, (int)strength);

            return source.Decrypt(startIndex, length, keyIV[KeyIndex], keyIV[IVIndex], strength);
        }
Esempio n. 16
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>
        /// Creates a decrypted file from source file data.
        /// </summary>
        public static void DecryptFile(string sourceFilename, string destinationFilename, string encryptionKey, CipherStrength strength, Action<ProcessProgress<long>> progressHandler)
        {
            FileStream sourceFileStream = File.Open(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
            FileStream destFileStream = File.Create(destinationFilename);
            byte[] key = GetBinaryKeyFromString(encryptionKey);

            sourceFileStream.Decrypt(destFileStream, key, key, strength, progressHandler);

            destFileStream.Flush();
            destFileStream.Close();
            sourceFileStream.Close();
        }
Esempio n. 17
0
        /// <summary>
        /// Returns a binary array of decrypted data for the given parameters.
        /// </summary>
        /// <param name="source">Binary array of data to decrypt.</param>
        /// <param name="startIndex">Offset into <paramref name="source"/> buffer.</param>
        /// <param name="length">Number of bytes in <paramref name="source"/> buffer to decrypt starting from <paramref name="startIndex"/> offset.</param>
        /// <param name="key">Encryption key to use to decrypt data.</param>
        /// <param name="iv">Initialization vector to use to decrypt data.</param>
        /// <param name="strength">Cryptographic strength to use when decrypting data.</param>
        /// <returns>A decrypted version of the source data.</returns>
        public static byte[] Decrypt(this byte[] source, int startIndex, int length, byte[] key, byte[] iv, CipherStrength strength)
        {
            if (strength == CipherStrength.None)
                return source;

            Aes symmetricAlgorithm;

            if (s_managedEncryption)
                symmetricAlgorithm = new AesManaged();
            else
                symmetricAlgorithm = new AesCryptoServiceProvider();

            symmetricAlgorithm.KeySize = (int)strength;

            return symmetricAlgorithm.Decrypt(source, startIndex, length, key, iv);
        }
Esempio n. 18
0
        protected ServerBase()
		{
			// Setup the default values.
			m_receiveBufferSize = 8192;
			m_maximumClients = - 1;
			m_handshake = true;
			m_encryption = CipherStrength.None;
			m_compression = CompressionStrength.NoCompression;
			//m_crcCheck = CRCCheckType.None;
			m_enabled = true;
			m_textEncoding = System.Text.Encoding.ASCII;
			m_serverID = Guid.NewGuid(); // Create an ID for the server.
			m_clientIDs = new List<Guid>();
			m_settingsCategory = this.GetType().Name;			
			m_startTime = 0;
			m_stopTime = 0;
			m_buffer = new byte[m_receiveBufferSize];
		}
Esempio n. 19
0
File: Cipher.cs Progetto: avs009/gsf
 /// <summary>Returns a binary array of decrypted data for the given parameters.</summary>
 public static byte[] Decrypt(this byte[] source, byte[] key, byte[] IV, CipherStrength strength)
 {
     return source.Decrypt(0, source.Length, key, IV, strength);
 }
Esempio n. 20
0
File: Cipher.cs Progetto: avs009/gsf
        /// <summary>
        /// Returns a Base64 encoded string of the returned binary array of the encrypted data, generated with
        /// the given parameters.
        /// </summary>
        /// <param name="source">Source string to encrypt.</param>
        /// <param name="password">User password used for key lookup.</param>
        /// <param name="strength">Cryptographic strength to use when encrypting string.</param>
        /// <returns>An encrypted version of the source string.</returns>
        public static string Encrypt(this string source, string password, CipherStrength strength)
        {
            if (string.IsNullOrEmpty(source))
                return null;

            if (strength == CipherStrength.None)
                return source;

            return Convert.ToBase64String(Encoding.Unicode.GetBytes(source).Encrypt(password, strength));
        }
Esempio n. 21
0
File: Cipher.cs Progetto: avs009/gsf
        // The following pure stream decryption implementation is incompatible with the one that implements a progress handler
        // since it uses embedded stream lengths from the original encrypted stream - so this method was removed to prevent possible
        // confusion/errors during decryption cycle. 

        ///// <summary>Returns a stream of decrypted data for the given parameters.</summary>
        //public static Stream Decrypt(Stream inStream, byte[] key, byte[] IV, CipherStrength strength)
        //{
        //    if (strength == CipherStrength.None)
        //        return inStream;

        //    // Performs requested levels of decryption.
        //    if (strength >= CipherStrength.Level5)
        //        inStream = Deobfuscate(inStream, key);

        //    if (strength >= CipherStrength.Level4)
        //        inStream = Decrypt(new RijndaelManaged(), inStream, key, IV);

        //    if (strength >= CipherStrength.Level3)
        //        inStream = Decrypt(new RC2CryptoServiceProvider(), inStream, key, IV);

        //    if (strength >= CipherStrength.Level2)
        //        inStream = Decrypt(new TripleDESCryptoServiceProvider(), inStream, key, IV);

        //    return Crypt(inStream, key);
        //}

        #endregion

        /// <summary>Returns a stream of decrypted data for the given parameters.</summary>
        /// <remarks>
        /// This returns a memory stream of the decrypted results, if the incoming stream is
        /// very large this will consume a large amount memory.  In this case use the overload
        /// that takes the destination stream as a parameter instead.
        /// </remarks>
        public static MemoryStream Decrypt(this Stream source, byte[] key, byte[] IV, CipherStrength strength)
        {
            MemoryStream destination = new MemoryStream();

            source.Decrypt(destination, key, IV, strength, null);
            destination.Position = 0;

            return destination;
        }
Esempio n. 22
0
 /// <summary>
 /// Returns a binary array of encrypted data for the given parameters.
 /// </summary>
 /// <param name="source">Binary array of data to encrypt.</param>
 /// <param name="key">Encryption key to use to encrypt data.</param>
 /// <param name="iv">Initialization vector to use to encrypt data.</param>
 /// <param name="strength">Cryptographic strength to use when encrypting data.</param>
 /// <returns>An encrypted version of the source data.</returns>
 public static byte[] Encrypt(this byte[] source, byte[] key, byte[] iv, CipherStrength strength)
 {
     return source.Encrypt(0, source.Length, key, iv, strength);
 }
Esempio n. 23
0
File: Cipher.cs Progetto: avs009/gsf
 /// <summary>Creates a decrypted file from source file data.</summary>
 public static void DecryptFile(string sourceFilename, string destinationFilename, CipherStrength strength)
 {
     DecryptFile(sourceFilename, destinationFilename, null, strength, null);
 }
Esempio n. 24
0
        /// <summary>
        /// Creates an encrypted file from source file data.
        /// </summary>
        /// <param name="sourceFileName">Source file name.</param>
        /// <param name="destinationFileName">Destination file name.</param>
        /// <param name="password">User password used for key lookup.</param>
        /// <param name="strength">Cryptographic strength to use when encrypting file.</param>
        /// <param name="progressHandler">Optional delegate to handle progress updates for encrypting large files.</param>
        public static void EncryptFile(string sourceFileName, string destinationFileName, string password, CipherStrength strength, Action<ProcessProgress<long>> progressHandler)
        {
            using (FileStream sourceFileStream = File.Open(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read), destFileStream = File.Create(destinationFileName))
            {
                if (string.IsNullOrEmpty(password))
                    throw new ArgumentNullException(nameof(password));

                byte[][] keyIV = s_keyIVCache.GetCryptoKeyIV(password, (int)strength);

                sourceFileStream.Encrypt(destFileStream, keyIV[KeyIndex], keyIV[IVIndex], strength, progressHandler);

                destFileStream.Flush();
                destFileStream.Close();
            }
        }
Esempio n. 25
0
File: Cipher.cs Progetto: avs009/gsf
 /// <summary>Returns a Base64 encoded string of the returned binary array of the encrypted data, generated with
 /// the given parameters using standard encryption.</summary>
 public static string Encrypt(this string source, CipherStrength strength)
 {
     return source.Encrypt(null, strength);
 }
Esempio n. 26
0
        /// <summary>
        /// Returns a decrypted string from a Base64 encoded string of binary encrypted data from the given
        /// parameters.
        /// </summary>
        /// <param name="source">Source string to decrypt.</param>
        /// <param name="password">User password used for key lookup.</param>
        /// <param name="strength">Cryptographic strength to use when decrypting string.</param>
        /// <returns>A decrypted version of the source string.</returns>
        public static string Decrypt(this string source, string password, CipherStrength strength)
        {
            if (string.IsNullOrEmpty(source))
                return null;

            if (strength == CipherStrength.None)
                return source;

            return s_textEncoding.GetString(Convert.FromBase64String(source).Decrypt(password, strength));
        }
Esempio n. 27
0
        /// <summary>
        /// Performs the necessary uncompression and decryption on the data contained in the <paramref name="buffer"/>.
        /// </summary>
        /// <param name="buffer">The buffer containing the data to be processed.</param>
        /// <param name="offset">The offset in the <paramref name="buffer"/> from where data is to be processed.</param>
        /// <param name="length">The length of data in <paramref name="buffer"/> starting at the <paramref name="offset"/>.</param>
        /// <param name="cryptoLevel">One of the <see cref="CipherStrength"/> values.</param>
        /// <param name="cryptoKey">The key to be used for decrypting the data in the <paramref name="buffer"/>.</param>
        /// <param name="compressLevel">One of the <see cref="CompressionStrength"/> values.</param>
        public static void ProcessReceived(ref byte[] buffer, ref int offset, ref int length, CipherStrength cryptoLevel, string cryptoKey, CompressionStrength compressLevel)
        {
            if (cryptoLevel != CipherStrength.None || compressLevel != CompressionStrength.NoCompression)
            {
                // Make a copy of the data to be processed.
                byte[] temp = buffer.BlockCopy(offset, length);

                if (cryptoLevel != CipherStrength.None)
                {
                    // Decrypt the data.
                    temp = temp.Decrypt(Encoding.ASCII.GetBytes(cryptoKey), cryptoLevel);
                    offset = 0;
                    length = temp.Length;
                }
                
                if (compressLevel != CompressionStrength.NoCompression)
                {
                    // Uncompress the data.
                    temp = new MemoryStream(temp).Decompress().ToArray();
                    offset = 0;
                    length = temp.Length;
                }

                if (temp.Length > buffer.Length)
                    // Processed data cannot fit in the existing buffer.
                    buffer = temp;
                else
                    // Copy the processed data into the existing buffer.
                    Buffer.BlockCopy(temp, offset, buffer, offset, length);
            }

            //if (cryptoLevel == CipherStrength.None && compressLevel == CompressionStrength.NoCompression)
            //{
            //    if (length - offset == data.Length)
            //        return data;
            //    else
            //        return data.BlockCopy(offset, length);
            //}
            //else
            //{
            //    if (cryptoLevel != CipherStrength.None)
            //    {
            //        data = data.Decrypt(offset, length, Encoding.ASCII.GetBytes(cryptoKey), cryptoLevel);
            //        offset = 0;
            //        length = data.Length;
            //    }

            //    if (compressLevel != CompressionStrength.NoCompression)
            //    {
            //        data = new MemoryStream(data, offset, length).Decompress().ToArray();
            //    }

            //    return data;
            //}
        }
Esempio n. 28
0
 /// <summary>
 /// Returns a binary array of decrypted data for the given parameters.
 /// </summary>
 /// <param name="source">Binary array of data to decrypt.</param>
 /// <param name="password">User password used for key lookup.</param>
 /// <param name="strength">Cryptographic strength to use when decrypting data.</param>
 /// <returns>A decrypted version of the source data.</returns>
 public static byte[] Decrypt(this byte[] source, string password, CipherStrength strength)
 {
     return source.Decrypt(0, source.Length, password, strength);
 }
Esempio n. 29
0
        /// <summary>
        /// Initializes a new instance of the client.
        /// </summary>
        protected ClientBase()
            : base()
        {
            m_serverID = Guid.Empty;
            m_clientID = Guid.NewGuid();
            m_textEncoding = Encoding.ASCII;
            m_currentState = ClientState.Disconnected;
            m_maxConnectionAttempts = DefaultMaxConnectionAttempts;
            m_handshake = DefaultHandshake;
            m_handshakeTimeout = DefaultHandshakeTimeout;
            m_sharedSecret = DefaultSharedSecret;
            m_encryption = DefaultEncryption;
            m_secureSession = DefaultSecureSession;
            m_receiveTimeout = DefaultReceiveTimeout;
            m_receiveBufferSize = DefaultReceiveBufferSize;
            m_compression = DefaultCompression;
            m_persistSettings = DefaultPersistSettings;
            m_settingsCategory = DefaultSettingsCategory;

        }
Esempio n. 30
0
        protected ClientBase()
		{
			// Setup the default values.
			m_receiveBufferSize = 8192;
			m_receiveTimeout = - 1;
			m_maximumConnectionAttempts = - 1;
			m_textEncoding = Encoding.ASCII;
			m_handshake = true;
			m_encryption = CipherStrength.None;
			m_compression = CompressionStrength.NoCompression;
			//m_crcCheck = CRCCheckType.None;
			m_enabled = true;
			m_clientID = Guid.NewGuid();
			m_settingsCategory = this.GetType().Name;			
			m_connectionWaitHandle = new System.Threading.ManualResetEvent(false);
			m_buffer = new byte[m_receiveBufferSize];
		}