使用 .NET 创建内容密钥
警告
请在 2024 年 2 月 29 日之前将 Azure 媒体服务 REST API 和 SDK 更新到 v3。 版本 3 的 Azure 媒体服务 REST API 与适用于 .NET 和 Java 的客户端 SDK 提供了比版本 2 更多的功能。 我们即将停用版本 2 的 Azure 媒体服务 REST API 与适用于 .NET 和 Java 的客户端 SDK。
必需的操作:若要最大程度地减少工作负载的中断,请参阅迁移指南,在 2024 年 2 月 29 日之前将代码从版本 2 的 API 和 SDK 转换为版本 3 的 API 和 SDK。 2024 年 2 月 29 日之后,Azure 媒体服务将不再接受来自版本 2 REST API、ARM 帐户管理 API 版本 2015-10-01 或版本 2 的 .NET 客户端 SDK 的流量。这其中包括可以调用版本 2 API 的任何第三方开源客户端 SDK。从媒体服务 v3 概述开始了解最新版本。
媒体服务允许创建资产和传送加密的资产。 ContentKey 提供对资产的安全访问。
创建新资产时(例如,上传文件之前),可以指定以下加密选项:StorageEncrypted、CommonEncryptionProtected 或 EnvelopeEncryptionProtected。
将资产传送到客户端时,可以使用以下两个加密选项之一将资产配置为动态加密:DynamicEnvelopeEncryption 或 DynamicCommonEncryption。
加密的资产必须与 ContentKey 关联。 本文介绍如何创建内容密钥。
注意
使用媒体服务 .NET SDK 创建新的 StorageEncrypted 资产时,会自动创建 ContentKey 并将其链接到资产。
ContentKeyType
创建内容密钥时必须设置的值之一是内容密钥类型。 选择以下值之一。
public enum ContentKeyType
{
/// <summary>
/// Specifies a content key for common encryption.
/// </summary>
/// <remarks>This is the default value.</remarks>
CommonEncryption = 0,
/// <summary>
/// Specifies a content key for storage encryption.
/// </summary>
StorageEncryption = 1,
/// <summary>
/// Specifies a content key for configuration encryption.
/// </summary>
ConfigurationEncryption = 2,
/// <summary>
/// Specifies a content key for Envelope encryption. Only used internally.
/// </summary>
EnvelopeEncryption = 4
}
创建信封类型 ContentKey
以下代码段创建信封加密类型的内容密钥。 然后,它将密钥与指定的资产关联。
static public IContentKey CreateEnvelopeTypeContentKey(IAsset asset)
{
// Create envelope encryption content key
Guid keyId = Guid.NewGuid();
byte[] contentKey = GetRandomBuffer(16);
IContentKey key = _context.ContentKeys.Create(
keyId,
contentKey,
"ContentKey",
ContentKeyType.EnvelopeEncryption);
asset.ContentKeys.Add(key);
return key;
}
static private byte[] GetRandomBuffer(int size)
{
byte[] randomBytes = new byte[size];
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(randomBytes);
}
return randomBytes;
}
call
IContentKey key = CreateEnvelopeTypeContentKey(encryptedsset);
创建公共类型 ContentKey
以下代码段创建公共加密类型的内容密钥。 然后,它将密钥与指定的资产关联。
static public IContentKey CreateCommonTypeContentKey(IAsset asset)
{
// Create common encryption content key
Guid keyId = Guid.NewGuid();
byte[] contentKey = GetRandomBuffer(16);
IContentKey key = _context.ContentKeys.Create(
keyId,
contentKey,
"ContentKey",
ContentKeyType.CommonEncryption);
// Associate the key with the asset.
asset.ContentKeys.Add(key);
return key;
}
static private byte[] GetRandomBuffer(int length)
{
var returnValue = new byte[length];
using (var rng =
new System.Security.Cryptography.RNGCryptoServiceProvider())
{
rng.GetBytes(returnValue);
}
return returnValue;
}
call
IContentKey key = CreateCommonTypeContentKey(encryptedsset);