Javascript Node.js中的php openssl_seal等效项

Javascript Node.js中的php openssl_seal等效项,javascript,node.js,openssl,cryptojs,Javascript,Node.js,Openssl,Cryptojs,我有一个php代码片段,我想把它移到node.js中,但我似乎找不到正确的方法 类加密服务 { const PUBLIC_CERT_PATH='CERT/PUBLIC.cer'; const PRIVATE_CERT_PATH='CERT/PRIVATE.key'; 常量错误\u加载\u X509\u证书=0x10000001; 常量错误加密数据=0x10000002; public$outEncData=null; public$outEnvKey=null; 公共数据; 公共函数encryp

我有一个php代码片段,我想把它移到node.js中,但我似乎找不到正确的方法

类加密服务
{
const PUBLIC_CERT_PATH='CERT/PUBLIC.cer';
const PRIVATE_CERT_PATH='CERT/PRIVATE.key';
常量错误\u加载\u X509\u证书=0x10000001;
常量错误加密数据=0x10000002;
public$outEncData=null;
public$outEnvKey=null;
公共数据;
公共函数encrypt()
{
$publicKey=openssl\u pkey\u get\u public(self::public\u CERT\u PATH);
如果($publicKey==false){
$publicKey=openssl\u pkey\u get\u public(“文件:/”.self::public\u证书路径);
}
如果($publicKey==false){
$errorMessage=“加载X509公钥证书时出错!原因:”;
而($errorString=openssl\u error\u string()){
$errorMessage.=$errorString.“\n”;
}
抛出新异常($errorMessage,self::ERROR\u LOAD\u X509\u证书);
}
$publicKeys=数组($publicKey);
$encData=null;
$envKeys=null;
$result=openssl\u seal($this->srcData,$encData,$envKeys,$publicKeys);
如果($result==false)
{
$this->outEncData=null;
$this->outEnvKey=null;
$errorMessage=“加密数据时出错!原因:”;
而($errorString=openssl\u error\u string())
{
$errorMessage.=$errorString.“\n”;
}
抛出新异常($errorMessage,self::ERROR\u ENCRYPT\u DATA);
}
$this->outEncData=base64\u encode($encData);
$this->outEnvKey=base64_encode($envKeys[0]);
}
};
问题是我在任何地方都找不到Javascript中的实现。我确实需要保留此结构,因为我同时使用
outEncData
outEnvKey

我设法用
crypto
包找到了与
openssl\u-sign
等效的
openssl\u-seal
实现

乐补充了工作方案作为答案


好的,我已经花了一些时间来弄清楚这一点,简言之,它现在在回购协议中:。但我们要遵守这些规则

下面假设您拥有用于对生成的密钥进行签名的公钥和用于测试是否一切正常的私钥

  • 用于加密的随机生成的密钥:
  • const crypto=require('crypto');
    const generaterandomkeysync=async()=>{
    返回新承诺((解决、拒绝)=>{
    crypto.scrypt(“密码”,“salt”,24,(err,derivedKey)=>{
    如果(错误)拒绝(错误);
    解析(derivedKey.toString('hex');
    });
    });
    }
    
  • 使用生成的密钥加密数据,然后使用给定的公钥加密该密钥。我们希望返回加密的详细信息和加密的密钥,因为我们希望另一端的用户拥有私钥
  • const crypto=require('crypto');
    const path=require('path');
    常数fs=要求('fs');
    const encryptKeyWithPubAsync=async(文本)=>{
    返回新承诺((解决)=>{
    fs.readFile(path.resolve('./public_key.pem'),'utf8',(err,publicKey)=>{
    如果(错误)抛出错误;
    const buffer=buffer.from(文本“utf8”);
    const encrypted=crypto.publicEncrypt(公钥,缓冲区);
    解析(加密的.toString('base64'));
    });
    });
    }
    常量encryptStringAsync=async(明文)=>{
    const encryptionKey=await generateRandomKeyAsync();
    const cipher=await crypto.createCipheriv(“RC4”,encryptionKey,null);
    const encryptedKey=等待encryptKeyWithPubAsync(encryptionKey);
    返回新承诺((解决、拒绝)=>{
    让encryptedData='';
    cipher.on('readable',()=>{
    让块;
    while(null!=(chunk=cipher.read()){
    encryptedData+=chunk.toString('hex');
    }
    });
    cipher.on('end',()=>{
    解析([encryptedKey,encryptedData]);//返回值
    });
    密码。写(明文);
    cipher.end();
    });
    }
    
  • 现在我们可以加密细节了:
  • encryptStringAsync(“foo-bar-baz”)
    。然后(详细信息=>{
    log(`encrypted val${details[1]},encrypted key${details[0]}`);
    })
    
    将打印如下内容:

    加密foo-bar-baz
    加密val B4C6C7A7971224FBE35D4,加密密钥4.我的研究成果是一个关于BKBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBXLGWIY9EL9PDHTM5JTSVW==
    
  • 要测试上述假设,首先需要使用私钥解密密钥:
  • const decryptKeyWithPrivateAncy=async(encryptedKey)=>{
    返回新承诺((解决)=>{
    fs.readFile(path.resolve('./private_key.pem'),'utf8',(err,privateKey)=>{
    如果(错误)抛出错误;
    const buffer=buffer.from(encryptedKey'base64')
    const decrypted=crypto.privateDecrypt({
    key:privateKey.toString(),
    密码短语:“”,
    },缓冲区);
    解析(解密.toString('utf8'));
    });
    });
    }
    
  • 密钥解密后,可以对消息进行解密:
  • const decryptedWithEncryptedKey=async(encKey,encVal)=>{
    const k=等待解密密钥WithPrivateAync(encKey);
    const decipher=等待crypto.createDecipheriv(“RC4”,k,null);
    返回新承诺((解决、拒绝)=>{
    让解密=“”;
    破译.on('readable',()=>{
    while(null!==(chunk=decipher.read()){
    解密+=chunk.toString('utf8');
    }
    });