How can I decrypt an encrypted configuration value?
-
13-04-2021 - |
Question
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;
}
Echo Result : idfrk3-45pfnrkhwneirgplbmisniepssnie:hirtw45 True Key - 'p92GBhcQl7TklHOsWcxBk4eOmL6wpQWBG9nT2Qcf'
Solution
Finally Get Success In Decrypt code...
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;
Share and help others...
OTHER TIPS
\Magento\Framework\App\Config\ScopeConfigInterface::getValue
will return the decrypted value. When ScopeConfigInterface::getValue
returns an encrypted value, the configuration option is setup incorrectly. A correct implementation of an encrypted configuration value is:
Vendor/Module/etc/adminhtml/system.xml
Here we add an obscure configuration value in the path payment/webpay/keyid
the critical things here is ensuring the field
has type="obscure"
and uses Magento\Config\Model\Config\Backend\Encrypted
for the backend_model
. This is how Magento knows to use a masked form field and encrypt any user input on save.
<?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>
Vendor/Module/etc/config.xml
Adding backend_model="Magento\Config\Model\Config\Backend\Encrypted"
here tells Magento the config value should be decrypted when retrieved with 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>
Vendor/Module/etc/di.xml
This adds the configuration path to the sensitive array and prevents the path's value from being included when dumping the store configuration.
<?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>
If you have n98-magerun2.phar installed, you can get a decrypted config value with something like:
php bin/n98-magerun2.phar config:store:get --decrypt payment/webpay/keyid
You can also set encrypted config values from the command line with something like:
php bin/n98-magerun2.phar config:store:set --encrypt payment/webpay/keyid NEW_KEY_ID_VALUE_HERE
You can get n98-magerun2.phar from here: https://github.com/netz98/n98-magerun2
You can try with below method for payment encryption method to get value
,
You have to replace \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
with below class path, \Magento\Payment\Gateway\ConfigInterface
This is works fine,
<?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')
}
}
In case you want to decrypt some value using some key: Put below code into decrypt-config-value.php in the root of your magento project.
<?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";
Run php decrypt-config-value.php using console or //yourwebsite.com/decrypt-config-value.php using a browser.
Try with below code for json decode value,
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;
}
}