1

I'm struggling to discover how to transform an javascript object, into some variable (array?) and save it with a code like:

function onDownload() {
    document.location = 'data:Application/octet-stream,' +
                         encodeURIComponent(RsaKey);
}

Why?

The library cryptico (documentation: github) gives-me an easy to use RSA interface. But my nightmare came when the generated key pair, is an object. I don't know how to export this object from the client's browser, to a file, for later using. The public part of the key, can be converted in string, by the function

publicKeyString

But the private part, is inside the object. I read about the RSA algorithm and saw that the "d" is what matters in the private key, and I have the "d" in the key gen function, inside the rsa.js

        this.d = ee.modInverse(phi);

I tried to get this value and put into a string, but didn't worked. Don't know what else to do. Appreciate any help.

user2864778
  • 333
  • 5
  • 18
  • You can save entire objects to readable JSON strings using 'JSON.stringify(obj);'. Is that what you're looking for? – Bassdrop Cumberwubwubwub Dec 24 '14 at 13:23
  • I have too little knowledge in javascript, and in JSON I know nothing. I prefer a pure javascript solution, if possible. And I don't know if the best way to do this, is transform the entire object into a string. I would prefer to have only the private key part saved... – user2864778 Dec 24 '14 at 14:08

1 Answers1

1

If you are using cryptico.js can use rsa.js and jsbn.js and add from the repository(https://github.com/wwwtyro/cryptico) to your project.

And try this for save on localStorage:

localStorage["strPrivateKey"] = JSON.stringify(RSAKey.toJSON());

And try this for get back the key:

var key = RSAParse(localStorage["strPrivateKey"]);

If for some reason RSAParse() isn't on rsa.js you can added:

function RSAParse(rsaString) {
    var json = JSON.parse(rsaString);
    var rsa = new RSAKey();

    rsa.setPrivateEx(json.n, json.e, json.d, json.p, json.q, json.dmp1, json.dmq1, json.coeff);

    return rsa;
}