Exemple #1
0
        public object GetValue(string property)
        {
            // Si propiedad es Hash, coloca valor hash y lo devuelve
            if (property == FieldHash)
            {
                string hash = FieldsHashCode;
                Reflex.SetPropertyValue(this, property, hash);
                return(hash);
            }

            object value = Reflex.GetPropertyValue(this, property);

            if (Reflex.PropertyHasAttribute(this, property, typeof(EncryptAttribute)))
            {
                if (EncryptIters != 0)
                {
                    Encriptador.Iters = EncryptIters;
                }
                if (!string.IsNullOrEmpty(EncryptPass))
                {
                    Encriptador.Pass = EncryptPass;
                }
                if (!string.IsNullOrEmpty(EncryptSalt))
                {
                    Encriptador.Salt = EncryptSalt;
                }

                // obtiene valor encriptado de acuerdo al tipo de propiedad
                switch (value.GetType().Name)
                {
                case "Byte[]":
                    byte[] bytesToEncrypt = (byte[])value;
                    byte[] bytesNoEncrypt = null;
                    if (bytesToEncrypt.Length > 100)
                    {
                        bytesToEncrypt = new byte[100];
                        Array.Copy((byte[])value, bytesToEncrypt, bytesToEncrypt.Length);
                        bytesNoEncrypt = new byte[((byte[])value).Length - 100];
                        Array.Copy((byte[])value, 100, bytesNoEncrypt, 0, bytesNoEncrypt.Length);
                    }
                    byte[] valueEncrypted = Encriptador.EncryptBytes(bytesToEncrypt);
                    byte[] bytesLength    = Convierte.IntToBytes(valueEncrypted.Length);
                    value = bytesLength.Concat(valueEncrypted).ToArray();
                    if (bytesNoEncrypt != null)
                    {
                        value = ((byte[])value).Concat(bytesNoEncrypt).ToArray();
                    }
                    break;

                default:
                    value = Encriptador.Encrypt(value.ToString());
                    break;
                }
            }

            return(value);
        }
Exemple #2
0
        public void SetValue(string property, object value)
        {
            // Si propiedad es Hash, coloca valor hash
            if (property == FieldHash)
            {
                Reflex.SetPropertyValue(this, property, FieldsHashCode);
                return;
            }

            if (Reflex.PropertyHasAttribute(this, property, typeof(EncryptAttribute)))
            {
                if (EncryptIters != 0)
                {
                    Encriptador.Iters = EncryptIters;
                }
                if (!string.IsNullOrEmpty(EncryptPass))
                {
                    Encriptador.Pass = EncryptPass;
                }
                if (!string.IsNullOrEmpty(EncryptSalt))
                {
                    Encriptador.Salt = EncryptSalt;
                }

                // obtiene valor desencriptado de acuerdo al tipo de propiedad
                switch (Reflex.GetPropertyTypeName(this, property))
                {
                case "Double":
                    string doubleDecrypt = Encriptador.Decrypt(value.ToString());
                    value = double.Parse(doubleDecrypt);
                    break;

                case "Int32":
                    string intDecrypt = Encriptador.Decrypt(value.ToString());
                    value = int.Parse(intDecrypt);
                    break;

                case "Byte[]":
                    byte[] bytesLength = new byte[4];
                    Array.Copy(((byte[])value), bytesLength, bytesLength.Length);
                    int    lengthEncrypted = Convierte.BytesToInt(bytesLength);
                    byte[] bytesEncrypted  = new byte[lengthEncrypted];
                    Array.Copy((byte[])value, 4, bytesEncrypted, 0, bytesEncrypted.Length);
                    byte[] bytesNoEncrypted = null;
                    if (((byte[])value).Length > 4 + lengthEncrypted)
                    {
                        int lengthNoEncrypted = ((byte[])value).Length - (4 + lengthEncrypted);
                        bytesNoEncrypted = new byte[lengthNoEncrypted];
                        Array.Copy((byte[])value, 4 + lengthEncrypted, bytesNoEncrypted, 0, bytesNoEncrypted.Length);
                    }
                    byte[] bytesDecrypted = Encriptador.DecryptBytes(bytesEncrypted);
                    value = bytesNoEncrypted == null ? bytesDecrypted : bytesDecrypted.Concat(bytesNoEncrypted).ToArray();
                    break;

                default:
                    value = Encriptador.Decrypt(value.ToString());
                    break;
                }
            }

            Reflex.SetPropertyValue(this, property, value);
        }