Decrypting data
Collapse the table of content
Expand the table of content

Decrypting data (HTML)

The following example shows how to use the Decrypt method to decrypt data.



function decryptDataBuffer(keyMaterial, stringToDecrypt, algNameString, keysize, ivBuffer) {

    // Input arguments:
    //    keyMaterial.......Buffer that contains random key material (same as that used for encryption )
    //    stringToEncrypt...String that contains information to be decrypted
    //    algNameString.....String that contains the name of the symmetric algrorithm to use
    //    keysize...........Requested key size (same as that used for encryption )
    //    ivBuffer..........Buffer that contains the initialization buffer (must match the IV used for encryption)
    
    var decryptedBuffer;
    try {

        // Convert the input string to binary.
        var inputDataBuffer = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(stringToDecrypt);
            
        // Open the algorithm provider for the algorithm specified on input.
        var algorithmProvider = Windows.Security.Cryptography.Core.SymmetricKeyAlgorithmProvider.openAlgorithm(algNameString);
        if (keysize > algorithmProvider.supportedKeyLengths.max) {
            // Handle error.
        }

        // Create a symmetric key.
        var symmetricKey = algorithmProvider.createSymmetricKey(keyMaterial);
            
        // Check the padding scheme and determine whether the data must be aligned.
        if (algorithmProvider.padding != Windows.Security.Cryptography.Core.CryptographicPadding.block) {
            // Data must be aligned by block size.
            var i = inputDataBuffer.length % algorithmProvider.blockLength;
            if (i > 0) {
                // Handle error.
            }
        }

        // Decrypt the input data.
        decryptedBuffer = Windows.Security.Cryptography.Core.CryptographicEngine.decrypt(symmetricKey, inputDataBuffer, ivBuffer);
     }
    catch (e) {
        // Handle error.
    }       
     
    return decryptedBuffer;
 }

Related topics

Encrypting and decrypting data

 

 

Show:
© 2017 Microsoft