本文整理汇总了C#中Microsoft.Azure.KeyVault.SymmetricKey.UnwrapKeyAsync方法的典型用法代码示例。如果您正苦于以下问题:C# SymmetricKey.UnwrapKeyAsync方法的具体用法?C# SymmetricKey.UnwrapKeyAsync怎么用?C# SymmetricKey.UnwrapKeyAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Azure.KeyVault.SymmetricKey
的用法示例。
在下文中一共展示了SymmetricKey.UnwrapKeyAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CloudBlockBlobValidateEncryptionAPM
public void CloudBlockBlobValidateEncryptionAPM()
{
CloudBlobContainer container = GetRandomContainerReference();
try
{
container.Create();
int size = 5 * 1024 * 1024;
byte[] buffer = GetRandomBuffer(size);
CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
// Create the Key to be used for wrapping.
SymmetricKey aesKey = new SymmetricKey("symencryptionkey");
// Create the encryption policy to be used for upload.
BlobEncryptionPolicy uploadPolicy = new BlobEncryptionPolicy(aesKey, null);
// Set the encryption policy on the request options.
BlobRequestOptions uploadOptions = new BlobRequestOptions() { EncryptionPolicy = uploadPolicy };
// Upload the encrypted contents to the blob.
MemoryStream stream = new MemoryStream(buffer);
using (AutoResetEvent waitHandle = new AutoResetEvent(false))
{
ICancellableAsyncResult result = blob.BeginUploadFromStream(
stream, size, null, uploadOptions, null, ar => waitHandle.Set(), null);
waitHandle.WaitOne();
blob.EndUploadFromStream(result);
}
// Encrypt locally.
CryptoStream encryptedStream;
using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider())
{
string metadata = blob.Metadata[Constants.EncryptionConstants.BlobEncryptionData];
BlobEncryptionData encryptionData = JsonConvert.DeserializeObject<BlobEncryptionData>(metadata);
myAes.IV = encryptionData.ContentEncryptionIV;
myAes.Key = aesKey.UnwrapKeyAsync(encryptionData.WrappedContentKey.EncryptedKey, encryptionData.WrappedContentKey.Algorithm, CancellationToken.None).Result;
stream.Seek(0, SeekOrigin.Begin);
encryptedStream = new CryptoStream(stream, myAes.CreateEncryptor(), CryptoStreamMode.Read);
}
// Download the encrypted blob.
MemoryStream outputStream = new MemoryStream();
using (AutoResetEvent waitHandle = new AutoResetEvent(false))
{
ICancellableAsyncResult result = blob.BeginDownloadToStream(outputStream, ar => waitHandle.Set(), null);
waitHandle.WaitOne();
blob.EndDownloadToStream(result);
}
outputStream.Seek(0, SeekOrigin.Begin);
for (int i = 0; i < outputStream.Length; i++)
{
Assert.AreEqual(encryptedStream.ReadByte(), outputStream.ReadByte());
}
}
finally
{
container.DeleteIfExists();
}
}
示例2: CloudQueueMessageValidateEncryption
public void CloudQueueMessageValidateEncryption()
{
// 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 without decrypting
CloudQueueMessage retrMessage = queue.GetMessage(null, null, null);
// Decrypt locally
CloudQueueMessage decryptedMessage;
CloudQueueEncryptedMessage encryptedMessage = JsonConvert.DeserializeObject<CloudQueueEncryptedMessage>(retrMessage.AsString);
EncryptionData encryptionData = encryptedMessage.EncryptionData;
byte[] contentEncryptionKey = aesKey.UnwrapKeyAsync(encryptionData.WrappedContentKey.EncryptedKey, encryptionData.WrappedContentKey.Algorithm, CancellationToken.None).Result;
using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider())
{
myAes.Key = contentEncryptionKey;
myAes.IV = encryptionData.ContentEncryptionIV;
byte[] src = Convert.FromBase64String(encryptedMessage.EncryptedMessageContents);
using (ICryptoTransform decryptor = myAes.CreateDecryptor())
{
decryptedMessage = new CloudQueueMessage(decryptor.TransformFinalBlock(src, 0, src.Length));
}
}
TestHelper.AssertBuffersAreEqual(message.AsBytes, decryptedMessage.AsBytes);
}
finally
{
queue.DeleteIfExists();
}
}