在magento 2中加密和解密数据的正确方法是什么?

在magento 2中加密和解密数据的正确方法是什么?,magento,magento2,Magento,Magento2,我正在创建magento 2自定义模块,在这里我使用API调用获取一些数据。我需要将该数据存储在自定义表中。在存储之前,我需要加密这些数据。我认为有默认的加密功能。我使用了Mage::helper('Mage\u Core\u helper\u Data')->加密($value)。但是没有成功。您可以这样使用它: use Magento\Framework\Encryption\EncryptorInterface as Encryptor; 在构造函数中: $this->encryp

我正在创建magento 2自定义模块,在这里我使用API调用获取一些数据。我需要将该数据存储在自定义表中。在存储之前,我需要加密这些数据。我认为有默认的加密功能。我使用了
Mage::helper('Mage\u Core\u helper\u Data')->加密($value)
。但是没有成功。

您可以这样使用它:

use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
在构造函数中:

$this->encryptor = $encryptor;
然后调用encrypt函数进行加密:

$encrypt = $this->encryptor->encrypt($data);
以及解密:

$decrypt = $this->encryptor->decrypt($data);

您可以这样使用它:

use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
在构造函数中:

$this->encryptor = $encryptor;
然后调用encrypt函数进行加密:

$encrypt = $this->encryptor->encrypt($data);
以及解密:

$decrypt = $this->encryptor->decrypt($data);

默认Magento,开箱即用,具有使用EncryptorInterface类进行加密和解密的功能。 首先,我们需要在文件中定义EncryptorInterface类

use Magento\Framework\Encryption\EncryptorInterface;
之后,我们需要在类中声明一个变量

protected $encryptor;
现在我们必须创建类的构造函数

$this->encryptor = $encryptor;
之后,我们需要调用encrypt函数来加密给定的信息

$id='magecomp';
$encrypt = $this->encryptor->encrypt($id);
为了解密这些信息,我们只需要写下面的一行

$decrypt = $this->encryptor->decrypt($encrypt);
有关更多详细信息,请访问以下链接:


默认Magento,开箱即用,具有使用EncryptorInterface类进行加密和解密的功能。 首先,我们需要在文件中定义EncryptorInterface类

use Magento\Framework\Encryption\EncryptorInterface;
之后,我们需要在类中声明一个变量

protected $encryptor;
现在我们必须创建类的构造函数

$this->encryptor = $encryptor;
之后,我们需要调用encrypt函数来加密给定的信息

$id='magecomp';
$encrypt = $this->encryptor->encrypt($id);
为了解密这些信息,我们只需要写下面的一行

$decrypt = $this->encryptor->decrypt($encrypt);
有关更多详细信息,请访问以下链接:


这是为Magento 1还是为Magento 2准备的?示例代码是Magento 1,但您已在问题中标记了2。寻求调试帮助的问题(“此代码为什么不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现该问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。我的问题是如何在magento 2中调用函数encrypt()。@kushalmehta,你知道如何在magento 2中加密/解密数据吗?如果是,你可以共享一些东西。这是magento 1还是magento 2的?示例代码是Magento 1,但您已在问题中标记了2。寻求调试帮助的问题(“此代码为什么不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现该问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。我的问题是如何在magento 2中调用函数encrypt()。@kushalmehta,你知道如何在magento 2中加密/解密数据吗?如果是,你能分享一些东西吗。