CryptographicEngine.Decrypt(CryptographicKey, IBuffer, IBuffer) 方法

定义

解密以前使用对称或非对称算法加密的内容。

public:
 static IBuffer ^ Decrypt(CryptographicKey ^ key, IBuffer ^ data, IBuffer ^ iv);
 static IBuffer Decrypt(CryptographicKey const& key, IBuffer const& data, IBuffer const& iv);
public static IBuffer Decrypt(CryptographicKey key, IBuffer data, IBuffer iv);
function decrypt(key, data, iv)
Public Shared Function Decrypt (key As CryptographicKey, data As IBuffer, iv As IBuffer) As IBuffer

参数

key
CryptographicKey

用于解密的加密密钥。 这可以是非对称密钥,也可以是对称密钥。 有关详细信息,请参阅 AsymmetricKeyAlgorithmProviderSymmetricKeyAlgorithmProvider

data
IBuffer

包含加密数据的缓冲区。

iv
IBuffer

包含初始化向量的缓冲区。 如果使用初始化向量 (IV) 来加密数据,则必须使用相同的 IV 来解密数据。 有关详细信息,请参阅 加密

返回

已解密的数据。

示例

public void SampleCipherDecryption(
    String strAlgName,
    IBuffer buffEncrypt,
    IBuffer iv,
    BinaryStringEncoding encoding,
    CryptographicKey key)
{
    // Declare a buffer to contain the decrypted data.
    IBuffer buffDecrypted;

    // Open an symmetric algorithm provider for the specified algorithm. 
    SymmetricKeyAlgorithmProvider objAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strAlgName);

    // The input key must be securely shared between the sender of the encrypted message
    // and the recipient. The initialization vector must also be shared but does not
    // need to be shared in a secure manner. If the sender encodes a message string 
    // to a buffer, the binary encoding method must also be shared with the recipient.
    buffDecrypted = CryptographicEngine.Decrypt(key, buffEncrypt, iv);

    // 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);
}

注解

key 参数可以是使用 PersistedKeyProvider 类从证书获取的持久密钥。

如果密钥是持久密钥,并且解密操作需要 UI 或花费很长时间,请改用 DecryptAsync 方法。 例如,使用受强保护的密钥进行解密时,需要 UI。 如果使用持久化密钥且需要 UI,请使用 DecryptAsync 方法,因为 Decrypt 方法将失败。

适用于

另请参阅