微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

错误:错误:0909006C:PEM例程:get_name:无起始行,原因:“无起始行”,代码:“ ERR_OSSL_PEM_NO_START_LINE”

如何解决错误:错误:0909006C:PEM例程:get_name:无起始行,原因:“无起始行”,代码:“ ERR_OSSL_PEM_NO_START_LINE”

const rsaWrapper = require('./rsa-wrapper');
// generate opened and closed keys for browser and server
rsaWrapper.generate('server');
rsaWrapper.generate('client');
console.log('Keys generated …');
<!doctype html>
<html>

<head>
  <title>Encryption/decryption RSA/AES</title>
  <Meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  <link rel="stylesheet" type="text/css" href="/css/style.css" />
</head>

<body>
  <div class="container">
    <div class="text-container">
      <h2>Server public key</h2>
      <textarea id="server_public"></textarea>
    </div>
    <div class="text-container">
      <h2>Client public key</h2>
      <textarea id="client_public"></textarea>
    </div>
    <div class="text-container">
      <h2>Client private key</h2>
      <textarea id="client_private"></textarea>
    </div>
    <div style="clear: both"></div>
  </div>
  <div class="log-container">
    <h1>Log container</h1>
    <div id="log" style="padding-top: 20px"></div>
  </div>
  <script src="/socket.io/socket.io.js"> </script> <script>
    var socket = io();
  </script>
</body>

</html>

我遇到错误,请帮助我

1

const express = require("express");
const app = express();
const rsaWrapper = require("./components/rsa-wrapper");
rsaWrapper.initLoadServerKeys(__dirname);
rsaWrapper.serverExampleEncrypt();

const http = require("http").Server(app);
const io = require("socket.io")(http);

// middleware for static processing
app.use(express.static(__dirname + "/static"));
// web socket connection event
io.on("connection",function (socket) {
  console.log("connection");
});
http.listen(3000,function () {
  console.log("listening on *:3000");
});
const path = require("path");
const rsaWrapper = {};
const fs = require("fs");
const NodeRSA = require("node-rsa");
const crypto = require("crypto");
// open and closed keys generation method
rsaWrapper.generate = (direction) => {
  let key = new NodeRSA();
  // 2048 — key length,65537 open exponent
  key.generateKeyPair(2048,65537);
  //save keys as pem line in pkcs8
  fs.writeFileSync(
    path.resolve(__dirname,"../keys",direction + ".private.pem"),key.exportKey("pkcs8-private-pem")
  );
  fs.writeFileSync(
    path.resolve(__dirname,direction + ".public.pem"),key.exportKey("pkcs8-public-pem")
  );
  return true;
};
// encrypting RSA,using padding OAEP,with nodejs crypto:
rsaWrapper.encrypt = (publicKey,message) => {
  let enc = crypto.publicEncrypt({
      key: publicKey,padding: crypto.RSA_PKCS1_OAEP_PADDING,},Buffer.from(message)
  );
  return enc.toString("base64");
};
// descrypting RSA,with nodejs crypto:
rsaWrapper.decrypt = (privateKey,message) => {
  let enc = crypto.privateDecrypt({
      key: privateKey,Buffer.from(message,"base64")
  );
  return enc.toString();
};
// Loading RSA keys from files to variables:
rsaWrapper.initLoadServerKeys = (basePath) => {
  rsaWrapper.serverPub = fs.readFileSync(
    path.resolve(basePath,"keys","server.public.pem")
  );
  rsaWrapper.serverPrivate = fs.readFileSync(
    path.resolve(basePath,"server.private.pem")
  );
  rsaWrapper.clientPub = fs.readFileSync(
    path.resolve(basePath,"client.public.pem")
  );
};
// Run RSA encryption test scenario. Message is encrypted,log on console in base64 format and message is decrypted and log on console.
rsaWrapper.serverExampleEncrypt = () => {
  console.log("Server public encrypting");
  let enc = rsaWrapper.encrypt(rsaWrapper.serverPub,"Server init hello");
  console.log("Server private encrypting …");
  console.log("Encrypted RSA string ","\n",enc);
  let dec = rsaWrapper.decrypt(rsaWrapper.serverPrivate,enc);
  console.log("Decrypted RSA string …");
  console.log(dec);
};
module.exports = rsaWrapper;
Error: error:0909006C:PEM routines:get_name:no start line
at Object.rsaWrapper.encrypt (E:\Web Site\NodeJS\Back end website\encryption\components\rsa-wrapper.js:24:20)
at Object.rsaWrapper.serverExampleEncrypt (E:\Web Site\NodeJS\Back end website\encryption\components\rsa-wrapper.js:59:24)
at Object.<anonymous> (E:\Web Site\NodeJS\Back end website\encryption\index.js:5:12)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。