Skip to content

Commit 2d670cd

Browse files
authored
Release 0.4.4
* Removed unnecessary line breaks * README.md updated * ElGamal homomorphic ciphertext extension
1 parent bd80202 commit 2d670cd

6 files changed

+61
-10
lines changed

README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ Python library for demonstrating the functionality of common cryptographic algor
44

55
## Requirements
66

7-
Python 3.7.9 or later including pip for installing the following requirements:
8-
9-
```shell
10-
pip install -r requirements.txt
11-
```
7+
Python 3.7.9 or later.
128

139
### Creating a virtual environment
1410

cryptographic_functions/elgamal_calculations.py

+56
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,62 @@ def homomorphic_multiplicative_scheme(public_key, private_key, c_1, c_2, print_m
301301
return m
302302

303303

304+
# ElGamal homomorphic ciphertext extension
305+
def homomorphic_ciphertext_extension(public_key, private_key, m_1, a_b, print_matrix=False,
306+
print_linear_factorization=True):
307+
print(tabulate([['Homomorphe Erweiterung des Geheimtextes']], tablefmt='fancy_grid'))
308+
309+
# Unpack both keys into its components
310+
p, g, e = public_key
311+
p_v, d = private_key
312+
313+
# Unpack the combined ciphertext into its components
314+
a, b = a_b
315+
316+
# The value of p must be identical in both keys
317+
if p != p_v:
318+
print(f'Die Variablen p = {p} und p_v = {p_v} müssen identisch sein.')
319+
return -1
320+
321+
# Calculation of m
322+
a_d = (a ** d) % p
323+
a_i = modulo_inverse_multiplicative.mim(p, a_d, print_matrix, print_linear_factorization, 1)
324+
m = (a_i * b) % p
325+
326+
# Calculation of m_2
327+
m_1_i = modulo_inverse_multiplicative.mim(p, m_1, print_matrix, print_linear_factorization, 2)
328+
m_2 = (m * m_1_i) % p
329+
330+
# Calculation path output
331+
print(
332+
f'Gegeben sind K(pub) = {{p, g, e}} = {{{p}, {g}, {e}}} und K(priv) = {{p, d}} = {{{p_v}, {d}}} mit dem aus '
333+
f'Geheimtext 1 und 2 erweiterten Geheimtext a_b = {{a, b}} = {{{a}, {b}}}. Ebenfalls bekannt ist der zu '
334+
f'Geheimtext 1 zugehörige Klartext m_1 = {m_1}. Durch die Umkehrung der Geheimtext-Erweiterung soll nun der '
335+
f'Klartext m_2 ermittelt werden.', end='\n\n')
336+
print(
337+
f'Der zum erweiterten Geheimtext (a, b) gehörende Klartext m ergibt sich aus der Gleichung a^d * m = b mod p '
338+
f'zu:\n'
339+
f'm = b * (a^d)^-1 mod p\n'
340+
f'm = {b} * ({a}^{d})^-1 mod {p}\n'
341+
f'm = {b} * {a_d}^-1 mod {p}\n'
342+
f'<AUXILIARY 1>Achtung: Die Namen der Variablen können abweichen!</AUXILIARY 1>\n'
343+
f'm = {b} * {a_i} mod {p}\n'
344+
f'm = {m}', end='\n\n')
345+
print(
346+
f'Der Klartext m_2, welcher zur Erweiterung des Klartexts m_1 verwendet wurde, ergibt sich aus:\n'
347+
f'm_2 = m * m_1^-1 mod p\n'
348+
f'm_2 = {m} * {m_1}^-1 mod {p}\n'
349+
f'<AUXILIARY 2>Achtung: Die Namen der Variablen können abweichen!</AUXILIARY 2>\n'
350+
f'm_2 = {m} * {m_1_i} mod {p}\n'
351+
f'm_2 = {m_2}', end='\n\n')
352+
print(
353+
f'Verifikation:\n'
354+
f'm = m_1 * m_2 mod p\n'
355+
f'{m} = {m_1} * {m_2} mod {p}\n'
356+
f'{m} = {(m_1 * m_2) % p}', end='\n\n')
357+
return m_2
358+
359+
304360
# ElGamal homomorphic multiplicative decryption
305361
def homomorphic_multiplicative_decryption(public_key, private_key, m_1, c_1, c_2, print_matrix=False,
306362
print_linear_factorization=True):

cryptographic_functions/modulo_calculations.py

-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def addition(m, a, b):
3434
f'({a} + {b}) / {m} = {q} + ({r} / {m})\n'
3535
f'{a} + {b} = {q} * {m} + {r}\n'
3636
f'Daraus folgt: {a}{b} = {r}', end='\n\n')
37-
3837
return r
3938

4039

@@ -88,7 +87,6 @@ def multiplication(m, a, b):
8887
f'({a} * {b}) / {m} = {q} + ({r} / {m})\n'
8988
f'{a} * {b} = {q} * {m} + {r}\n'
9089
f'Daraus folgt: {a}{b} = {r}', end='\n\n')
91-
9290
return r
9391

9492

cryptographic_functions/modulo_cyclic_groups.py

-1
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,4 @@ def mcg(m, print_matrix=False):
5454
print(f'Für die zyklische Gruppe der Ordnung m = {m} konnten keine nicht-primitiven Elemente ermittelt werden.',
5555
end='\n\n')
5656
n = -1
57-
5857
return p, n

cryptographic_functions/shared_functions.py

-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def gcd_extended(m, a):
3232
list_r.append(r)
3333
m = a
3434
a = r
35-
3635
return list_m, list_a, list_q, list_r
3736

3837

@@ -72,7 +71,6 @@ def linear_factorization(list_m, list_a, list_q, list_r):
7271
list_y_calc.append(f'{x} - {q} * ({list_x[-1]}) = {y}')
7372
list_y.append(y)
7473
x = list_x[-1]
75-
7674
return list_m, list_a, list_q, list_r, list_x[::-1], list_y_calc[::-1], list_y[::-1]
7775

7876

main.py

+4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
elgamal_p_n = 3
100100
elgamal_s = 7
101101
elgamal_signed_message = (elgamal_plaintext, elgamal_p_n, elgamal_s)
102+
elgamal_homomorphic_a_b = (3, 3)
102103
elgamal_homomorphic_c_1 = (3, 3)
103104
elgamal_homomorphic_c_2 = (6, 3)
104105
elgamal_homomorphic_m_1 = 6
@@ -112,6 +113,9 @@
112113
# elgamal_calculations.homomorphic_multiplicative_scheme(elgamal_public_key, elgamal_private_key,
113114
# elgamal_homomorphic_c_1, elgamal_homomorphic_c_2,
114115
# print_matrix, print_linear_factorization)
116+
# elgamal_calculations.homomorphic_ciphertext_extension(elgamal_public_key, elgamal_private_key,
117+
# elgamal_homomorphic_m_1, elgamal_homomorphic_a_b,
118+
# print_matrix, print_linear_factorization)
115119
# elgamal_calculations.homomorphic_multiplicative_decryption(elgamal_public_key, elgamal_private_key,
116120
# elgamal_homomorphic_m_1, elgamal_homomorphic_c_1,
117121
# elgamal_homomorphic_c_2, print_matrix,

0 commit comments

Comments
 (0)