예제 #1
0
        /// <summary>
        /// Encrypts a string of data and returns the data
        /// </summary>
        /// <param name="Data">The Data to encrypt</param>
        /// <param name="Key">The Key or Password used to encrypt the Data</param>
        /// <param name="Salt">The Data used to hash the Encrypted Data</param>
        /// <param name="Algorithm">A symmetric algorithm used to encrypt the Data</param>
        /// <param name="Base64Encode">True to return the encrypted data as a Base64 Encoded String (for use in Xml files)</param>
        /// <returns>An encrypted String of the Data</returns>
        public static string Encrypt(string Data, byte[] Key, byte[] Salt, SymmetricAlgorithms Algorithm, bool Base64Encode)
        {
            // Determine the Algorithm
            SymmetricAlgorithm symAlgorithm = null;

            switch (Algorithm)
            {
            case SymmetricAlgorithms.DES:
                symAlgorithm = new DESCryptoServiceProvider();
                break;

            case SymmetricAlgorithms.RC2:
                symAlgorithm = new RC2CryptoServiceProvider();
                break;

            case SymmetricAlgorithms.Rijndael:
                symAlgorithm = new RijndaelManaged();
                break;

            case SymmetricAlgorithms.TripleDES:
                symAlgorithm = new TripleDESCryptoServiceProvider();
                break;
            }
            if (symAlgorithm == null)
            {
                return(Data);
            }

            try
            {
                symAlgorithm.IV   = Salt;
                symAlgorithm.Key  = Key;
                symAlgorithm.Mode = CipherMode.CBC;

                byte[] cryptData = Cryptographer.Encrypt(Data, symAlgorithm);
                if (Base64Encode)
                {
                    return(Convert.ToBase64String(cryptData));
                }
                else
                {
                    return(System.Text.Encoding.UTF8.GetString(cryptData));
                }
            }
            catch
            {
                return(Data);
            }
            finally
            {
                if (symAlgorithm != null)
                {
                    symAlgorithm.Clear();
                    symAlgorithm = null;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Encrypts the Properties of an Object
        /// </summary>
        /// <param name="Target"></param>
        private static void EncryptProperties(object Target)
        {
            if (Target == null)
            {
                return;
            }
            if (Target is System.Data.DataTable)
            {
                return;
            }

            object oValue = null;

            try
            {
                Type           type  = Target.GetType();
                PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                foreach (PropertyInfo prop in props)
                {
                    Type propType = EncryptableAttribute.GetPropertyType(prop.PropertyType);
                    if (prop.PropertyType.IsClass &&
                        propType.FullName != "System.String" &&
                        propType.FullName != "System.String[]" &&
                        propType.FullName != "System.Object" &&
                        propType.FullName != "System.Object[]" &&
                        propType.FullName.Contains("List") == false)
                    {
                        oValue = prop.GetValue(Target, null);
                        EncryptProperties(oValue);
                        continue;
                    }

                    if (!prop.CanWrite)
                    {
                        continue;
                    }
                    object[] attribs = prop.GetCustomAttributes(typeof(EncryptableAttribute), true);
                    if (attribs.Length == 0)
                    {
                        continue;
                    }

                    EncryptableAttribute attrib = (EncryptableAttribute)attribs[0];
                    oValue = prop.GetValue(Target, null);
                    if (oValue != null && !string.IsNullOrEmpty(oValue.ToString()))
                    {
                        oValue = Cryptographer.Encrypt(oValue.ToString(), attrib.Key, attrib.Salt, attrib.Algorithm, true);
                        SetPropertyValue(prop, Target, oValue, "");                             // Set the Value
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #3
0
        /// <summary>
        /// Encrypts the Fields of an Object
        /// </summary>
        /// <param name="Target"></param>
        private static void EncryptFields(object Target)
        {
            if (Target == null)
            {
                return;
            }
            if (Target is System.Data.DataTable || Target is System.Data.DataRow)
            {
                return;
            }

            object oValue = null;

            try
            {
                Type        type   = Target.GetType();
                FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
                foreach (FieldInfo field in fields)
                {
                    Type fieldType = EncryptableAttribute.GetPropertyType(field.FieldType);
                    if (fieldType.IsClass &&
                        fieldType.FullName != "System.String" &&
                        fieldType.FullName != "System.String[]" &&
                        fieldType.FullName != "System.Object" &&
                        fieldType.FullName != "System.Object[]" &&
                        fieldType.FullName.Contains("List") == false)
                    {
                        oValue = field.GetValue(Target);
                        EncryptFields(oValue);
                        continue;
                    }

                    object[] attribs = field.GetCustomAttributes(typeof(EncryptableAttribute), true);
                    if (attribs.Length == 0)
                    {
                        continue;
                    }

                    EncryptableAttribute attrib = (EncryptableAttribute)attribs[0];
                    oValue = field.GetValue(Target);
                    if (oValue != null && !string.IsNullOrEmpty(oValue.ToString()))
                    {
                        oValue = Cryptographer.Encrypt(oValue.ToString(), attrib.Key, attrib.Salt, attrib.Algorithm, true);
                        SetFieldValue(field, Target, oValue);                           // Set the Value
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }