Skip to content

Commit 6101081

Browse files
committed
Added codec: ezip64
1 parent 5cee0cb commit 6101081

5 files changed

Lines changed: 80 additions & 19 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ This category also contains `ascii85`, `adobe`, `[x]btoa`, `zeromq` with the `ba
364364
- [X] `columnar`: aka Columnar Transposition Cipher (keyword-ordered columns ; a second keyword gives the double transposition)
365365
- [X] `enigma`: aka Enigma machine (Enigma I, M3 and M4, with reflector, rotors, rings, positions and plugboard)
366366
- [X] `esab46`: aka ESAB-46 (Base64 with a shuffled alphabet, from online JavaScript tools)
367+
- [X] `ezip64`: aka EZIP-64 (variant of ARMON-64 with smaller blocks)
367368
- [X] `foursquare`: aka Four-square Cipher (bigrams substituted with two keyed squares and two plain ones)
368369
- [X] `fractionated-morse`: aka Fractionated Morse Cipher (Morse code split into groups of three symbols, each replaced by a letter of a keyed alphabet)
369370
- [X] `gronsfeld`: aka Gronsfeld Cipher (variant of Vigenere Cipher with a numeric key)

‎docs/pages/enc/crypto.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,27 @@ This toy cipher ("Base-64" reversed) comes from online JavaScript encryption too
397397

398398
-----
399399

400+
### EZIP-64
401+
402+
This toy cipher, a variant of [ARMON-64](#armon-64), comes from online JavaScript encryption tools. It works the same way, but with blocks of a third of the key length (rounded), a factor of 0.02 in its fourth operation and `/` as the separator.
403+
404+
**Codec** | **Conversions** | **Aliases** | **Comment**
405+
:---: | :---: | --- | ---
406+
`ezip64` | text <-> EZIP-64 ciphertext | `ezip-64`, `ezip_64` | parameterized with a mandatory key of at least 3 characters, e.g. `ezip64-secret` ; only characters with a code below 256
407+
408+
!!! warning "Precision"
409+
410+
As the computations are done with double-precision floats, as in JavaScript, keys of more than 19 characters give blocks that are too large to be decrypted exactly.
411+
412+
```python
413+
>>> codext.encode("test", "ezip64-test")
414+
'-261.47089108910893/-261.81544554455445/-261.4938613861386/-261.47089108910893'
415+
>>> codext.decode("-261.47089108910893/-261.81544554455445", "ezip64-test")
416+
'te'
417+
```
418+
419+
-----
420+
400421
### Four-square Cipher
401422

402423
The Four-square cipher, invented by Felix Delastelle, substitutes pairs of letters using four squares arranged in a 2x2 matrix: the upper-left and lower-right ones hold the alphabet in order, while the upper-right and lower-left ones are filled with two keywords. The first letter of each pair is located in the upper-left square and the second one in the lower-right square ; they are replaced by the letters at the other corners of the rectangle they form (in the upper-right square, then in the lower-left one).

‎src/codext/crypto/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .columnar import *
1616
from .enigma import *
1717
from .esab46 import *
18+
from .ezip64 import *
1819
from .foursquare import *
1920
from .fractionatedmorse import *
2021
from .hill import *

‎src/codext/crypto/_jsfloat.py‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
# -*- coding: UTF-8 -*-
2-
"""Floating-point block ciphers, shared by the toy ciphers of online JavaScript encryption tools (AER-256, ARMON-64).
2+
"""Floating-point block ciphers, shared by the toy ciphers of online JavaScript encryption tools (AER-256, ARMON-64,
3+
EZIP-64).
34
4-
The text is split into blocks of half the key length (rounded up), each block being read as a big-endian number of its
5-
character codes. For each character of the key (at least 3), an operation with its code is applied to this number in
6-
turn (in floating-point arithmetic, cycling through the operations of the cipher), and the resulting numbers are
7-
written as JavaScript would (e.g. "123.456") and joined with a separator. Decryption applies the inverse operations in
8-
reverse order and rounds the result. As numbers are double-precision floats, blocks of more than 6 characters (i.e.
9-
keys of more than 12 characters) lose precision and generally cannot be fully decrypted. Only characters with a code
10-
below 256 can be encrypted, and null characters at the start of a block are lost.
5+
The text is split into blocks of a fraction of the key length (e.g. half of it, rounded), each block being read as a
6+
big-endian number of its character codes. For each character of the key (at least 3), an operation with its code is
7+
applied to this number in turn (in floating-point arithmetic, cycling through the operations of the cipher), and the
8+
resulting numbers are written as JavaScript would (e.g. "123.456") and joined with a separator. Decryption applies the
9+
inverse operations in reverse order and rounds the result. As numbers are double-precision floats, blocks of more than
10+
6 characters lose precision and generally cannot be fully decrypted. Only characters with a code below 256 can be
11+
encrypted, and null characters at the start of a block are lost.
1112
"""
1213
import re
1314
from math import floor, isfinite
1415

1516
from ..__common__ import ensure_str, handle_error
1617

1718

18-
# operations of the ciphers with their inverses, as (encryption, decryption) pairs
19+
# operations of the ciphers with their inverses, as (encryption, decryption) pairs ; the last one also takes the factor
20+
# of the cipher (as in JavaScript, the factor and the key code are multiplied first)
1921
OPERATIONS = [
20-
(lambda v, k: v + k, lambda v, k: v - k),
21-
(lambda v, k: v / k, lambda v, k: v * k),
22-
(lambda v, k: v - k, lambda v, k: v + k),
23-
(lambda v, k: v * (.01 * k), lambda v, k: v / (.01 * k)),
22+
(lambda v, k, f: v + k, lambda v, k, f: v - k),
23+
(lambda v, k, f: v / k, lambda v, k, f: v * k),
24+
(lambda v, k, f: v - k, lambda v, k, f: v + k),
25+
(lambda v, k, f: v * (f * k), lambda v, k, f: v / (f * k)),
2426
]
2527

2628

@@ -52,17 +54,18 @@ def js_str(x):
5254
return d[0] + ("." + d[1:] if k > 1 else "") + "e" + "+-"[n < 1] + str(abs(n - 1))
5355

5456

55-
def float_cipher(name, sep, n_ops):
56-
""" Make the encoding and decoding function factories of a cipher, given its separator and its number of
57-
operations. """
57+
def float_cipher(name, sep, n_ops, divisor=2, factor=.01):
58+
""" Make the encoding and decoding function factories of a cipher, given its separator, its number of operations,
59+
the divisor of the key length giving the block size and the factor of its fourth operation. """
5860
# the separator is not matched inside numbers in exponent notation (e.g. "1e+21")
5961
split = re.compile(r"(?<![eE])" + re.escape(sep))
6062

6163
def _key(key):
6264
key = str(key or "")
6365
if len(key) < 3:
6466
raise LookupError(f"Bad parameter for encoding '{name}': key must be at least 3 characters long")
65-
return [ord(c) for c in key], (len(key) + 1) // 2
67+
# block size as given by Math.round(key.length / divisor)
68+
return [ord(c) for c in key], (2 * len(key) + divisor) // (2 * divisor)
6669

6770
def encode_factory(key=None):
6871
def encode(text, errors="strict"):
@@ -73,7 +76,7 @@ def encode(text, errors="strict"):
7376
for i in range(0, len(t), n):
7477
v = float(int("".join(f"{ord(c):02x}" for c in t[i:i+n]), 16))
7578
for j, kc in enumerate(k):
76-
v = OPERATIONS[j % n_ops][0](v, kc)
79+
v = OPERATIONS[j % n_ops][0](v, kc, factor)
7780
r.append(js_str(v))
7881
return sep.join(r), len(text)
7982
return encode
@@ -89,7 +92,7 @@ def decode(text, errors="strict"):
8992
r += _h(s, i)
9093
continue
9194
for j in range(len(k) - 1, -1, -1):
92-
v = OPERATIONS[j % n_ops][1](v, k[j])
95+
v = OPERATIONS[j % n_ops][1](v, k[j], factor)
9396
# Math.round, then the hexadecimal digits of the number (nothing when it is not positive)
9497
v = floor(v) + (v - floor(v) >= .5) if isfinite(v) else 0
9598
h = f"{v:x}" if v > 0 else ""

‎src/codext/crypto/ezip64.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: UTF-8 -*-
2+
"""EZIP-64 Codec - EZIP-64 content encryption.
3+
4+
EZIP-64 is a toy cipher found on online JavaScript encryption tools, a variant of ARMON-64. The text is split into
5+
blocks of a third of the key length (rounded), each block being read as a big-endian number of its character codes.
6+
For each character of the key (at least 3), its code is added to, divides, is subtracted from or (multiplied by 0.02)
7+
multiplies this number in turn (in floating-point arithmetic), and the resulting numbers are written as JavaScript
8+
would (e.g. "123.456") and separated by "/". Decryption applies the inverse operations in reverse order and rounds the
9+
result. As numbers are double-precision floats, blocks of more than 6 characters (i.e. keys of more than 19
10+
characters) lose precision and generally cannot be fully decrypted. Only characters with a code below 256 can be
11+
encrypted, and null characters at the start of a block are lost.
12+
13+
This codec:
14+
- en/decodes strings from str to str
15+
- en/decodes strings from bytes to bytes
16+
- decodes file content to str (read)
17+
- encodes file content from str to bytes (write)
18+
19+
Reference: http://qbarbe.free.fr/crypto/eng_ezip64p.php
20+
"""
21+
from ._jsfloat import float_cipher
22+
from ..__common__ import add
23+
24+
25+
__examples__ = {
26+
'enc(ezip64|ezip64-ab)': None,
27+
'enc(ezip64-test|ezip-64_test)': {'test': "-261.47089108910893/-261.81544554455445/-261.4938613861386/"
28+
"-261.47089108910893"},
29+
'dec(ezip64-test)': {'-261.47089108910893/-261.81544554455445': "te", '-261.4/x': None},
30+
'enc-dec(ezip64-key|ezip64-secret|ezip64-K3y!|ezip64-1234567890123456789)': ["", "this is a test",
31+
"Hello, World! 123", "\xe9t\xe9 \xff"],
32+
}
33+
34+
35+
add("ezip64", *float_cipher("ezip64", "/", 4, 3, .02), r"^ezip[-_]?64(?:[-_](\S*))?$", guess=None)

0 commit comments

Comments
 (0)