Internet Explorer 11中的公钥加密 [英] Public key encryption in Internet Explorer 11
本文介绍了Internet Explorer 11中的公钥加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< script>
var data = new Uint8Array([0,1,2,3,4,5,6,7,8,9]);
var crypto = window.crypto || window.msCrypto;
var cryptoSubtle = crypto.subtle;
var genOp = cryptoSubtle.generateKey(
{
name:RSA-OAEP,
modulusLength:2048,
publicExponent:new Uint8Array 0x01,0x00,0x01]),
哈希:{名称:SHA-256},
},
true,
[encrypt,decrypt]
);
genOp.onerror = function(e){
console.error(e);
};
genOp.oncomplete = function(e){
var key = e.target.result;
console.log(key);
console.log(key.publicKey);
var encOp = cryptoSubtle.encrypt(
{
name:RSA-OAEP
},
key.publicKey,
data
);
encOp.onerror = function(e){
console.error(e);
};
encOp.oncomplete = function(e){
var encrypted = e.target.result;
console.log(new Uint8Array(encrypted));
};
};
< / script>
它生成密钥对,但无法使用错误事件进行加密。具有对称AES密钥的类似代码工作。 IE11支持公钥加密吗?我的代码有什么问题吗?
解决方案
我发现了这个的原因。调用加密调用时需要添加哈希字段:
var encOp = cryptoSubtle.encrypt(
{
名称:RSA-OAEP,
哈希:{name:SHA-256}
},
key.publicKey,
data
) ;
这与 Web Cryptography API规范,但它有效。
I am trying to implement public key encryption using JavaScript for IE11 with the following code:
<script>
var data = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
var crypto = window.crypto || window.msCrypto;
var cryptoSubtle = crypto.subtle;
var genOp = cryptoSubtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: { name: "SHA-256" },
},
true,
["encrypt", "decrypt"]
);
genOp.onerror = function (e) {
console.error(e);
};
genOp.oncomplete = function (e) {
var key = e.target.result;
console.log(key);
console.log(key.publicKey);
var encOp = cryptoSubtle.encrypt(
{
name: "RSA-OAEP"
},
key.publicKey,
data
);
encOp.onerror = function (e) {
console.error(e);
};
encOp.oncomplete = function (e) {
var encrypted = e.target.result;
console.log(new Uint8Array(encrypted));
};
};
</script>
It generates the key pair but fails to do the encryption with an error event. Similar code with a symmetric AES key works. Is public key encryption supported by IE11? Is there anything wrong with my code?
解决方案
I've found out the cause of this. I need to add the hash field when invoking the encrypt call:
var encOp = cryptoSubtle.encrypt(
{
name: "RSA-OAEP",
hash: { name: "SHA-256" }
},
key.publicKey,
data
);
This does not match the Web Cryptography API specification but it works.
这篇关于Internet Explorer 11中的公钥加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文