Exemplo n.º 1
0
        /// <summary>
        /// Serializes a processor to a string. The string is encrypted and validated.
        /// Thanks to Dean Brettle for help with this http://www.brettle.com.
        /// </summary>
        /// <param name="processor">Processor to serialize.</param>
        /// <returns>The serialized processor.</returns>
        internal string SerializeProcessor(IFileProcessor processor)
        {
            SettingsStorageObject so        = new SettingsStorageObject();
            MemoryStream          ms        = new MemoryStream();
            MemoryStream          outStream = new MemoryStream();
            BinaryFormatter       bf        = new BinaryFormatter();

            bf.Serialize(ms, processor);

            // Encrypt the serialized object
            MemoryStream       cipherTextStream = new MemoryStream();
            SymmetricAlgorithm cipher           = SymmetricAlgorithm.Create();

            cipher.Mode    = CipherMode.CBC;
            cipher.Padding = PaddingMode.PKCS7;
            cipher.Key     = DefaultEncryptionKey;
            CryptoStream cryptoStream = new CryptoStream(cipherTextStream, cipher.CreateEncryptor(), CryptoStreamMode.Write);

            byte[] cryptoBytes = ms.ToArray();
            cryptoStream.Write(cryptoBytes, 0, cryptoBytes.Length);
            cryptoStream.Close();

            so.CipherText = cipherTextStream.ToArray();
            so.CipherIV   = cipher.IV;

            // Generate a hash for the encrypted data
            KeyedHashAlgorithm kh = KeyedHashAlgorithm.Create();

            kh.Key  = DefaultValidationKey;
            so.Hash = kh.ComputeHash(so.CipherText);

            bf.Serialize(outStream, so);
            return(Convert.ToBase64String(outStream.ToArray()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deserializes a processor from an encrypted string. The string is encrypted and validated.
        /// Thanks to Dean Brettle for help with this http://www.brettle.com.
        /// </summary>
        /// <param name="input">The encrypted and signed input string.</param>
        /// <returns>The deserialized processor.</returns>
        internal IFileProcessor DeserializeProcessor(string input)
        {
            MemoryStream    ms;
            BinaryFormatter bf = new BinaryFormatter();

            byte[] bytes = Convert.FromBase64String(input);
            ms = new MemoryStream(bytes);

            SettingsStorageObject so = (SettingsStorageObject)bf.Deserialize(ms);

            // Compute and check the hash
            KeyedHashAlgorithm macAlgorithm = KeyedHashAlgorithm.Create();
            MemoryStream       hashStream   = new MemoryStream(so.CipherText);

            macAlgorithm.Key = DefaultValidationKey;
            byte[] expectedHash = macAlgorithm.ComputeHash(hashStream);

            bool valid = true;

            if (expectedHash.Length != so.Hash.Length)
            {
                valid = false;
            }
            else
            {
                for (int i = 0; i < expectedHash.Length; i++)
                {
                    if (expectedHash[i] != so.Hash[i])
                    {
                        valid = false;
                        break;
                    }
                }
            }

            if (!valid)
            {
                throw new System.Security.SecurityException("Processor settings invalid");
            }

            // Decrypt the settings
            MemoryStream       cipherTextStream = new MemoryStream(so.CipherText);
            SymmetricAlgorithm cipher           = SymmetricAlgorithm.Create();

            cipher.Mode    = CipherMode.CBC;
            cipher.Padding = PaddingMode.PKCS7;
            cipher.Key     = DefaultEncryptionKey;
            cipher.IV      = so.CipherIV;
            CryptoStream cryptoStream = new CryptoStream(cipherTextStream, cipher.CreateDecryptor(), CryptoStreamMode.Read);

            return((IFileProcessor)bf.Deserialize(cryptoStream));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Serializes a processor to a string. The string is encrypted and validated.
        /// Thanks to Dean Brettle for help with this http://www.brettle.com.
        /// </summary>
        /// <param name="processor">Processor to serialize.</param>
        /// <returns>The serialized processor.</returns>
        internal string SerializeProcessor(IFileProcessor processor)
        {
            SettingsStorageObject so = new SettingsStorageObject();
            MemoryStream ms = new MemoryStream();
            MemoryStream outStream = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, processor);

            // Encrypt the serialized object
            MemoryStream cipherTextStream = new MemoryStream();
            SymmetricAlgorithm cipher = SymmetricAlgorithm.Create();
            cipher.Mode = CipherMode.CBC;
            cipher.Padding = PaddingMode.PKCS7;
            cipher.Key = DefaultEncryptionKey;
            CryptoStream cryptoStream = new CryptoStream(cipherTextStream, cipher.CreateEncryptor(), CryptoStreamMode.Write);
            byte[] cryptoBytes = ms.ToArray();
            cryptoStream.Write(cryptoBytes, 0, cryptoBytes.Length);
            cryptoStream.Close();

            so.CipherText = cipherTextStream.ToArray();
            so.CipherIV = cipher.IV;

            // Generate a hash for the encrypted data
            KeyedHashAlgorithm kh = KeyedHashAlgorithm.Create();
            kh.Key = DefaultValidationKey;
            so.Hash = kh.ComputeHash(so.CipherText);

            bf.Serialize(outStream, so);
            return Convert.ToBase64String(outStream.ToArray());
        }