CryptographicEngine.DecryptAndAuthenticate 方法

定义

解密数据并对其进行身份验证。 有关详细信息和完整的代码示例,请参阅 EncryptedAndAuthenticatedData

public:
 static IBuffer ^ DecryptAndAuthenticate(CryptographicKey ^ key, IBuffer ^ data, IBuffer ^ nonce, IBuffer ^ authenticationTag, IBuffer ^ authenticatedData);
 static IBuffer DecryptAndAuthenticate(CryptographicKey const& key, IBuffer const& data, IBuffer const& nonce, IBuffer const& authenticationTag, IBuffer const& authenticatedData);
public static IBuffer DecryptAndAuthenticate(CryptographicKey key, IBuffer data, IBuffer nonce, IBuffer authenticationTag, IBuffer authenticatedData);
function decryptAndAuthenticate(key, data, nonce, authenticationTag, authenticatedData)
Public Shared Function DecryptAndAuthenticate (key As CryptographicKey, data As IBuffer, nonce As IBuffer, authenticationTag As IBuffer, authenticatedData As IBuffer) As IBuffer

参数

key
CryptographicKey

要使用的对称密钥。

data
IBuffer

要解密和身份验证的数据。

nonce
IBuffer

要使用的 Nonce。 这必须与 EncryptAndAuthenticate 方法使用的 nonce 相同。

authenticationTag
IBuffer

身份验证标记。

authenticatedData
IBuffer

经过身份验证的数据。 这可以为 Null

返回

包含解密数据的缓冲区。如果方法失败,身份验证将失败;如果方法成功,则身份验证也成功。

示例

public void AuthenticatedDecryption(
    String strAlgName,
    CryptographicKey key,
    EncryptedAndAuthenticatedData objEncrypted,
    BinaryStringEncoding encoding,
    IBuffer buffNonce)
{
    // Declare a buffer to contain the decrypted data.
    IBuffer buffDecrypted;

    // Open a SymmetricKeyAlgorithmProvider object for the specified algorithm.
    SymmetricKeyAlgorithmProvider objAlgProv = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strAlgName);

    // The input key must be securely shared between the sender of the encrypted message
    // and the recipient. The nonce must also be shared but does not need to be shared
    // in a secure manner. If the sender encodes the message string to a buffer, the
    // binary encoding method must also be shared with the recipient.
    // The recipient uses the DecryptAndAuthenticate() method as follows to decrypt the 
    // message, authenticate it, and verify that it has not been altered in transit.
    buffDecrypted = CryptographicEngine.DecryptAndAuthenticate(
        key,
        objEncrypted.EncryptedData,
        buffNonce,
        objEncrypted.AuthenticationTag,
        null);

    // Convert the decrypted buffer to a string (for display). If the sender created the
    // original message buffer from a string, the sender must tell the recipient what 
    // BinaryStringEncoding value was used. Here, BinaryStringEncoding.Utf8 is used to
    // convert the message to a buffer before encryption and to convert the decrypted
    // buffer back to the original plaintext.
    String strDecrypted = CryptographicBuffer.ConvertBinaryToString(encoding, buffDecrypted);

}

注解

经过身份验证的加密在一个操作中加密和验证内容。 身份验证器(也称为标记)在加密期间使用,并且进程的输出包含标记密码文本对。 有关详细信息,请参阅 AuthenticationTagEncryptedData 属性。 解密过程针对 标记验证密码文本。

SymmetricKeyAlgorithmProvider 类上调用 OpenAlgorithm 方法并指定要打开的算法的名称后,可以使用经过身份验证的加密算法。 经过身份验证的加密和解密支持以下算法名称:

适用于

另请参阅