TripleDESService::binsec2hexsec()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Greenlyst\BaseCommerce\Core;
6
7
final class TripleDESService
8
{
9
    private $io_key;
10
11
    /**
12
     * Creates a new TripleDESService using the provided HEX encoded String which will be converted back to bytes and used to generate the Key.
13
     *
14
     * @param $key string HEX encoded string to create Key from
15
     */
16
    public function __construct($key)
17
    {
18
        if (function_exists('hex2bin')) {
19
            $this->io_key = hex2bin($key);
20
        } else {
21
            $sbin = '';
22
            for ($i = 0; $i < strlen($key); $i += 2) {
23
                $sbin .= pack('H*', substr($key, $i, 2));
24
            }
25
            $this->io_key = $sbin;
26
        }
27
    }
28
29
    /**
30
     * Encrypts the plain text that is provided using the Key this TripleDESService was initialized with.
31
     *
32
     * @param string The plain text to be encrypted
0 ignored issues
show
Bug introduced by
The type Greenlyst\BaseCommerce\Core\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
     *
34
     * @return string The cipher text resulting from the encryption of the plain text
35
     */
36
    public function encrypt($input)
37
    {
38
        $data = openssl_encrypt($input, 'DES-EDE3', $this->io_key, OPENSSL_RAW_DATA, '');
39
        $data = $this->txtsec2binsec($data);
40
        $data = $this->binsec2hexsec($data);
41
        $data = urlencode($data);
42
43
        return $data;
44
    }
45
46
    /**
47
     * Decrypts the cipher text that is provided using the Key this TripleDESService was initialized with.
48
     *
49
     * @param string Cipher text the cipher text to be decrypted
0 ignored issues
show
Bug introduced by
The type Greenlyst\BaseCommerce\Core\Cipher was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
     *
51
     * @return string The plain text resulting from the decryption of the cipher text
52
     */
53
    public function decrypt($input)
54
    {
55
        $input = $this->hexsec2binsec($input);
56
        $input = $this->binsec2txtsec($input);
57
        $decrypted_data = openssl_decrypt($input, 'DES-EDE3', $this->io_key, OPENSSL_RAW_DATA, '');
58
59
        return trim(rtrim($decrypted_data));
60
    }
61
62
    /**
63
     * @param $binsec string
64
     *
65
     * @return string
66
     */
67
    private function binsec2txtsec($binsec): string
68
    {
69
        $data = '';
70
        for ($i = 0; $i < strlen($binsec); $i += 8) {
71
            $bin = substr($binsec, $i, 8);
72
            $data .= chr(bindec($bin));
0 ignored issues
show
Bug introduced by
It seems like bindec($bin) can also be of type double; however, parameter $ascii of chr() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
            $data .= chr(/** @scrutinizer ignore-type */ bindec($bin));
Loading history...
73
        }
74
75
        return $data;
76
    }
77
78
    /**
79
     * @param $txtsec string
80
     *
81
     * @return string
82
     */
83
    private function txtsec2binsec($txtsec): string
84
    {
85
        $data = '';
86
        for ($i = 0; $i < strlen($txtsec); $i++) {
87
            $mybyte = decbin(ord($txtsec[$i]));
88
            $MyBitSec = substr('00000000', 0, 8 - strlen($mybyte)).$mybyte;
89
            $data .= $MyBitSec;
90
        }
91
92
        return $data;
93
    }
94
95
    /**
96
     * @param $binsec string
97
     *
98
     * @return string
99
     */
100
    private function binsec2hexsec($binsec): string
101
    {
102
        $data = '';
103
        for ($i = 0; $i < strlen($binsec); $i += 8) {
104
            $bin = substr($binsec, $i, 8);
105
            $hex = dechex(bindec($bin));
0 ignored issues
show
Bug introduced by
It seems like bindec($bin) can also be of type double; however, parameter $number of dechex() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
            $hex = dechex(/** @scrutinizer ignore-type */ bindec($bin));
Loading history...
106
            $hex = substr('00', 0, 2 - strlen($hex)).$hex;
107
            $data .= $hex;
108
        }
109
110
        return $data;
111
    }
112
113
    /**
114
     * @param $hexsec string
115
     *
116
     * @return string
117
     */
118
    private function hexsec2binsec($hexsec): string
119
    {
120
        $data = '';
121
        for ($i = 0; $i < strlen($hexsec); $i += 2) {
122
            $byte = decbin(hexdec($hexsec[$i].$hexsec[$i + 1]));
0 ignored issues
show
Bug introduced by
It seems like hexdec($hexsec[$i] . $hexsec[$i + 1]) can also be of type double; however, parameter $number of decbin() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
            $byte = decbin(/** @scrutinizer ignore-type */ hexdec($hexsec[$i].$hexsec[$i + 1]));
Loading history...
123
            $bitSec = substr('00000000', 0, 8 - strlen($byte)).$byte;
124
            $data .= $bitSec;
125
        }
126
127
        return $data;
128
    }
129
}
130