Esempio n. 1
0
        protected static void DecryptEntity(EncryptedTableEntity entity)
        {
            var algorithm = AzureEncryption.GetAlgorithm(entity.EncryptionVersion.Value);

            foreach (var info in GetEncyptableProperties(entity))
            {
                try {
                    if (info.PropertyType == typeof(string))
                    {
                        var encrypted = (string)info.GetValue(entity);
                        if (!string.IsNullOrEmpty(encrypted))
                        {
                            var decrypted = Decrypt(Convert.FromBase64String(encrypted), algorithm);
                            info.SetValue(entity, Encoding.UTF8.GetString(decrypted));
                        }
                    }
                    else if (info.PropertyType == typeof(byte[]))
                    {
                        var encrypted = (byte[])info.GetValue(entity);
                        if (encrypted != null && encrypted.Length > 0)
                        {
                            info.SetValue(entity, Decrypt(encrypted, algorithm));
                        }
                    }
                    else
                    {
                        // TODO: Log, throw, what? I feel there should be some feedback if you mark an unsupported property as [Encrypt]
                    }
                }
                catch (Exception ex) {
                    Trace.TraceWarning("Failed to decrypt {0}.{1}: {2}", entity.GetType().FullName, info.Name, ex);
                }
            }
        }
Esempio n. 2
0
        protected static void EncryptEntity(EncryptedTableEntity entity, IDictionary <string, EntityProperty> entityProperties)
        {
            var algorithm = AzureEncryption.GetAlgorithm(entity.EncryptionVersion.Value);

            foreach (var info in GetEncyptableProperties(entity))
            {
                try {
                    if (!entityProperties.TryGetValue(info.Name, out var entityProp))
                    {
                        continue; // Do we need to log this, idk if it's every actually going to come up.
                    }
                    switch (entityProp.PropertyType)
                    {
                    case EdmType.Binary:
                        if (entityProp.BinaryValue != null && entityProp.BinaryValue.Length > 0)
                        {
                            entityProp.BinaryValue = Encrypt(entityProp.BinaryValue, algorithm);
                        }
                        break;

                    case EdmType.String:
                        if (!string.IsNullOrEmpty(entityProp.StringValue))
                        {
                            entityProp.StringValue = Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(entityProp.StringValue), algorithm));
                        }
                        break;

                    default:
                        // TODO: Log, throw, what? I feel there should be some feedback if you mark an unsupported property as [Encrypt]
                        break;
                    }
                }
                catch (Exception ex) {
                    Trace.TraceWarning("Failed to encrypt {0}.{1}: {2}", entity.GetType().FullName, info.Name, ex);
                }
            }
        }