【问题标题】:Is it possible to generate RSA keys from a string?是否可以从字符串生成 RSA 密钥?
【发布时间】:2021-07-07 02:43:54
【问题描述】:

我正在使用 node-rsa 包通过 RSA 函数进行非对称加密。

目前,我正在生成我的公钥和私钥,如下所示:

generateKeys = function() {
  const key = new NodeRSA({ b: 1024 });
  return {
    public: key.exportKey('public'),
    private: key.exportKey('private'),
  }
}

是否有任何可能的方法可以从给定的字符串生成密钥?我想这样做,以便我的用户可以轻松写下他们的私钥,这对我的项目很重要。毕竟,写下 1024 个字符长的私钥几乎是不可能的。

我希望是这样的:

const key = new NodeRSA({ b: 1024 }, "Secret goes here")

我认为这是可能的,因为 sha256 函数可以接受任何要散列的字符串。我知道 RSA 加密并不是真正的散列函数,所以我不确定是否可以实现相同的效果。

感谢任何帮助!

【问题讨论】:

    标签: node.js encryption rsa


    【解决方案1】:

    似乎有一种简单的方法可以做到这一点。有一个包叫cryptico 那可以做的事情。

    首先安装包:

    npm i cryptico
    

    示例

    const cryptico = require('cryptico');
    const PassPhrase = "The Moon is a Harsh Mistress."; 
    
    // The length of the RSA key, in bits.
    const Bits = 1024; 
    
    const RSAkey = cryptico.generateRSAKey(PassPhrase, Bits);
    
    const PublicKeyString = cryptico.publicKeyString(RSAkey); 
    

    可以找到更好更完整的例子here

    更新

    因此,如果您需要ASYMMETRIC 加密(据我所知,这适用于一对称为publickeyprivatekey 的密钥),您可以简单地使用纯Node.js 实现。

    示例

    const { generateKeyPairSync, publicEncrypt, privateDecrypt } = require('crypto');
    
    const PassPhrase = "The Moon is a Harsh Mistress.";
    
    const Bits = 1024;
    
    const encryptWithRSA = (input, publickey) => {
        const buffer = Buffer.from(input, 'utf-8');
        const encrypted = publicEncrypt(publicKey, buffer);
        return encrypted.toString("base64");
    }
    
    const decryptWithRSA = function (input, privatekey) {
        const buffer = Buffer.from(input, 'base64');
        const decrypted = privateDecrypt(
            {
                key: privatekey,
                passphrase: PassPhrase,
            },
            buffer,
        )
        return decrypted.toString("utf8");
    };
    
    const { privateKey, publicKey } = generateKeyPairSync('rsa', {
        modulusLength: Bits,
        publicKeyEncoding: {
            type: 'spki',
            format: 'pem'
        },
        privateKeyEncoding: {
            type: 'pkcs8',
            format: 'pem',
            cipher: 'aes-256-cbc',
            passphrase: PassPhrase
        }
    });
    
    const encrypted = encryptWithRSA('yes i know', publicKey)
    console.log(encrypted);
    
    console.log(decryptWithRSA(encrypted, privateKey));
    

    输出(encrypted 值是随机的,如您所知)

    t3Gw+PlKn84gx2wkj99we345C6ZjIElpgDkzXjio2aWRwI28qTMev14H7o219P6Lw9STGnfK4FgTYO/cvLGlzplqRv6X5AgrstwsGQN88wmKHe5+6cxlpzPFNPWLUqtAvsIOPZe+ghaRGCkOT2ETUp4PiqwOTJ2EtmyVqQHt+f4=
    yes i know
    

    【讨论】:

    • 非常感谢!我意识到我在最初的问题中遗漏了一些东西:我正在做 ASYMMETRIC 加密。我的错,我应该指定的。 cryptico 是否支持非对称加密?浏览文档,它看起来不像,但如果是,请告诉我。
    • @Jacobjanak 更新了答案,我认为这可能会解决您的问题,PassPhrase 变量可以解决问题(它被用作privatekey 的密码)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2021-02-18
    相关资源
    最近更新 更多