Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit d4954a2

Browse files
committed
ECB stateless, so no Context
1 parent f560ac3 commit d4954a2

19 files changed

+112
-83
lines changed

lib/Mode/ECB.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,28 @@
77

88
class ECB extends Cipher
99
{
10-
private $key;
11-
12-
function __construct(Key $key)
13-
{
14-
$this->key = $key;
15-
}
16-
17-
function encrypt(string $message): string
10+
function encrypt(Key $key, string $message): string
1811
{
1912
$offset = 0;
2013
$out = '';
2114

2215
$blocks = strlen($message) >> 4;
2316
while ($blocks--) {
24-
$out .= $this->encryptBlock($this->key, substr($message, $offset, 16));
17+
$out .= $this->encryptBlock($key, substr($message, $offset, 16));
2518
$offset += 16;
2619
}
2720

2821
return $out;
2922
}
3023

31-
function decrypt(string $message): string
24+
function decrypt(Key $key, string $message): string
3225
{
3326
$offset = 0;
3427
$out = '';
3528

3629
$blocks = strlen($message) >> 4;
3730
while ($blocks--) {
38-
$out .= $this->decryptBlock($this->key, substr($message, $offset, 16));
31+
$out .= $this->decryptBlock($key, substr($message, $offset, 16));
3932
$offset += 16;
4033
}
4134

test/ECB/ECBGFSbox128Test.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ function caseProvider()
3232
*/
3333
function testEncrypt($key, $plaintext, $ciphertext)
3434
{
35-
$ecb = new ECB(new Key(hex2bin($key)));
36-
$result = $ecb->encrypt(hex2bin($plaintext));
35+
$key = new Key(hex2bin($key));
36+
$ecb = new ECB;
37+
$result = $ecb->encrypt($key, hex2bin($plaintext));
3738
$this->assertSame(hex2bin($ciphertext), $result);
3839
}
3940

@@ -42,8 +43,9 @@ function testEncrypt($key, $plaintext, $ciphertext)
4243
*/
4344
function testDecrypt($key, $plaintext, $ciphertext)
4445
{
45-
$ecb = new ECB(new Key(hex2bin($key)));
46-
$result = $ecb->decrypt(hex2bin($ciphertext));
46+
$key = new Key(hex2bin($key));
47+
$ecb = new ECB;
48+
$result = $ecb->decrypt($key, hex2bin($ciphertext));
4749
$this->assertSame(hex2bin($plaintext), $result);
4850
}
4951
}

test/ECB/ECBGFSbox192Test.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ function caseProvider()
3131
*/
3232
function testEncrypt($key, $plaintext, $ciphertext)
3333
{
34-
$ecb = new ECB(new Key(hex2bin($key)));
35-
$result = $ecb->encrypt(hex2bin($plaintext));
34+
$key = new Key(hex2bin($key));
35+
$ecb = new ECB;
36+
$result = $ecb->encrypt($key, hex2bin($plaintext));
3637
$this->assertSame(hex2bin($ciphertext), $result);
3738
}
3839

@@ -41,8 +42,9 @@ function testEncrypt($key, $plaintext, $ciphertext)
4142
*/
4243
function testDecrypt($key, $plaintext, $ciphertext)
4344
{
44-
$ecb = new ECB(new Key(hex2bin($key)));
45-
$result = $ecb->decrypt(hex2bin($ciphertext));
45+
$key = new Key(hex2bin($key));
46+
$ecb = new ECB;
47+
$result = $ecb->decrypt($key, hex2bin($ciphertext));
4648
$this->assertSame(hex2bin($plaintext), $result);
4749
}
4850
}

test/ECB/ECBGFSbox256Test.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ function caseProvider()
3030
*/
3131
function testEncrypt($key, $plaintext, $ciphertext)
3232
{
33-
$ecb = new ECB(new Key(hex2bin($key)));
34-
$result = $ecb->encrypt(hex2bin($plaintext));
33+
$key = new Key(hex2bin($key));
34+
$ecb = new ECB;
35+
$result = $ecb->encrypt($key, hex2bin($plaintext));
3536
$this->assertSame(hex2bin($ciphertext), $result);
3637
}
3738

@@ -40,8 +41,9 @@ function testEncrypt($key, $plaintext, $ciphertext)
4041
*/
4142
function testDecrypt($key, $plaintext, $ciphertext)
4243
{
43-
$ecb = new ECB(new Key(hex2bin($key)));
44-
$result = $ecb->decrypt(hex2bin($ciphertext));
44+
$key = new Key(hex2bin($key));
45+
$ecb = new ECB;
46+
$result = $ecb->decrypt($key, hex2bin($ciphertext));
4547
$this->assertSame(hex2bin($plaintext), $result);
4648
}
4749
}

test/ECB/ECBKeySbox128Test.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ function caseProvider()
4646
*/
4747
function testEncrypt($key, $plaintext, $ciphertext)
4848
{
49-
$ecb = new ECB(new Key(hex2bin($key)));
50-
$result = $ecb->encrypt(hex2bin($plaintext));
49+
$key = new Key(hex2bin($key));
50+
$ecb = new ECB;
51+
$result = $ecb->encrypt($key, hex2bin($plaintext));
5152
$this->assertSame(hex2bin($ciphertext), $result);
5253
}
5354

@@ -56,8 +57,9 @@ function testEncrypt($key, $plaintext, $ciphertext)
5657
*/
5758
function testDecrypt($key, $plaintext, $ciphertext)
5859
{
59-
$ecb = new ECB(new Key(hex2bin($key)));
60-
$result = $ecb->decrypt(hex2bin($ciphertext));
60+
$key = new Key(hex2bin($key));
61+
$ecb = new ECB;
62+
$result = $ecb->decrypt($key, hex2bin($ciphertext));
6163
$this->assertSame(hex2bin($plaintext), $result);
6264
}
6365
}

test/ECB/ECBKeySbox192Test.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ function caseProvider()
4949
*/
5050
function testEncrypt($key, $plaintext, $ciphertext)
5151
{
52-
$ecb = new ECB(new Key(hex2bin($key)));
53-
$result = $ecb->encrypt(hex2bin($plaintext));
52+
$key = new Key(hex2bin($key));
53+
$ecb = new ECB;
54+
$result = $ecb->encrypt($key, hex2bin($plaintext));
5455
$this->assertSame(hex2bin($ciphertext), $result);
5556
}
5657

@@ -59,8 +60,9 @@ function testEncrypt($key, $plaintext, $ciphertext)
5960
*/
6061
function testDecrypt($key, $plaintext, $ciphertext)
6162
{
62-
$ecb = new ECB(new Key(hex2bin($key)));
63-
$result = $ecb->decrypt(hex2bin($ciphertext));
63+
$key = new Key(hex2bin($key));
64+
$ecb = new ECB;
65+
$result = $ecb->decrypt($key, hex2bin($ciphertext));
6466
$this->assertSame(hex2bin($plaintext), $result);
6567
}
6668
}

test/ECB/ECBKeySbox256Test.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ function caseProvider()
4141
*/
4242
function testEncrypt($key, $plaintext, $ciphertext)
4343
{
44-
$ecb = new ECB(new Key(hex2bin($key)));
45-
$result = $ecb->encrypt(hex2bin($plaintext));
44+
$key = new Key(hex2bin($key));
45+
$ecb = new ECB;
46+
$result = $ecb->encrypt($key, hex2bin($plaintext));
4647
$this->assertSame(hex2bin($ciphertext), $result);
4748
}
4849

@@ -51,8 +52,9 @@ function testEncrypt($key, $plaintext, $ciphertext)
5152
*/
5253
function testDecrypt($key, $plaintext, $ciphertext)
5354
{
54-
$ecb = new ECB(new Key(hex2bin($key)));
55-
$result = $ecb->decrypt(hex2bin($ciphertext));
55+
$key = new Key(hex2bin($key));
56+
$ecb = new ECB;
57+
$result = $ecb->decrypt($key, hex2bin($ciphertext));
5658
$this->assertSame(hex2bin($plaintext), $result);
5759
}
5860
}

test/ECB/ECBMCT128Test.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,11 @@ function decryptProvider()
231231
*/
232232
function testEncrypt($key, $plaintext, $ciphertext)
233233
{
234-
$ecb = new ECB(new Key(hex2bin($key)));
234+
$key = new Key(hex2bin($key));
235+
$ecb = new ECB;
235236
$result = hex2bin($plaintext);
236237
for ($i = 0; $i < 1000; $i++) {
237-
$result = $ecb->encrypt($result);
238+
$result = $ecb->encrypt($key, $result);
238239

239240
}
240241
$this->assertSame(hex2bin($ciphertext), $result);
@@ -245,10 +246,11 @@ function testEncrypt($key, $plaintext, $ciphertext)
245246
*/
246247
function testDecrypt($key, $ciphertext, $plaintext)
247248
{
248-
$ecb = new ECB(new Key(hex2bin($key)));
249+
$key = new Key(hex2bin($key));
250+
$ecb = new ECB;
249251
$result = hex2bin($ciphertext);
250252
for ($i = 0; $i < 1000; $i++) {
251-
$result = $ecb->decrypt($result);
253+
$result = $ecb->decrypt($key, $result);
252254
}
253255
$this->assertSame(hex2bin($plaintext), $result);
254256
}

test/ECB/ECBMCT192Test.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,11 @@ function decryptProvider()
231231
*/
232232
function testEncrypt($key, $plaintext, $ciphertext)
233233
{
234-
$ecb = new ECB(new Key(hex2bin($key)));
234+
$key = new Key(hex2bin($key));
235+
$ecb = new ECB;
235236
$result = hex2bin($plaintext);
236237
for ($i = 0; $i < 1000; $i++) {
237-
$result = $ecb->encrypt($result);
238+
$result = $ecb->encrypt($key, $result);
238239

239240
}
240241
$this->assertSame(hex2bin($ciphertext), $result);
@@ -245,10 +246,11 @@ function testEncrypt($key, $plaintext, $ciphertext)
245246
*/
246247
function testDecrypt($key, $ciphertext, $plaintext)
247248
{
248-
$ecb = new ECB(new Key(hex2bin($key)));
249+
$key = new Key(hex2bin($key));
250+
$ecb = new ECB;
249251
$result = hex2bin($ciphertext);
250252
for ($i = 0; $i < 1000; $i++) {
251-
$result = $ecb->decrypt($result);
253+
$result = $ecb->decrypt($key, $result);
252254
}
253255
$this->assertSame(hex2bin($plaintext), $result);
254256
}

test/ECB/ECBMCT256Test.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,11 @@ function decryptProvider()
231231
*/
232232
function testEncrypt($key, $plaintext, $ciphertext)
233233
{
234-
$ecb = new ECB(new Key(hex2bin($key)));
234+
$key = new Key(hex2bin($key));
235+
$ecb = new ECB;
235236
$result = hex2bin($plaintext);
236237
for ($i = 0; $i < 1000; $i++) {
237-
$result = $ecb->encrypt($result);
238+
$result = $ecb->encrypt($key, $result);
238239

239240
}
240241
$this->assertSame(hex2bin($ciphertext), $result);
@@ -245,10 +246,11 @@ function testEncrypt($key, $plaintext, $ciphertext)
245246
*/
246247
function testDecrypt($key, $ciphertext, $plaintext)
247248
{
248-
$ecb = new ECB(new Key(hex2bin($key)));
249+
$key = new Key(hex2bin($key));
250+
$ecb = new ECB;
249251
$result = hex2bin($ciphertext);
250252
for ($i = 0; $i < 1000; $i++) {
251-
$result = $ecb->decrypt($result);
253+
$result = $ecb->decrypt($key, $result);
252254
}
253255
$this->assertSame(hex2bin($plaintext), $result);
254256
}

0 commit comments

Comments
 (0)