【问题标题】:crypto.privateDecrypt works on windows but not in ubuntucrypto.privateDecrypt 适用于 Windows 但不适用于 ubuntu
【发布时间】:2019-06-12 05:41:02
【问题描述】:

我有一个读取 privateKey(PEM 格式)的代码和我想要解密它的加密文本。当我在 Windows 中运行代码时,一切正常,它通过

解密文本
let buffer = Buffer.from(encryptedData,'base64');
crypto.privateDecrypt(privatekey, buffer);

但是,当我在 Ubuntu 中运行相同的代码时,我收到以下错误:

"'Passphrase required for encrypted key.TypeError: Passphrase ' +
  'required for encrypted key    at Object.privateDecrypt ' +
  '(internal/crypto/cipher.js:53:12)    at e1c2 ' +...

"

我在 windows 和 Ubuntu 系统中都使用了 console.log(encryptedData,privatekey,buffer),它们是相同的。同样在私钥中,我没有使用密码。 任何人都知道为什么在 Ubuntu 中我得到这样的错误,而在 Windows 中它工作正常?它是否与实际上是 .pam 格式的私钥格式有关,并且由于空格 linux 无法处理它?

编辑:

我的私钥有这种格式

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIJrTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIdq79fP1MZogCAggA
MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEqBBDMX/L46fPKcSQkgXrwpLtqBIIJ
..

我基本上是通过

生成的
crypto.generateKeyPairSync('rsa', {
    modulusLength: 4096,
    publicKeyEncoding: {
      type: 'spki',
      format: 'pem'
    },
    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem',
      cipher: 'aes-256-cbc',
      passphrase: ''
    }
  });

当我在互联网上查看更多信息时,我在enter link description here 看到了一个示例 私钥的标题是“-----BEGIN RSA PRIVATE KEY-----”,实际上我可以在 Ubuntu 中运行该代码并且不会发生错误。所以看起来问题与由于privateKeyEncoding中的'pkcs8'类型而改变的标题有关。 我还注意到我在 windows 上的节点版本是 10.15.3 而在 ubuntu 上是 12.4 会不会也和节点版本变化有关?

【问题讨论】:

    标签: node.js windows ubuntu-16.04 cryptojs


    【解决方案1】:

    我在 Windows 10 和 Ubuntu 18.04 上都尝试过非常相似的东西。就我而言,我生成公钥/私钥文件,使用这些文件加密然后解密。它在两个平台上都运行良好。值得一试,看看它是否适合你。节点版本为:Windows:10.15 Ubuntu 10.16。

    const crypto = require('crypto');
    const fs = require("fs");
    
    function generateKeyFiles() {
    
        const keyPair = crypto.generateKeyPairSync('rsa', {
            modulusLength: 4096,
            publicKeyEncoding: {
                type: 'spki',
                format: 'pem'
            },
            privateKeyEncoding: {
            type: 'pkcs8',
            format: 'pem',
            cipher: 'aes-256-cbc',
            passphrase: ''
            }
        });
    
        fs.writeFileSync("public_key", keyPair.publicKey);
        fs.writeFileSync("private_key", keyPair.privateKey);
    }
    
    // Encrypt a string given a public key file. Encode the result in base64.
    function encryptString (plaintext, publicKeyFile) {
        const publicKey = fs.readFileSync(publicKeyFile, "utf8");
        const encrypted = crypto.publicEncrypt(publicKey, Buffer.from(plaintext));
        return encrypted.toString("base64");
    }
    
    // Encrypt a string given a cipherText (in base64) and a private key file.
    function decryptString (ciphertext, privateKeyFile) {
        const privateKey = fs.readFileSync(privateKeyFile, "utf8");
        const decrypted = crypto.privateDecrypt(privateKey, Buffer.from(ciphertext, "base64"));
        return decrypted.toString("utf8");
    }
    
    generateKeyFiles();
    
    const plainText = "I have spread my dreams under your feet. Tread softly because you tread on my dreams.";
    
    const cipherText = encryptString(plainText, "./public_key");
    
    console.log();
    console.log("Plaintext:", plainText);
    console.log();
    console.log("Ciphertext: ", cipherText);
    console.log();
    console.log("Decrypted Text: ", decryptString(cipherText, "private_key"));
    console.log();
    

    您也可以尝试将密钥文件从 Windows 复制到 Ubuntu(跳过生成密钥文件步骤)。这对我也有用。

    【讨论】:

    • 谢谢特里。我只是将 Ubuntu 中安装的节点更改为 LTS 版本,所以我暂时无法测试它。
    猜你喜欢
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2015-01-12
    • 2020-06-26
    • 2019-12-05
    相关资源
    最近更新 更多