Share via


CryptographicEngine.EncryptAndAuthenticate Método

Definição

Executa a criptografia autenticada.

public:
 static EncryptedAndAuthenticatedData ^ EncryptAndAuthenticate(CryptographicKey ^ key, IBuffer ^ data, IBuffer ^ nonce, IBuffer ^ authenticatedData);
 static EncryptedAndAuthenticatedData EncryptAndAuthenticate(CryptographicKey const& key, IBuffer const& data, IBuffer const& nonce, IBuffer const& authenticatedData);
public static EncryptedAndAuthenticatedData EncryptAndAuthenticate(CryptographicKey key, IBuffer data, IBuffer nonce, IBuffer authenticatedData);
function encryptAndAuthenticate(key, data, nonce, authenticatedData)
Public Shared Function EncryptAndAuthenticate (key As CryptographicKey, data As IBuffer, nonce As IBuffer, authenticatedData As IBuffer) As EncryptedAndAuthenticatedData

Parâmetros

key
CryptographicKey

Chave simétrica a ser usada para criptografia.

data
IBuffer

Dados a serem criptografados e autenticados.

nonce
IBuffer

Nonce a ser usado. Um nonce é uma variável que tem chance mínima de repetir. Por exemplo, você pode usar um valor aleatório que é recém-gerado para cada uso, um carimbo de data/hora, um número de sequência ou alguma combinação deles. A implementação do Microsoft GCM requer um nonce de 12 bytes. A implementação do CCM requer um nonce de 7 a 13 bytes.

authenticatedData
IBuffer

Dados autenticados. Isso pode ser Null.

Retornos

Os dados criptografados e autenticados. Se o método falhar, a autenticação falhará; se o método for bem-sucedido, a autenticação também terá êxito.

Exemplos

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

}

Comentários

A criptografia autenticada criptografa e autentica o conteúdo em uma operação. Um autenticador, também chamado de marca, é usado durante a criptografia e a saída do processo contém um par de texto cifrado de marca. Para obter mais informações, consulte as propriedades AuthenticationTag e EncryptedData . O processo de descriptografia verifica o texto cifrado em relação à marca.

Você pode usar um algoritmo de criptografia autenticado depois de chamar o método OpenAlgorithm na classe SymmetricKeyAlgorithmProvider e especificar o nome do algoritmo a ser aberto. Há suporte para os seguintes nomes de algoritmo para criptografia e descriptografia autenticadas:

Aplica-se a

Confira também