html 数据加密,加密数据 (HTML)

加密数据 (HTML)

12/11/2015

本文内容

[ 本文适用于编写 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;

}

相关主题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值