如何解密加密的配置值?


12
protected $_paymentData;
protected $_scopeConfig;
protected $logger;

public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
    \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
    \Magento\Payment\Helper\Data $paymentData,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Payment\Model\Method\Logger $logger,
    \Magento\Framework\Module\ModuleListInterface $moduleList,
    \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
    \Magento\Directory\Model\CountryFactory $countryFactory,
    \Stripe\Stripe $stripe,
    \Inchoo\Stripe\Model\StripeFactory $stripeFactory,
    array $data = array()
) {
    parent::__construct(
        $context,
        $registry,
        $extensionFactory,
        $customAttributeFactory,
        $paymentData,
        $scopeConfig,
        $logger,
        $moduleList,
        $localeDate,
        null,
        null,
        $data
    );
    $this->_scopeConfig = $scopeConfig;
    $this->logger = $logger;
    $this->initializeData($data);
}
 public function getPaymentKey(){
   $key= $this->_scopeConfig->getValue('payment/webpay/keyid');
    echo $key;
    exit; 
}

回声结果:idfrk3-45pfnrkhwneirgplbmisniepssnie:hirtw45 True Key-'p92GBhcQl7TklHOsWcxBk4eOmL6wpQWBG9nT2Qcf'

Answers:


26

终于获得成功的解密代码...

protected $_encryptor;

public function __construct(
    \Magento\Framework\Encryption\EncryptorInterface $encryptor,
) {
    $this->_encryptor = $encryptor;
    parent::__construct($context);
}
$test = 'dfrk3-45pfnrkhwneirgplbmisniepssnie';
$test = $this->_encryptor->decrypt($test);
echo $test;

分享和帮助他人...


它返回空白值。如何获得可读格式的输出?
— Emipro Technologies Pvt。Ltd.

您可以共享发行代码吗?
— Magento2 Devloper

20

\Magento\Framework\App\Config\ScopeConfigInterface::getValue将返回解密后的值。当ScopeConfigInterface::getValue返回的加密值,所述配置选项是设置不正确。加密配置值的正确实现是:

供应商/模块/etc/adminhtml/system.xml

在这里,我们添加路径一个不起眼的配置价值payment/webpay/keyid的关键的东西在这里是确保field拥有type="obscure"和使用Magento\Config\Model\Config\Backend\Encrypted的backend_model。这就是Magento知道如何使用带掩码的表单字段并在保存时对任何用户输入进行加密的方式。

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="payment">
            <group id="webpay">
                <field id="keyid" translate="label" type="obscure" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Key Id</label>
                    <backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
                </field>
            </group>
        </section>
    </system>
</config>

供应商/模块/etc/config.xml

在backend_model="Magento\Config\Model\Config\Backend\Encrypted"此处添加内容可告知Magento使用以下命令检索时应解密配置值ScopeConfigInterface::getValue

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <payment>
            <webpay>
                <keyid backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
            </webpay>
        </payment>
    </default>
</config>

供应商/模块/etc/di.xml

这会将配置路径添加到敏感阵列,并防止在转储存储配置时包括路径的值。

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Config\Model\Config\TypePool">
        <arguments>
            <argument name="sensitive" xsi:type="array">
                <item name="payment/webpay/keyid" xsi:type="string">1</item>
            </argument>
        </arguments>
    </type>
</config>

在某些情况下,这似乎行不通,在我看来,这是行得通的,而在我将该字段移动到包含的组xml配置中之后,它就坏了。以上建议已实施,但未起作用
— snez

@snez在移动后是否尝试过重新保存配置?
— 罗曼·斯尼科

5

如果您安装了n98-magerun2.phar,则可以使用以下内容获取解密的配置值:

php bin/n98-magerun2.phar config:store:get --decrypt payment/webpay/keyid

您还可以从命令行使用以下方式设置加密的配置值:

php bin/n98-magerun2.phar config:store:set --encrypt payment/webpay/keyid NEW_KEY_ID_VALUE_HERE

您可以从这里获取n98-magerun2.phar:https : //github.com/netz98/n98-magerun2


2
n98是不是最伟大的事情?
— 威廉·特兰

这也适用于n98-magerun(适用于Magento 1)
— CCBlackburn

0

You can try with below method for payment encryption method to get value,

您必须\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,用下面的类路径替换,\Magento\Payment\Gateway\ConfigInterface 这很好用,

   <?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Module\Gateway\Http;

use Magento\Payment\Gateway\Http\TransferBuilder;
use Magento\Payment\Gateway\Http\TransferFactoryInterface;
use Magento\Payment\Gateway\Http\TransferInterface;
use Magento\Payment\Gateway\ConfigInterface;

class TransferFactory implements TransferFactoryInterface
{
    private $config;

    private $transferBuilder;

    public function __construct(
        ConfigInterface $config,
        TransferBuilder $transferBuilder
    ) {
        $this->config = $config;
        $this->transferBuilder = $transferBuilder;
    }


    public function getPaymentKey()
    {
        echo $this->config->getValue('payment/webpay/keyid')
    }
}

这适合您吗?
— Rakesh Jesadiya '16

致命错误:无法在第73行上实例化E:\ wamp \ www \ magento2_8 \ vendor \ magento \ framework \ ObjectManager \ Factory \ Dynamic \ Developer.php中的接口Magento \ Payment \ Gateway \ ConfigInterface,位于第73行
— Magento2 Devloper

你解决问题了吗?
— Rakesh Jesadiya'7

不会出现致命错误:无法在第73行错误中实例化E:\ wamp \ www \ magento2_8 \ vendor \ magento \ framework \ ObjectManager \ Factory \ Dynamic \ De‌veloper.php中的接口Magento \ Payment \ Gateway \ ConfigInterface。
— Magento2 Devloper '16

尝试使用上述更新的代码并删除var文件夹。
— Rakesh Jesadiya'7

0

如果您想使用某些密钥来解密某些值,请执行以下操作:将以下代码放入magento项目根目录中的crypto-config-value.php中。

<?php

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');


######################################################################################################################

/**
 * @var \Magento\Framework\Encryption\EncryptorInterfaceFactory $ef
 */
$ef = $obj->get('Magento\Framework\Encryption\EncryptorInterfaceFactory');

class CustomDeploymentConfig extends \Magento\Framework\App\DeploymentConfig {
    public function get($key = null, $defaultValue = null)
    {
        return '8343d1c27ee612c73131c0ec693ed86e';
    }
}

/**
 * @var CustomDeploymentConfig $d
 */
$d = $obj->get(CustomDeploymentConfig::class);

/**
 * @var \Magento\Framework\Encryption\EncryptorInterface $e
 */
$e = $ef->create(['deploymentConfig' => $d]);

echo ">>>", $e->decrypt('encripted-value-here'), "<<<\n";

使用控制台运行php crypto-config-value.php或使用浏览器运行//yourwebsite.com/decrypt-config-value.php。


-1

尝试使用以下代码获取json解码值,

class Paymentmodule
{
    protected $jsonEncoder;
    protected $jsonDecoder;

    public function __construct(
        ..//
        \Magento\Framework\Json\DecoderInterface $jsonDecoder
    ) {
        ..//
        $this->jsonDecoder = $jsonDecoder;
    }

    public function getPaymentKey()
    {
        $key= $this->_scopeConfig->getValue('payment/webpay/keyid');
        $config = $this->jsonDecoder->decode($key);
        echo $key;
    }

}

1
解码失败:语法错误“; i:1; s:10720:”#0 E:\ wamp \ www \ magento2_8 \ vendor \ magento \ framework \ Json \ Decoder.php(20):Zend_Json :: decode('0: 2:234SyEIM4aj ...')#1 E:\ wamp \ www \ magento2_8 \ vendor \ magento \ module-checkout \ Controller \ Onepage \ Success.php(58):Magento \ Framework \ Json \ Decoder-> decode(' 0:2:234SyEIM4aj ...')
— Magento2 Devloper

对这个错误有任何想法吗?
— Magento2 Devloper '16

我对此一无所知,因为这是用于付款方式的,我已经为上述方法进行了简单的查询
— Rakesh Jesadiya

我认为语法错误定义了另一种类型。
— Magento2 Devloper '16

在简单查询中工作正常?
— Magento2 Devloper '16
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.