Navaneeth S R
Navaneeth S R

Reputation: 33

How to encrypt one or two specific columns in an azure table when storing data?

I have an azure table with columns

[PartitionKey],[RowKey],[UserId],[UserName],[Email].

I want to encrypt only the Username and Emails. Is there any way to do this in Azure Table? Any help is appreciated. Thanks in advance.

Upvotes: 1

Views: 693

Answers (1)

Tom Sun
Tom Sun

Reputation: 24529

Is there any way to do this in Azure Table?

Yes, in your entity mark properties with attribute EncryptProperty

[EncryptProperty]
public string UserName { get; set; }
[EncryptProperty]
public string Email { get; set; }

We also could refer to this document to get more information about how to encrypt table entity.

I also do a demo for it, following is the detail steps.

1.Create a .net console application

2.Install the WindowsAzure.Storage and Microsoft.Azure.KeyVault.Extensions with nuget

3.Add a new class named User with following code.

public class User:TableEntity
{
    public string UserId { get; set; }
    [EncryptProperty]
    public string UserName { get; set; }
    [EncryptProperty]
    public string Email { get; set; }
    public User()
    {
        PartitionKey = "Tom";
        RowKey = Guid.NewGuid().ToString();

    }
    public User(string userId, string userName, string email)
    {
        PartitionKey = "Tom";
        RowKey = Guid.NewGuid().ToString();
        UserId = userId;
        UserName = userName;
        Email = email;

    }

}

4.Add the test code in the Program.cs

static void Main(string[] args)
{
      var connectionstring = "DefaultEndpointsProtocol=https;AccountName=accountName;AccountKey=accountKey";
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);
      RsaKey key = new RsaKey("mykey" /* key identifier */);
      // Create the encryption policy to be used for upload and download.
      TableEncryptionPolicy policy = new TableEncryptionPolicy(key, null);
      TableRequestOptions options = new TableRequestOptions
       {
           EncryptionPolicy = policy
       };
       CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
       // Create the CloudTable object that represents the "tomtest" table.
       CloudTable table = tableClient.GetTableReference("tomtest");
       table.CreateIfNotExists();
       //var insertList = new List<User>();
       var user = new User { UserId = Guid.NewGuid().ToString(),UserName="tom1",Email="[email protected]" };
       table.Execute(TableOperation.Insert(user), options);
       TableRequestOptions retrieveoptions = new TableRequestOptions
       {
           EncryptionPolicy = policy
       };
       var query = new TableQuery<User>().Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, user.RowKey));
       var list = table.ExecuteQuery(query, retrieveoptions);
       foreach (User entity in list)
       {
          Console.WriteLine($"PartionKey:{entity.PartitionKey},RowKey:{entity.RowKey},userId:{entity.UserId},UserName: {entity.UserName},email:{entity.Email}");
       }

       Console.ReadKey();
   } 

5.Check it with Microsoft Azure storage exploer

enter image description here

6.Retrieve the table entity from table and output to console

enter image description here

Upvotes: 4

Related Questions