Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js crypto.privateDecrypt在windows上工作,但在ubuntu中不工作_Node.js_Windows_Ubuntu 16.04_Cryptojs - Fatal编程技术网

Node.js crypto.privateDecrypt在windows上工作,但在ubuntu中不工作

Node.js crypto.privateDecrypt在windows上工作,但在ubuntu中不工作,node.js,windows,ubuntu-16.04,cryptojs,Node.js,Windows,Ubuntu 16.04,Cryptojs,我有一个读取私钥(PEM格式)的代码和我想要解密的加密文本。当我在windows中运行代码时,一切正常,它通过 let buffer = Buffer.from(encryptedData,'base64'); crypto.privateDecrypt(privatekey, buffer); 但是,当我在Ubuntu中运行相同的代码时,我收到以下错误: "'Passphrase required for encrypted key.TypeError: Passphrase ' + '

我有一个读取私钥(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),它们是相同的。在privatekey中我也没有使用密码短语。 有人知道为什么在Ubuntu中我会遇到这样的错误,而在windows中它工作得很好吗?这可能与privatekey格式有关,实际上是.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: ''
    }
  });
当我在互联网上查看更多信息时,我在 私钥的头是“----BEGIN RSA private key-----”,实际上我可以在Ubuntu中运行该代码,并且不会发生错误。因此,问题似乎与由于privateKeyEncoding中的“pkcs8”类型而更改的头有关。 我还注意到我在windows上的节点版本是10.15.3,在ubuntu上是12.4
这也可能与节点版本的更改有关吗?

我在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(跳过生成密钥文件步骤)。这对我也适用。

谢谢Terry。我刚刚将Ubuntu中安装的节点更改为LTS版本,因此目前无法测试它。