The private key I generated in Node.js is not recognized as PEM format in Go

WBOY
Release: 2024-02-13 19:09:08
forward
463 people have browsed it

我在 Node.js 中生成的私钥在 Go 中不被识别为 PEM 格式

php editor Xigua encountered a problem during the recent development process. He found that the private key generated using Node.js could not be recognized as PEM format in Go. This problem troubled him for a long time, and he tried various methods to solve it, but without success. In this article, we will explore the causes of this problem and possible solutions to help readers solve similar problems.

Question content

I generated the public and private keys in node.js using the encryption library and the following code.

function generatekeyfiles() {
  const keypair = crypto.generatekeypairsync("rsa", {
    moduluslength: 4096,
    publickeyencoding: {
      type: "spki",
      format: "pem",
    },
    privatekeyencoding: {
      type: "pkcs8",
      format: "pem",
      cipher: "aes-256-cbc",
      passphrase: "",
    },
  });
  // writing the keys in the following files
  fs.writefilesync("public_key", keypair.publickey);
  fs.writefilesync("private_key", keypair.privatekey);
}
Copy after login

I know the keys are working because I've used them to encrypt and decrypt data. But I try to use them in go and it cannot detect the private key in pem format. However, it does recognize the public key. Here is my go code snippet:

// Load public key from the "public_key" file generated by Node.js
publicKeyData, err := ioutil.ReadFile("public_key")
if err != nil {
fmt.Println("Error reading the public key file:", err)
return
}

// Load public key in PEM format
block, _ := pem.Decode(publicKeyData)
if block == nil || block.Type != "PUBLIC KEY" {
fmt.Println("The public key file is not in PEM format")
return
}
publicKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
fmt.Println("Error loading the public key:", err)
return
}

// Successfully loaded the public key in Go
fmt.Println("Public key loaded successfully:", publicKey)

// Load private key from the "private_key" file generated by Node.js
privateKeyData, err := ioutil.ReadFile("private_key")
if err != nil {
fmt.Println("Error reading the private key file:", err)
return
}

// Load private key in PEM format
block, _ = pem.Decode(privateKeyData)
if block == nil || block.Type != "PRIVATE KEY" {
fmt.Println("The private key file is not in PEM format")
return
}
Copy after login

Please, I need help. I don't understand why when I use public and private keys for encryption in my other node.js program, it reads the public key but not the private key. It says "private key file is not in pem format" but that doesn't make any sense.

I tried generating a new key but the exact same problem persists.

Workaround

I finally solved this problem by using the OpenSSL library on Windows cmd to generate the key. I then encrypted and decrypted the data using the keys generated by OpenSSL. I had to sanitize the decrypted data in go, but it finally worked.

The above is the detailed content of The private key I generated in Node.js is not recognized as PEM format in Go. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!