Compartir a través de


CryptographicEngine.Encrypt(CryptographicKey, IBuffer, IBuffer) Método

Definición

Cifra los datos mediante un algoritmo simétrico o asimétrico.

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

Parámetros

key
CryptographicKey

Clave criptográfica que se va a usar para el cifrado. Puede ser una clave asimétrica o simétrica. Para obtener más información, vea AsymmetricKeyAlgorithmProvider y SymmetricKeyAlgorithmProvider.

data
IBuffer

Datos que se van a cifrar.

iv
IBuffer

Búfer que contiene el vector de inicialización. Puede ser null para un algoritmo simétrico y siempre debe ser NULL para un algoritmo asimétrico. Si se usó un vector de inicialización (IV) para cifrar los datos, debe usar el mismo IV para descifrar los datos. Puede usar el método GenerateRandom para crear un IV que contenga datos aleatorios. Otros IV, como los vectores generados por nonce, requieren una implementación personalizada. Para más información, consulte Claves criptográficas.

Los algoritmos de modo de cifrado de bloques de encadenamiento de bloques (CBC) requieren un vector de inicialización. Para obtener más información, vea la sección Comentarios.

Devoluciones

Datos cifrados.

Ejemplos

public IBuffer SampleCipherEncryption(
    String strMsg,
    String strAlgName,
    UInt32 keyLength,
    out BinaryStringEncoding encoding,
    out IBuffer iv,
    out CryptographicKey key)
{
    // Initialize the initialization vector.
    iv = null;

    // Initialize the binary encoding value.
    encoding = BinaryStringEncoding.Utf8;

    // Create a buffer that contains the encoded message to be encrypted. 
    IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg, encoding);

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

    // Determine whether the message length is a multiple of the block length.
    // This is not necessary for PKCS #7 algorithms which automatically pad the
    // message to an appropriate length.
    if (!strAlgName.Contains("PKCS7"))
    {
        if ((buffMsg.Length % objAlg.BlockLength) != 0)
        {
            throw new Exception("Message buffer length must be multiple of block length.");
        }
    }

    // Create a symmetric key.
    IBuffer keyMaterial = CryptographicBuffer.GenerateRandom(keyLength);
    key = objAlg.CreateSymmetricKey(keyMaterial);

    // CBC algorithms require an initialization vector. Here, a random
    // number is used for the vector.
    if (strAlgName.Contains("CBC"))
    {
        iv = CryptographicBuffer.GenerateRandom(objAlg.BlockLength);
    }

    // Encrypt the data and return.
    IBuffer buffEncrypt = CryptographicEngine.Encrypt(key, buffMsg, iv);
    return buffEncrypt;
}

Comentarios

De los algoritmos simétricos admitidos por Microsoft, lo siguiente requiere un vector de inicialización:

Se aplica a

Consulte también