本文介绍了Electronjs和Nodejs(Crypto)crypto.scryptSync不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台运行Crypto的服务器,它工作正常.我使用electronjs作为客户端,并假定将加密内置到节点中.当我尝试使用该模块时,它返回"crypto.scryptSync不是函数"

我有

  let crypto = require('crypto'); 

在页面顶部

我只是做一个简单的电话

Encrypt_AES

  function Encrypt_AES(data,pubkey){const algorithm ='aes-192-cbc';const key = crypto.scryptSync(pubkey,'salt',24);const iv = Buffer.alloc(16,0);//初始化向量.const cipher = crypto.createCipheriv(算法,密钥,iv);让加密= cipher.update(JSON.stringify(data),'utf8','hex');加密+ = cipher.final('hex');返回加密} 

Decrypt_AES

 函数Decrypt_AES(data,pubkey){const algorithm ='aes-192-cbc';const key = crypto.scryptSync(pubkey,'salt',24);const iv = Buffer.alloc(16,0);//初始化向量.const decipher = crypto.createDecipheriv(algorithm,key,iv);让解密= decipher.update(data,'hex','utf8');解密+ = decipher.final('utf8');返回JSON.parse(decrypted);} 

我不明白它是如何无法吸收电子中的模块的,我从来没有遇到过任何其他模块的问题.

我尝试了一次npm-我加密了

npm WARN不建议使用crypto@1.0.1:不再支持此程序包.现在,它是内置的Node模块.如果您依赖加密,则应切换到内置密码.npm通知创建了一个作为package-lock.json的锁文件.您应该提交此文件.+加密@ 1.0.1在4.95秒内添加了1个程序包并审核了724个程序包找到了0个漏洞

解决方案

问题

当Electron支持的Node版本早于本地Node安装时,在使用Electron时未定义Node函数或不使用特定参数,即使它在本地Node安装中工作正常也可能发生.

找出您使用的版本

一个好的开始是查看将相关功能添加到Node的时间.您可以通过查看

在这里我们看到在节点10.5.0中添加了 scryptSync .

如果您不知道Electron安装支持的Node版本,则可以使用Electron运行以下命令:

console.log(process.versions)

您应该在输出中找到使用的Node版本.例如对于Electron 3.1.4,它应如下所示:

  {http_parser:'2.8.0',节点:"10.2.0",v8:"6.6.346.32",uv:"1.20.3",zlib:"1.2.11",战神:"1.14.0",模块:"64",nghttp2:"1.29.0",napi:"3",openssl:'1.1.0h',电子:"3.1.4",chrome:'66 .0.3359.181'} 

为比较起见,这是我的Electron 4.0.2安装的输出:

  {http_parser:'2.8.0',节点:"10.11.0",v8:"6.9.427.24-electron.0",uv:"1.23.0",zlib:"1.2.11",战神:"1.14.0",模块:"64",nghttp2:'1.33.0',napi:"3",openssl:'1.1.0',电子:'4.0.2',chrome:"69.0.3497.106",icu:​​"62.1",unicode:"11.0",cldr:"33.1",tz:'2018e'} 

我们看到电子3使用节点10.2.0.因此,在尚未使用Electron使用 scryptSync 的节点中,因此尚未实现 undefined .

如何解决这个问题

如果已经发布了支持所需节点版本的较新电子版本,则可以解决此问题.

您可以在

因此,升级到最新的Electron版本应该可以解决此问题.

Hi So I have a server running Crypto it working perfectly. Im using electronjs as a client side and crypto is suppose to be build into node. when I try use the module it returns "crypto.scryptSync is not a function"

I have

 let crypto = require('crypto');

at the top of the page

im just doing a simple call like this

Encrypt_AES

function Encrypt_AES(data, pubkey) {
 const algorithm = 'aes-192-cbc';
 const key = crypto.scryptSync(pubkey, 'salt', 24);
 const iv = Buffer.alloc(16, 0); // Initialization vector.
 const cipher = crypto.createCipheriv(algorithm, key, iv);
 let encrypted = cipher.update(JSON.stringify(data), 'utf8', 'hex');
 encrypted += cipher.final('hex');
 return encrypted;
}

Decrypt_AES

function Decrypt_AES(data, pubkey) {
 const algorithm = 'aes-192-cbc';
 const key = crypto.scryptSync(pubkey, 'salt', 24);
 const iv = Buffer.alloc(16, 0); // Initialization vector.
 const decipher = crypto.createDecipheriv(algorithm, key, iv);
 let decrypted = decipher.update(data, 'hex', 'utf8');
 decrypted += decipher.final('utf8');
 return JSON.parse(decrypted);
}

I dont understand how it cannot pick up the module in electron I've never had this problem with any other module.

I tried a npm - i crypto

npm WARN deprecated crypto@1.0.1: This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in.npm notice created a lockfile as package-lock.json. You should commit this file.+ crypto@1.0.1added 1 package and audited 724 packages in 4.95sfound 0 vulnerabilities

解决方案

The Problem

That a Node function is not defined when using Electron or does not take a specific parameter even though it works fine with your local Node installation can happen, when the Node version that Electron supports is older than your local Node installation.

Find out the version you use

A good first start is to look when the function in question was added to Node. You can do that by looking in the Node.js documentation. You will find a History table right below the function name in a drop down block.

Here we see that scryptSync was added in Node 10.5.0.

If you don't know what Node version your Electron installation supports, you can run the following with Electron:

console.log(process.versions)

You should find the used Node version in the output. For example for Electron 3.1.4 it should look like this:

{ http_parser: '2.8.0',
  node: '10.2.0',
  v8: '6.6.346.32',
  uv: '1.20.3',
  zlib: '1.2.11',
  ares: '1.14.0',
  modules: '64',
  nghttp2: '1.29.0',
  napi: '3',
  openssl: '1.1.0h',
  electron: '3.1.4',
  chrome: '66.0.3359.181' }

For comparison here is the output of my Electron 4.0.2 installation:

{ http_parser: '2.8.0',
  node: '10.11.0',
  v8: '6.9.427.24-electron.0',
  uv: '1.23.0',
  zlib: '1.2.11',
  ares: '1.14.0',
  modules: '64',
  nghttp2: '1.33.0',
  napi: '3',
  openssl: '1.1.0',
  electron: '4.0.2',
  chrome: '69.0.3497.106',
  icu: '62.1',
  unicode: '11.0',
  cldr: '33.1',
  tz: '2018e' }

As we see Electron 3 uses Node 10.2.0. So in the Node that Electron uses scryptSync is not yet implemented and therefore undefined.

How to solve this

You can solve this if a newer Electron version that supports the Node version you need is already released.

You can look this up in the Electron release notes. Up until now Node version upgrades were implemented in major versions so looking at the next x.0.0 version is a good start.

In this case we are in luck: Electron 4 supports Node 10.11.0 which implements scryptSync.

So upgrading to the latest Electron version should solve this problem.

这篇关于Electronjs和Nodejs(Crypto)crypto.scryptSync不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 20:24