Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Decrypter.php #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 10 additions & 51 deletions src/Decrypter.php
Original file line number Diff line number Diff line change
@@ -1,126 +1,92 @@
<?php

namespace Decrypter;

use DOMDocument;
use Defuse\Crypto\Crypto;
use Defuse\Crypto\KeyProtectedByPassword;
use Defuse\Crypto\Key;

class Decrypter
{
protected $xmlPath;

protected $exportKey;

protected $masterKey;

public function __construct($xmlPath, $exportKey, $masterKey)
{
$this->xmlPath = $xmlPath;
$this->exportKey = $exportKey;
$this->masterKey = $masterKey;
}

public function decrypt()
{
$decryptedNodes = $this->decryptXmlNodes($this->xmlPath, $this->exportKey);

$decryptedNodesValidXML = $this->decryptedNodesToValidXML($decryptedNodes);

list($categories, $customers) = $this->getCategoriesAndCustomers($decryptedNodesValidXML);

list($categories, $clients) = $this->getCategoriesAndClients($decryptedNodesValidXML);
$decryptedAccounts = $this->decryptAccountPasswords(
$decryptedNodesValidXML,
$categories,
$customers
$clients
);

return $decryptedAccounts;
}

protected function unlockKey($key, $password)
{
$unlockedKey = KeyProtectedByPassword::loadFromAsciiSafeString($key)
->unlockKey($password);

return $unlockedKey;
}

protected function decryptXmlNodes($xml, $exportKey)
{
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

$dataNodes = $xmlDoc->getElementsByTagName('Data');
$decryptedNodes = [];

foreach ($dataNodes as $dataNode) {
$data = base64_decode($dataNode->nodeValue);

$lockedKey = $dataNode->getAttribute('key');
$unlockedKey = $this->unlockKey($lockedKey, $this->exportKey);

$decryptedNode = Crypto::decrypt($data, $unlockedKey);
$decryptedNodes[] = $decryptedNode;
}

return $decryptedNodes;
}

protected function decryptedNodesToValidXML($decryptedNodes)
{
$decryptedNodesValidXML = '<root>';

foreach ($decryptedNodes as $decryptedNode) {
$decryptedNodesValidXML .= $decryptedNode;
}

$decryptedNodesValidXML .= '</root>';

file_put_contents('decrypted.xml', $decryptedNodesValidXML);

return $decryptedNodesValidXML;
}

protected function getCategoriesAndCustomers($decryptedNodes)
protected function getCategoriesAndClients($decryptedNodes)
{
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($decryptedNodes, LIBXML_PARSEHUGE);
$xmlDoc->saveXML();

$nodeNames = ['Category', 'Customer'];
$categoriesAndCustomers = [];
$nodeNames = ['Category', 'Client'];
$categoriesAndClients = [];
$outerNodeIndex = 0;

foreach ($nodeNames as $nodeName) {
$nodes = $xmlDoc->getElementsByTagName($nodeName);

foreach ($nodes as $node) {
$id = $node->getAttribute('id');
$name = $node->getElementsByTagName('name')
->item(0)
->nodeValue;

$categoriesAndCustomers[$outerNodeIndex][$id] = $name;
$categoriesAndClients[$outerNodeIndex][$id] = $name;
}

$outerNodeIndex++;
}

return $categoriesAndCustomers;
return $categoriesAndClients;
}

protected function decryptAccountPasswords($decryptedNodes, $categories, $customers)
protected function decryptAccountPasswords($decryptedNodes, $categories, $clients)
{
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($decryptedNodes, LIBXML_PARSEHUGE);
$xmlDoc->saveXML();

$accountNodes = $xmlDoc->getElementsByTagName('Account');

$decryptedAccounts = [];

foreach ($accountNodes as $accountNode) {
$url = $accountNode->getElementsByTagName('url')
->item(0)
Expand All @@ -137,34 +103,27 @@ protected function decryptAccountPasswords($decryptedNodes, $categories, $custom
$accountPassword = $accountNode->getElementsByTagName('pass')
->item(0)
->nodeValue;
$customerId = $accountNode->getElementsByTagName('customerId')
$clientId = $accountNode->getElementsByTagName('clientId')
->item(0)
->nodeValue;
$categoryId = $accountNode->getElementsByTagName('categoryId')
->item(0)
->nodeValue;

$unlockedKey = $this->unlockKey($accountKey, $this->masterKey);

$decryptedPassword = Crypto::decrypt($accountPassword, $unlockedKey);
$encoding = mb_detect_encoding($decryptedPassword);

if (strtolower($encoding) === 'utf-8') {
$decryptedPassword = utf8_decode($decryptedPassword);
}

$decryptedAccounts[] = [
'url' => $url,
'name' => $name,
'login' => $accountLogin,
'password' => $decryptedPassword,
'category' => $categories[$categoryId],
'customer' => $customers[$customerId]
'client' => $clients[$clientId]
];
}

return $decryptedAccounts;
}

}