Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

GDPR encrypt decrypt data in Umbraco backoffice

private void Content_Saving(IContentService sender, SaveEventArgs<IContent> e)
        {
            var node = e.SavedEntities.ToList();
            foreach (var items in node.Where(x => x.ContentType.Alias == "example"))
            {
                string firstName = items.GetValue<string>("firstName");
                //string encryptFirstName = EncryptionDecryption.EncryptString(firstName, "SupplyPassPhrase");
                string encryptFirstName = EncryptionDecryption.EncryptWithNoPassPhrase(firstName);
                items.SetValue("firstName", encryptFirstName);
                sender.SaveAndPublishWithStatus(items, 0, false);
            }
        }
public static string EncryptWithNoPassPhrase(string plainText)
        {
            if (!string.IsNullOrEmpty(plainText))
            {
                byte[] data = Encoding.Unicode.GetBytes(plainText);
                CspParameters cspParameters         = new CspParameters { KeyContainerName = Iv };
                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048, cspParameters))
                {
                    byte[] reBytes = rsa.Encrypt(data, false);
                    return Convert.ToBase64String(reBytes);
                }
            }
            return "No Data Supplied";
        }
EditorModelEventManager.SendingContentModel += (sender, e) =>
            {
                var node = e.Model.Properties.ToList();
                if (e.Model.IsChildOfListView && e.Model.ContentTypeAlias == "example")
                {
                    string firstNameDecrypted   = node.Where(x => x.Alias.ToLower() == "firstname").Select(x => x.Value).First().ToString();
                    var firstName   = e.Model.Properties.FirstOrDefault(x => x.Alias.ToLower() == "firstname");
                    //string decryptFirstName = EncryptionDecryption.DecryptString(firstNameDecrypted,"SupplyPassPhrase");
                    string decryptFirstName = EncryptionDecryption.DecryptWithNoPassPhrase(firstNameDecrypted);
                    if (firstName?.Value != null)
                    {
                        firstName.Value = $"{decryptFirstName}";
                    }
                }
            };
text unencrypted

Please enter your comment.