private void DoCloudQueueAddUpdateEncryptedMessageAPM(IKey key, DictionaryKeyResolver keyResolver)
        {
            CloudQueueClient client = GenerateCloudQueueClient();
            CloudQueue       queue  = client.GetQueueReference(GenerateNewQueueName());

            try
            {
                queue.CreateIfNotExists();

                string            messageStr = Guid.NewGuid().ToString();
                CloudQueueMessage message    = new CloudQueueMessage(messageStr);

                QueueEncryptionPolicy policy        = new QueueEncryptionPolicy(key, null);
                QueueRequestOptions   createOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    // Add message
                    IAsyncResult result = queue.BeginAddMessage(message, null, null, createOptions, null, ar => waitHandle.Set(), null);
                    waitHandle.WaitOne();
                    queue.EndAddMessage(result);

                    policy = new QueueEncryptionPolicy(null, keyResolver);
                    QueueRequestOptions retrieveOptions = new QueueRequestOptions()
                    {
                        EncryptionPolicy = policy
                    };

                    // Retrieve message
                    result = queue.BeginGetMessage(null, retrieveOptions, null, ar => waitHandle.Set(), null);
                    waitHandle.WaitOne();
                    CloudQueueMessage retrMessage = queue.EndGetMessage(result);
                    Assert.AreEqual(messageStr, retrMessage.AsString);

                    // Update message
                    string updatedMessage = Guid.NewGuid().ToString("N");
                    retrMessage.SetMessageContent(updatedMessage);
                    result = queue.BeginUpdateMessage(retrMessage, TimeSpan.FromSeconds(0), MessageUpdateFields.Content | MessageUpdateFields.Visibility, createOptions, null, ar => waitHandle.Set(), null);
                    waitHandle.WaitOne();
                    queue.EndUpdateMessage(result);

                    // Retrieve updated message
                    result = queue.BeginGetMessage(null, retrieveOptions, null, ar => waitHandle.Set(), null);
                    waitHandle.WaitOne();
                    retrMessage = queue.EndGetMessage(result);
                    Assert.AreEqual(updatedMessage, retrMessage.AsString);
                }
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
        public void CloudQueueAddUpdateEncryptedEncodedMessage()
        {
            // Create the Key to be used for wrapping.
            SymmetricKey aesKey = new SymmetricKey("symencryptionkey");

            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                byte[] messageBytes = new byte[100];
                Random rand         = new Random();
                rand.NextBytes(messageBytes);

                string            inputMessage = Convert.ToBase64String(messageBytes);
                CloudQueueMessage message      = new CloudQueueMessage(inputMessage);
                queue.EncodeMessage = false;

                QueueEncryptionPolicy policy  = new QueueEncryptionPolicy(aesKey, null);
                QueueRequestOptions   options = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };

                // Add message
                queue.AddMessage(message, null, null, options, null);

                // Retrieve message
                CloudQueueMessage retrMessage = queue.GetMessage(null, options, null);
                Assert.AreEqual(inputMessage, retrMessage.AsString);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
        public void CloudQueueAddEncrypted64KMessage()
        {
            // Create the Key to be used for wrapping.
            SymmetricKey aesKey = new SymmetricKey("symencryptionkey");

            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                string            inputMessage = new string('a', 64 * 1024);
                CloudQueueMessage message      = new CloudQueueMessage(inputMessage);
                queue.EncodeMessage = false;

                QueueEncryptionPolicy policy  = new QueueEncryptionPolicy(aesKey, null);
                QueueRequestOptions   options = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };

                // Add message
                queue.AddMessage(message, null, null, null, null);

                // Add encrypted Message
                TestHelper.ExpectedException <ArgumentException>(
                    () => queue.AddMessage(message, null, null, options, null),
                    "Adding an encrypted message that exceeds message limits should throw");
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }