-
-
Notifications
You must be signed in to change notification settings - Fork 645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encryption and decryption errors occur repeatedly #582
Comments
import { KJUR, hextob64, KEYUTIL, b64utohex, RSAKey } from 'jsrsasign'; |
I've tried encryption and decryption with your keys and data without your Promise codes 100 times. Then they didn't fail. #!/usr/bin/env node
var rs = require("jsrsasign");
const key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6kw0JFxZqgxISVe54G+ZHeyOoIpGr++DqplOQuijlR48GhkPjYd99J5DwQQXwb8lbNUPU7iwlQ7sqfNAMjk/CXmMM56th/jPlMEydZuzn+/SmNMPsVUd+Cn3dUMQMhZZZcNkFZuVuIzTSQsrjUah7dGdiozsKyriBbksiehO2i7Vc61PlTJOU/1PCgKHjsaw5qzB9Qfh2R/esM9miuiOKEiA4kupij3w7GTwi6NZ/VjSD+2uDgCpaIWjhxJQIT6kzE6hjx12wV94eMkKvkv9F/sZGmCBefFVzvahUmINTe4xGdainQ7aHZkYdHWJpnFm7N/u/MdNMAmawLv8RbKlwQIDAQAB";
const pKey = "MIIEpAIBAAKCAQEA6kw0JFxZqgxISVe54G+ZHeyOoIpGr++DqplOQuijlR48GhkPjYd99J5DwQQXwb8lbNUPU7iwlQ7sqfNAMjk/CXmMM56th/jPlMEydZuzn+/SmNMPsVUd+Cn3dUMQMhZZZcNkFZuVuIzTSQsrjUah7dGdiozsKyriBbksiehO2i7Vc61PlTJOU/1PCgKHjsaw5qzB9Qfh2R/esM9miuiOKEiA4kupij3w7GTwi6NZ/VjSD+2uDgCpaIWjhxJQIT6kzE6hjx12wV94eMkKvkv9F/sZGmCBefFVzvahUmINTe4xGdainQ7aHZkYdHWJpnFm7N/u/MdNMAmawLv8RbKlwQIDAQABAoIBAHHckKZu/uAl0edaq6lHcqjZMkMQj+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Q+PEwvtK4ORGA3FIPgfDXaV5g7gThsTGTZ11GAKuzdtQSCPRuRR1+wcxZaGF0WfLJw1Jvd8vKEt2qBJ+Jrlj4rnH2mZ9n3ZFEof14L7iQ==";
var data = "ngzCT7HX5GK3gLaT";
let pubkey = rs.KEYUTIL.getKey(rs.b64topem(key, "PUBLIC KEY"));
let prvkey = rs.KEYUTIL.getKey(rs.b64topem(pKey, "RSA PRIVATE KEY"));
let countSuccess = 0;
for (let i = 0; i < 100; i++) {
//console.log(data);
let hEnc = rs.KJUR.crypto.Cipher.encrypt(data, pubkey, "RSAOAEP256");
//console.log(hEnc);
let sDec = rs.KJUR.crypto.Cipher.decrypt(hEnc, prvkey, "RSAOAEP256");
//console.log(sDec);
if (data == sDec) countSuccess++;
}
console.log(`SUCCESS=${countSuccess} FAIL=${100 - countSuccess}`); This shows:
It seems to having issue in your code. Thank you. |
Adding a little bit more frequency will make it appear |
Hello @davedoesdev , Thank you so much for your contribution for RSA OAEP encryption and decryption. As reported above, your OAEP encryption function will occasionally generate wrong encrypted data which can't be decrypted. Failure rate is approxmately 0.5% or so. Could you help me to fix it? Here is a test code to reproduce the issue: #!/usr/bin/env node
var rs = require("jsrsasign");
const key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6kw0JFxZqgxISVe54G+ZHeyOoIpGr++DqplOQuijlR48GhkPjYd99J5DwQQXwb8lbNUPU7iwlQ7sqfNAMjk/CXmMM56th/jPlMEydZuzn+/SmNMPsVUd+Cn3dUMQMhZZZcNkFZuVuIzTSQsrjUah7dGdiozsKyriBbksiehO2i7Vc61PlTJOU/1PCgKHjsaw5qzB9Qfh2R/esM9miuiOKEiA4kupij3w7GTwi6NZ/VjSD+2uDgCpaIWjhxJQIT6kzE6hjx12wV94eMkKvkv9F/sZGmCBefFVzvahUmINTe4xGdainQ7aHZkYdHWJpnFm7N/u/MdNMAmawLv8RbKlwQIDAQAB";
const pKey = "MIIEpAIBAAKCAQEA6kw0JF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NFeII0X+299MfM+whadKEbFKs6tcL738nvbx+QeueVMNfWcLX/K72p/cnYR1tmDGTBmnjnKPuAZ7NatKBISe79ikHRSh64cEGNv9+rPynNei4kOnpwepQBuk8icOk0OmeZe6WkQKBgQDO221uZSXYW0lVoPJHiM8UW6l+3meue4u7sr79DxEO99P2i34rwhdSEeVwmu3RVoXHbvV8eEQb6zyM+l14Uv+rPnoSOnm+JJ/qaxVBtASHM3/kPnm5W4Uky0ZHBlrxJ+g5PCnj6vfGlbrebh0fbzGExCnCLHEddGkY60Gb3CrYyQKBgQCqAqg9InHjP0ma+Co7ptHYAzm46+Jq7QowsCl07SoTujVf9qaYpohdhqp8kXVonwUrQQ+PEwvtK4ORGA3FIPgfDXaV5g7gThsTGTZ11GAKuzdtQSCPRuRR1+wcxZaGF0WfLJw1Jvd8vKEt2qBJ+Jrlj4rnH2mZ9n3ZFEof14L7iQ==";
var data = "ngzCT7HX5GK3gLaT";
let pubkey = rs.KEYUTIL.getKey(rs.b64topem(key, "PUBLIC KEY"));
let prvkey = rs.KEYUTIL.getKey(rs.b64topem(pKey, "RSA PRIVATE KEY"));
let numTry = 1000;
let countSuccess = 0;
let countException = 0;
for (let i = 0; i < numTry; i++) {
let hEnc, sDec;
hEnc = pubkey.encryptOAEP(data, "sha256");
try {
sDec = prvkey.decryptOAEP(hEnc, "sha256");
if (data == sDec) countSuccess++;
} catch(ex) {
console.log("DECRYPT FAILED");
console.log(hEnc);
countException++;
}
}
console.log(`SUCCESS=${countSuccess} FAIL=${numTry - countSuccess - countException} EXCEPTION=${countException}`); And below is its output:
|
Sure, I'll take a look. Might be a few days though. |
@kjur How do I build |
Hi @davedoesdev , I've attached some script to generate jsrsasign.js or jsrsasign-all-min.js without minification. These scripts will generate "lib/jsrsasign.js" and "lib/jsrsasign-all-min.js". USAGE
|
Hi @davedoesdev san, Current lib/jsrsasign.js and lib/jsrsasign-all-min.js generator depend on old and wrong "YUI compressor" and I think it would be better not to introduce you such generator. For your debugging, above scripts may enough. |
Otherwise I can write a QUnit test script for the issue like other test/qunit-*.html test scripts. Please ask me if you want it. |
Hi @yuanchangjian , this issue was fixed in 10.8.2 release today. Hi @davedoesdev san, thank you kindly for your help. |
Just FYI. Here is mocha test code for 10,000 times RSA OAEP encryption and decryption. It takes too much time so that it is not included in the release: // for issue 582, 583
var assert = require('assert');
var rs = require('../lib/jsrsasign.js');
const pubb64 = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6kw0JFxZqgxISVe54G+ZHeyOoIpGr++DqplOQuijlR48GhkPjYd99J5DwQQXwb8lbNUPU7iwlQ7sqfNAMjk/CXmMM56th/jPlMEydZuzn+/SmNMPsVUd+Cn3dUMQMhZZZcNkFZuVuIzTSQsrjUah7dGdiozsKyriBbksiehO2i7Vc61PlTJOU/1PCgKHjsaw5qzB9Qfh2R/esM9miuiOKEiA4kupij3w7GTwi6NZ/VjSD+2uDgCpaIWjhxJQIT6kzE6hjx12wV94eMkKvkv9F/sZGmCBefFVzvahUmINTe4xGdainQ7aHZkYdHWJpnFm7N/u/MdNMAmawLv8RbKlwQIDAQAB";
const prvb64 = "MIIEpAIBAAKCAQEA6kw0JFxZqgxISVe54G+ZHeyOoIpGr++DqplOQuijlR48GhkPjYd99J5DwQQXwb8lbNUPU7iwlQ7sqfNAMjk/CXmMM56th/jPlMEydZuzn+/SmNMPsVUd+Cn3dUMQMhZZZcNkFZuVuIzTSQsrjUah7dGdiozsKyriBbksiehO2i7Vc61PlTJOU/1PCgKHjsaw5qzB9Qfh2R/esM9miuiOKEiA4kupij3w7GTwi6NZ/VjSD+2uDgCpaIWjhxJQIT6kzE6hjx12wV94eMkKvkv9F/sZGmCBefFVzvahUmINTe4xGdainQ7aHZkYdHWJpnFm7N/u/MdNMAmawLv8RbKlwQIDAQABAoIBAHHckKZu/uAl0edaq6lHcqjZMkMQj+OmSV1Z3W+ksQKhy+F+9lD4XJzJYPHnNEhixSRLsoQkSCRYOWkqHrOJoHnRSPI5F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LJw1Jvd8vKEt2qBJ+Jrlj4rnH2mZ9n3ZFEof14L7iQ==";
const data = "ngzCT7HX5GK3gLaT";
describe("rsa oaep enc dec loop", function() {
describe("test", function() {
let pubkey = rs.KEYUTIL.getKey(rs.b64topem(pubb64, "PUBLIC KEY"));
let prvkey = rs.KEYUTIL.getKey(rs.b64topem(prvb64, "RSA PRIVATE KEY"));
let numTry = 10000;
//let numTry = 1;
let countSuccess = 0;
let countException = 0;
for (let i = 0; i < numTry; i++) {
let hEnc, sDec;
hEnc = pubkey.encryptOAEP(data, "sha256");
//console.log(hEnc);
try {
sDec = prvkey.decryptOAEP(hEnc, "sha256");
if (data == sDec) countSuccess++;
} catch(ex) {
console.log(ex);
countException++;
}
}
//console.log(`${countSuccess} ${countException}`);
assert.equal(countSuccess, numTry);
assert.equal(countException, 0);
});
}); |
`
export class LYRSA extends LYObject implements ILYRSA {
private _publicKey: string;
}
`
`
const key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6kw0JFxZqgxISVe54G+ZHeyOoIpGr++DqplOQuijlR48GhkPjYd99J5DwQQXwb8lbNUPU7iwlQ7sqfNAMjk/CXmMM56th/jPlMEydZuzn+/SmNMPsVUd+Cn3dUMQMhZZZcNkFZuVuIzTSQsrjUah7dGdiozsKyriBbksiehO2i7Vc61PlTJOU/1PCgKHjsaw5qzB9Qfh2R/esM9miuiOKEiA4kupij3w7GTwi6NZ/VjSD+2uDgCpaIWjhxJQIT6kzE6hjx12wV94eMkKvkv9F/sZGmCBefFVzvahUmINTe4xGdainQ7aHZkYdHWJpnFm7N/u/MdNMAmawLv8RbKlwQIDAQAB"
const pKey = "MIIEpAIBAAKCAQEA6kw0JFxZqgxISVe54G+ZHeyOoIpGr++DqplOQuijlR48GhkPjYd99J5DwQQXwb8lbNUPU7iwlQ7sqfNAMjk/CXmMM56th/jPlMEydZuzn+/SmNMPsVUd+Cn3dUMQMhZZZcNkFZuVuIzTSQsrjUah7dGdiozsKyriB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l+3meue4u7sr79DxEO99P2i34rwhdSEeVwmu3RVoXHbvV8eEQb6zyM+l14Uv+rPnoSOnm+JJ/qaxVBtASHM3/kPnm5W4Uky0ZHBlrxJ+g5PCnj6vfGlbrebh0fbzGExCnCLHEddGkY60Gb3CrYyQKBgQCqAqg9InHjP0ma+Co7ptHYAzm46+Jq7QowsCl07SoTujVf9qaYpohdhqp8kXVonwUrQQ+PEwvtK4ORGA3FIPgfDXaV5g7gThsTGTZ11GAKuzdtQSCPRuRR1+wcxZaGF0WfLJw1Jvd8vKEt2qBJ+Jrlj4rnH2mZ9n3ZFEof14L7iQ=="
@register('LYTestRSA')
class LYTestRSA extends LYRSA {
protected getPublicKey(): Promise {
return Promise.resolve(key);
}
}
describe.only('LYEnv', function () {
this.timeout(3000000);
});
`
The text was updated successfully, but these errors were encountered: