nodejs解密Openssl通过PHP进行了加密的加密字符串
我有2个PHP函数来加密和解密消息/令牌,因此我更愿意尝试完成相同的解密函数,而不是加密功能,而是在node.js中进行加密函数。
$this->encryptMethod = 'aes-256-ctr';
$this->encryptKey = hex2bin('KEY123');
public function token_encrypt($message, $encode = false)
{
$nonceSize = openssl_cipher_iv_length($this->encryptMethod);
$nonce = openssl_random_pseudo_bytes($nonceSize);
$ciphertext = openssl_encrypt(
$message,
$this->encryptMethod,
$this->encryptKey,
OPENSSL_RAW_DATA,
$nonce
);
if ($encode) {
return base64_encode($nonce.$ciphertext);
}
return $nonce.$ciphertext;
}
public function token_decrypt($message, $encoded = false)
{
if ($encoded) {
$message = base64_decode($message, true);
if ($message === false) {
return false;
}
}
$nonceSize = openssl_cipher_iv_length($this->encryptMethod);
$nonce = mb_substr($message, 0, $nonceSize, '8bit');
$ciphertext = mb_substr($message, $nonceSize, null, '8bit');
$plaintext = openssl_decrypt(
$ciphertext,
$this->encryptMethod,
$this->encryptKey,
OPENSSL_RAW_DATA,
$nonce
);
return $plaintext;
}
我能够在node.js中获得这么远,而不会完全丢失
const hex2bin = s => s.match(/../g).map(c => String.fromCharCode(parseInt(c, 16))).join``;
function token_decrypt(message) {
var encryptionKey = Buffer.from(hex2bin("KEY123")).toString('base64');
var encryptionMethod = "aes-256-ctr";
var encrypted = Buffer.from(message, 'base64').toString();
const nonceSize = 16;
return false;
}
I have 2 PHP functions to encrypt and decrypt messages/tokens and I am more-so trying to accomplish the same decrypt function rather then the encrypt function but in Node.JS.
$this->encryptMethod = 'aes-256-ctr';
$this->encryptKey = hex2bin('KEY123');
public function token_encrypt($message, $encode = false)
{
$nonceSize = openssl_cipher_iv_length($this->encryptMethod);
$nonce = openssl_random_pseudo_bytes($nonceSize);
$ciphertext = openssl_encrypt(
$message,
$this->encryptMethod,
$this->encryptKey,
OPENSSL_RAW_DATA,
$nonce
);
if ($encode) {
return base64_encode($nonce.$ciphertext);
}
return $nonce.$ciphertext;
}
public function token_decrypt($message, $encoded = false)
{
if ($encoded) {
$message = base64_decode($message, true);
if ($message === false) {
return false;
}
}
$nonceSize = openssl_cipher_iv_length($this->encryptMethod);
$nonce = mb_substr($message, 0, $nonceSize, '8bit');
$ciphertext = mb_substr($message, $nonceSize, null, '8bit');
$plaintext = openssl_decrypt(
$ciphertext,
$this->encryptMethod,
$this->encryptKey,
OPENSSL_RAW_DATA,
$nonce
);
return $plaintext;
}
I was able to get this far in Node.JS without getting completely lost
const hex2bin = s => s.match(/../g).map(c => String.fromCharCode(parseInt(c, 16))).join``;
function token_decrypt(message) {
var encryptionKey = Buffer.from(hex2bin("KEY123")).toString('base64');
var encryptionMethod = "aes-256-ctr";
var encrypted = Buffer.from(message, 'base64').toString();
const nonceSize = 16;
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论