CryptographicEngine.EncryptAndAuthenticate Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Esegue la crittografia autenticata.
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
Parametri
- key
- CryptographicKey
Chiave simmetrica da usare per la crittografia.
- data
- IBuffer
Dati da crittografare e autenticare.
- nonce
- IBuffer
Nonce da utilizzare. Un nonce è una variabile con la minima probabilità di ripetizione. Ad esempio, è possibile usare un valore casuale appena generato per ogni utilizzo, un timestamp, un numero di sequenza o una combinazione di questi. L'implementazione di Microsoft GCM richiede un nonce a 12 byte. L'implementazione di CCM richiede un nonce da 7 a 13 byte.
- authenticatedData
- IBuffer
Dati autenticati. Può essere Null.
Restituisce
Dati crittografati e autenticati. Se il metodo ha esito negativo, l'autenticazione ha esito negativo; se il metodo ha esito positivo, anche l'autenticazione è riuscita.
Esempio
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);
}
Commenti
La crittografia autenticata crittografa e autentica il contenuto in un'unica operazione. Un autenticatore, detto anche tag, viene usato durante la crittografia e l'output del processo contiene una coppia di testo crittografato tag. Per altre informazioni, vedere le proprietà AuthenticationTag e EncryptedData . Il processo di decrittografia verifica il testo crittografato sul tag.
È possibile usare un algoritmo di crittografia autenticato dopo aver chiamato il metodo OpenAlgorithm nella classe SymmetricKeyAlgorithmProvider e specificando il nome dell'algoritmo da aprire. I nomi degli algoritmi seguenti sono supportati per la crittografia autenticata e la decrittografia:
- SymmetricAlgorithmNames.AesGcm
- SymmetricAlgorithmNames.AesCcm Per un esempio completo contenente l'esempio di codice seguente, vedere la classe EncryptedAndAuthenticatedData .