我一直无法在 IE11 中找到任何 RSA-OAEP 加密的示例。
这是我的实现的一个片段,其中给了我一个非常模糊的错误。
function convertStringToArrayBufferView(str) {
var bytes = new Uint8Array(str.length);
for (var iii = 0; iii < str.length; iii++) {
bytes[iii] = str.charCodeAt(iii);
}
return bytes;
}
var crypto = window.crypto || window.msCrypto;
var config = {
name: 'RSA-OAEP',
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {
name: 'SHA-256'
}
};
var keyOp = crypto.subtle.generateKey(config, false, ['encrypt', 'decrypt']);
keyOp.onerror = function(e) {
console.error(e);
};
keyOp.oncomplete = function(e) {
encrypt(e.target.result);
};
function encrypt(keypair) {
var data = "abc1234444"
var encOp = crypto.subtle.encrypt({
name: config.name,
iv: config.iv || crypto.getRandomValues(new Uint8Array(16)),
key: keypair.publicKey
}, keypair.publicKey, convertStringToArrayBufferView(data));
encOp.onerror = function(e) {
console.error(e);
};
encOp.oncomplete = function(e) {
console.log({
data: new Uint8Array(e.target.result)
});
};
}
当我将算法更改为 RSAES-PKCS1-v1_5 时,一切正常
function convertStringToArrayBufferView(str) {
var bytes = new Uint8Array(str.length);
for (var iii = 0; iii < str.length; iii++) {
bytes[iii] = str.charCodeAt(iii);
}
return bytes;
}
var crypto = window.crypto || window.msCrypto;
var config = {
name: 'RSAES-PKCS1-v1_5',
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {
name: 'SHA-256'
}
};
var keyOp = crypto.subtle.generateKey(config, false, ['encrypt', 'decrypt']);
keyOp.onerror = function(e) {
console.error(e);
};
keyOp.oncomplete = function(e) {
encrypt(e.target.result);
};
function encrypt(keypair) {
var data = "abc1234444"
var encOp = crypto.subtle.encrypt({
name: config.name,
iv: config.iv || crypto.getRandomValues(new Uint8Array(16)),
key: keypair.publicKey
}, keypair.publicKey, convertStringToArrayBufferView(data));
encOp.onerror = function(e) {
console.error(e);
};
encOp.oncomplete = function(e) {
console.log({
data: new Uint8Array(e.target.result)
});
};
}