Exemplo n.º 1
0
        public static string EncryptMySecret()
        {
            //this is your 64-bit key string
            string myKeyString = AESEncryptor.CreateNewKey();

            using (var manager = new EncryptionManager(myKeyString))
            {
                //This is your initializor, or public key
                string iv = AESEncryptor.CreateInitializor();

                //This is how you encrypt with your specified key and iv
                encryptedSecretInfo = manager.Encrypt(secretInfo, iv);
            }


            //you can also encrypt with private keys of int a, b and c
            using (var manager = new EncryptionManager(100, 50, 10))
            {
                //This is your initializor, or public key
                string iv = manager.CreateInitializor();

                //This is your second public key, used with a, b, and c to compute the private key
                long ticks = DateTime.Now.Ticks;

                //This is how you encrypt with your specified key and iv
                encryptedSecretInfo = manager.Encrypt(secretInfo, iv, ticks);
            }

            return(encryptedSecretInfo);
        }
Exemplo n.º 2
0
        public IDictionary <string, EntityProperty> WriteEntity(OperationContext operationContext)
        {
            Dictionary <string, EntityProperty> retVals = new Dictionary <string, EntityProperty>();

#if RT
            IEnumerable <PropertyInfo> objectProperties = entity.GetType().GetRuntimeProperties();
            var classAttributes = System.Attribute.GetCustomAttributes(entity.GetType());
#else
            IEnumerable <PropertyInfo> objectProperties = this.GetType().GetProperties();
            var classAttributes = System.Attribute.GetCustomAttributes(this.GetType());
#endif
            EncryptionTicks = DateTime.Now.Ticks;

            EncryptionKey keyAttribute = (EncryptionKey)classAttributes.FirstOrDefault(x => !((x as EncryptionKey)?.IsDefaultValue ?? true));
            if (keyAttribute == null)
            {
                keyAttribute = (EncryptionKey)classAttributes.First(x => (x as EncryptionKey) != null);
                keyAttribute = keyAttribute ?? new EncryptionKey(24, 8, 3);
            }

            foreach (PropertyInfo property in objectProperties)
            {
                // reserved properties
                if (property.Name == "PartitionKey" ||
                    property.Name == "RowKey" ||
                    property.Name == "Timestamp" ||
                    property.Name == "ETag")
                {
                    continue;
                }

                // Enforce public getter / setter
#if RT
                if (property.SetMethod == null || !property.SetMethod.IsPublic || property.GetMethod == null || !property.GetMethod.IsPublic)
#else
                if (property.GetSetMethod() == null || !property.GetSetMethod().IsPublic || property.GetGetMethod() == null || !property.GetGetMethod().IsPublic)
#endif
                {
                    continue;
                }

                EntityProperty newProperty = null;
                if (property.GetCustomAttribute(typeof(EncryptedProperty)) != null)
                {
                    EntityProperty ivProperty = null;
                    if (keyAttribute.isSingleKey)
                    {
                        using (var manager = new EncryptionManager(keyAttribute.Key))
                        {
                            ivProperty  = CreateEntityPropertyFromObject(AESEncryptor.CreateInitializor(), false);
                            newProperty = CreateEntityPropertyFromObject(manager.Encrypt(JsonConvert.SerializeObject(property.GetValue(this, null)), ivProperty.StringValue), false);
                        }
                    }
                    else
                    {
                        using (var manager = new EncryptionManager(keyAttribute.A, keyAttribute.B, keyAttribute.C))
                        {
                            ivProperty  = CreateEntityPropertyFromObject(AESEncryptor.CreateInitializor(), false);
                            newProperty = CreateEntityPropertyFromObject(manager.Encrypt(JsonConvert.SerializeObject(property.GetValue(this, null)), ivProperty.StringValue, EncryptionTicks), false);
                        }
                    }
                    if (newProperty != null)
                    {
                        retVals.Add(property.Name + "IV", ivProperty);
                    }
                }
                else
                {
                    newProperty = CreateEntityPropertyFromObject(property.GetValue(this, null), false);
                }

                // property will be null if unknown type
                if (newProperty != null)
                {
                    retVals.Add(property.Name, newProperty);
                }
            }

            return(retVals);
        }