dousigan0499
2019-02-02 14:40
浏览 437
已采纳

在Nodejs中加密并在PHP中解密

I'm trying to decrypt a string in PHP which was originally encrypted in NodeJS.

PHP:

openssl_decrypt($raw_id, "aes-128-cbc", "s7UbmJpfm56r6CAC6mz7KVZdRc3Fxc4m", 0, null)

This seems to always returnfalse.

Encryption in Nodejs:

function encrypt(text) {
    var cipher = crypto.createCipher('aes-128-cbc', 's7UbmJpfm56r6CAC6mz7KVZdRc3Fxc4m');
    var encrypted = cipher.update(text, 'utf8', 'hex')
    encrypted  += cipher.final('hex')

    return encrypted; 
}

function decrypt(text) {
    var cipher = crypto.createDecipher('aes-128-cbc', 's7UbmJpfm56r6CAC6mz7KVZdRc3Fxc4m');
    var decrypted = cipher.update(text, 'hex', 'utf8')
    decrypted += cipher.final('utf8');

    return decrypted;
}

I basically want to encrypt, for example, encrypt("Pizza") in Nodejs, send it to the PHP page (3879f91a59e9a458db62f905b0a488a1), and decrypt it from there (openssl_decrypt: return Pizza).

I know this code is not safe, since I'm not using an IV but I'm not sure how to add one.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试解密最初在NodeJS中加密的PHP字符串。</ p> \ ñ

PHP:</ p>

<代码> openssl_decrypt($ raw_id, “AES-128-CBC”, “s7UbmJpfm56r6CAC6mz7KVZdRc3Fxc4m”,0,NULL)</代码> </ p> \ n

这似乎总是返回 false </ code>。</ p>

Nodejs中的加密:</ p>

  function 加密(文本){
变种密码= crypto.createCipher( 'AES-128-CBC', 's7UbmJpfm56r6CAC6mz7KVZdRc3Fxc4m'); 
变种加密= cipher.update(文字, 'UTF8', '六角')
加密+  = cipher.final('hex')
 
返回加密;  
 
 
功能解密(文本){
 var cipher = crypto.createDecipher('aes-128-cbc','s7UbmJpfm56r6CAC6mz7KVZdRc3Fxc4m'); 
 var decrypted = cipher.update(text,'hex','  utf8')
解密+ = cipher.final('utf8'); 
 
返回解密; 
} 
 </ code> </ pre> 
 
 

我基本上想要加密, 例如,<代码>加密( “比萨饼”)</代码>在的NodeJS,将其发送到PHP页面(<码> 3879f91a59e9a458db62f905b0a488a1 </代码>),并从那里解密它(<码> openssl_decrypt:返回披萨< /code>).

nn

我知道这段代码不安全,因为我没有使用IV,但我不知道如何添加。</ i> </ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题