摘要
Keypair是一个npm包,用于生成公钥和私钥对。本文将介绍如何使用该npm包生成密钥对,并演示如何在Node.js环境中使用它进行加密和解密。
安装
要安装Keypair,只需运行以下命令:
npm install keypair
生成密钥对
要生成密钥对,可以使用以下代码:
const keypair = require('keypair'); const pair = keypair(); console.log(pair);
这将输出包含公钥和私钥的对象,如下所示:
{ public: '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCiZa+7V1FyJX9Kzj8WxPByo18Y\n3qE6SvL8wrmO5Uc/LyCqXmX+uAqlpR6U/wJZOR5r6aQDM6Fk2K2LuO6N/04zYoBf\niaN1GK0YILVuMdpdFW/61SL+NRrlBz+q3zenyGuHc1x5dtJG5CG6U5eyZrQWO6JL\nbv2QbVsKMAyfXhX9CQIDAQAB\n-----END PUBLIC KEY-----', private: '-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQCiZa+7V1FyJX9Kzj8WxPByo18Y3qE6SvL8wrmO5Uc/LyCqXmX+\nuAqlpR6U/wJZOR5r6aQDM6Fk2K2LuO6N/04zYoBfiaN1GK0YILVuMdpdFW/61SL+\nNRrlBz+q3zenyGuHc1x5dtJG5CG6U5eyZrQWO6JLbv2QbVsKMAyfXhX9CQIDAQAB\nAoGAWzrzTB3qbrgshz9XOxwcLlU6RZG6l41Q38f5DmWAiCzb+vV7kcf4d8yg0hg4\nNTHqYV7eiswD8LSAP7SIRkXiHCuTtTOWTdr8NGvO9IYsZs0sQsNoW8HuFN7rqOjP\noCn2vfG1kwr9sg9XpZbYc1wwgJMJFS8IlPdnpBAFq3cseCECQQDS6Lk1ecX/U1Dl\ng5szAK+m6o5n2MX+0VCyBcN6xRV65yJvX0CY5vv0G5ue5FQMf6tr0LrOa+s/QunC\nQMdEJbTDAkEAxkeODZg1cUT/pLJJP8Ov/mkMNyfmLy1yvyA+yTJ8zvB9PfLJtVQ2\nkmn8MvKd1z4ME7FYdB/aA/Fo8lVb4Z3YqQJAMUUw+uwd1SjSgOzIbTmFWK8WgX9\nOdkYHmQa0c92aWJiEysvN6GPfBok7RllFuWxQ4KzY3Z/MyFAkVKh8D/ipwJBAIEh\ndnKoUc+5x5XnJ1CFZU1/a5UJr6F+2zlRO/gzmD22j2LLGxi73b10xXz+eYJpM++h\nTTbeIpBfZ+ByP6fjyHsCQEtLaEUV9EMzmiAFjScZ6/u1lOK19Fw+g28agMqy0jKc\nHG+5xGiXkA6iZU > 来源:[JavaScript中文网](https://www.javascriptcn.com/post/54310) ,转载请注明来源 [https://www.javascriptcn.com/post/54310](https://www.javascriptcn.com/post/54310)