I'm trying to encrypt a string in javascript (firefox) using SubtleCrypto. The problem with that is, that the encryption only works for short inputs. Once the string (testdata) is longer than 190 characters, it will fail with an OperationError. Why does SubtleCrypto behave like that and how can I resolve it?
Code:
function str2ab(str) {
var encoder = new TextEncoder('utf-8');
return encoder.encode(str);
}
function ab2str(buf) {
var decoder = new TextDecoder('utf-8');
return decoder.decode(buf);
}
var keypair;
var algorithmKeyGen = {
name: 'RSA-OAEP',
modulusLength: 2048,
publicExponent: new Uint8Array([1,
0,
1
]), // Equivalent to 65537
hash: {
name: 'SHA-256'
}
};
var crypter = window.crypto.subtle;
function encrypt(buffer) {
return crypter.encrypt(algorithmKeyGen, keypair.publicKey, buffer).then(
function(data) {
alert(ab2str(data));
},
function(error) {
alert(error);
}
);
}
var testdata = "aasasadasdaasasadasdaasazzzzzzzzzzzzzzzzzzzzuuuuuuuuuuuuuuuuuuuuuuuzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzuuuuudddsdfssssssssssdddddddddddzzzzzzzzzzzzzzzzzzzzzzzzzzzzzppppppggppppppppppppppppssssstt"
crypter.generateKey(algorithmKeyGen, true, [
'sign',
'encrypt',
'decrypt',
'verify',
'deriveKey'
]).then(function(res) {
keypair = res;
encrypt(str2ab(testdata));
}, console.error.bind(console, 'Unable to generate a key'));