nessuna biblioteca amici necessari,
Enter crypto
Ecco un piccolo modulo di janky si potrebbe usare per cifrare/decifrare le stringhe con chiavi RSA:
var crypto = require("crypto");
var path = require("path");
var fs = require("fs");
var encryptStringWithRsaPublicKey = function(toEncrypt, relativeOrAbsolutePathToPublicKey) {
var absolutePath = path.resolve(relativeOrAbsolutePathToPublicKey);
var publicKey = fs.readFileSync(absolutePath, "utf8");
var buffer = new Buffer(toEncrypt);
var encrypted = crypto.publicEncrypt(publicKey, buffer);
return encrypted.toString("base64");
};
var decryptStringWithRsaPrivateKey = function(toDecrypt, relativeOrAbsolutePathtoPrivateKey) {
var absolutePath = path.resolve(relativeOrAbsolutePathtoPrivateKey);
var privateKey = fs.readFileSync(absolutePath, "utf8");
var buffer = new Buffer(toDecrypt, "base64");
var decrypted = crypto.privateDecrypt(privateKey, buffer);
return decrypted.toString("utf8");
};
module.exports = {
encryptStringWithRsaPublicKey: encryptStringWithRsaPublicKey,
decryptStringWithRsaPrivateKey: decryptStringWithRsaPrivateKey
}
lo consiglio, non utilizzando metodi fs sincroni, ove possibile, e si potrebbe utilizzare promette di rendere questo meglio, ma per un uso semplice casi questo è l'approccio che ho visto lavorare e vorrei prendere
fonte
2015-06-25 21:46:29
Questo funziona per me ... è meglio andare nativo ... – ATOzTOA
Grazie per la modifica @uzyn –
Grazie per la soluzione @JacobMcKay Mi ha salvato un paio d'ore. – uzyn