데이터 암호화(HTML)

[ 이 문서는 Windows 런타임 앱을 작성하는 Windows에서 8.x 및 Windows Phone 8.x 개발자를 대상으로 합니다. Windows 10용으로 개발하는 경우에는 최신 설명서를 참조하세요.]

다음 예제에서는 encrypt 메서드를 사용하여 데이터를 암호화하는 방법을 보여 줍니다.


function encryptDataBuffer(keyMaterial, stringToEncrypt, algNameString, keysize, ivBuffer) {

    // Input arguments:
    //    keyMaterial.......Buffer that contains random key material (see the GenerateRandom method)
    //    stringToEncrypt...String that contains information to be encrypted
    //    algNameString.....String that contains the name of the symmetric algrorithm to use
    //    keysize...........Requested key size
    //    ivBuffer..........Buffer that contains the initialization buffer (this can be random data or custom defined)

    var encryptedBuffer;

    try {
            
        // Convert the input string to binary.
        var inputDataBuffer = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(stringToEncrypt, Windows.Security.Cryptography.BinaryStringEncoding.utf8);
            
        // Open the algorithm provider for the algorithm specified on input.
        var algorithmProvider = Windows.Security.Cryptography.Core.SymmetricKeyAlgorithmProvider.openAlgorithm(algNameString);

        // Check that the requested key size specified on input does not exceed the maximum size supported by the algorithm.
        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.
            }
        }

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

관련 항목

데이터 암호화 및 암호 해독