SymmetricKeyAlgorithmProvider.OpenAlgorithm | openAlgorithm method

Expand
This topic has not yet been rated - Rate this topic

SymmetricKeyAlgorithmProvider.OpenAlgorithm | openAlgorithm method

[This documentation is preliminary and is subject to change.]

Creates an instance of the SymmetricKeyAlgorithmProvider class and opens the specified algorithm for use.

Syntax


var symmetricKeyAlgorithmProvider = Windows.Security.Cryptography.Core.SymmetricKeyAlgorithmProvider.openAlgorithm(algorithm);

Parameters

algorithm

Type: String [JavaScript] | System.String [.NET] | Platform::String [C++]

Algorithm name.

Return value

Type: SymmetricKeyAlgorithmProvider

Represents a symmetric key algorithm provider.

Remarks

The following algorithm names are supported for use in this method:

  • No padding:
    • DES_CBC
    • DES_ECB
    • 3DES_CBC
    • 3DES_ECB
    • RC2_CBC
    • RC2_ECB
    • AES_CBC
    • AES_ECB
  • PKCS#7 block padding modes:
    • AES_CBC_PKCS7
    • AES_ECB_PKCS7
    • DES_CBC_PKCS7
    • DES_ECB_PKCS7
    • 3DES_CBC_PKCS7
    • 3DES_ECB_PKCS7
    • RC2_CBC_PKCS7
    • RC2_ECB_PKCS7
  • Authenticated modes (see the EncryptedAndAuthenticatedData class):
    • AES_GCM
    • AES_CCM
  • Stream Cipher:
    • RC4

Examples


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

    // Demonstrate how to retrieve the name of the algorithm used.
    String strAlgNameUsed = objAlg.AlgorithmName;

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


Requirements

Minimum supported client

Windows 8 Release Preview

Minimum supported server

Windows Server 2012

Namespace

Windows.Security.Cryptography.Core
Windows::Security::Cryptography::Core [C++]

Metadata

Windows.winmd

See also

SymmetricKeyAlgorithmProvider

 

 

Build date: 5/22/2012

Did you find this helpful?
(1500 characters remaining)
Community Additions ADD