added facotrisation and inverse modulo code
This commit is contained in:
parent
f377305f49
commit
64d66e5c82
67
column_transposition.py
Normal file
67
column_transposition.py
Normal file
@ -0,0 +1,67 @@
|
||||
# Entschlüsselung einer Spaltentransposition bzw. Erzeugung einer Spaltentransformation
|
||||
# gibt alle Varianten der Matrix aus (sofern möglich, aussser fuer 1 und Gesamtzahl)
|
||||
|
||||
import numpy as np
|
||||
|
||||
text = "Ml6,=o:3K1d81e1u49y3"
|
||||
|
||||
# Test ob Laenge des Einganstextes keine Primzahl
|
||||
prim = len(text)
|
||||
nonprim_list = []
|
||||
for n in range(2, prim):
|
||||
if prim % n == 0:
|
||||
nonprim_list.append(n)
|
||||
|
||||
if nonprim_list == []:
|
||||
print("Die Zeichenlänge", prim, "ist eine Primzahl, daher keine Spaltentransformation möglich.")
|
||||
|
||||
# leere Liste für Geheimtext
|
||||
t = [] * len(text)
|
||||
# wandle Geheimtext in Liste um
|
||||
for i in text:
|
||||
t.append(i)
|
||||
|
||||
|
||||
# ermittle die Teiler der Buchstabenanzahl
|
||||
def get_factors(x):
|
||||
# leere Liste fuer Faktoren
|
||||
f = []
|
||||
# durchlaufe Anzahl Buchstaben
|
||||
for i in range(1, x + 1):
|
||||
# wenn teilbar
|
||||
if x % i == 0:
|
||||
# erweitere Liste um Teiler
|
||||
f.append(i)
|
||||
return f
|
||||
|
||||
|
||||
# erstelle Matrix
|
||||
def get_matrix(t, f):
|
||||
# entferne Wert 1 und Wert der Buchstabenanzahl
|
||||
# 1 Spalte und 1 Zeile wird nicht dargestellt
|
||||
f = f[1:]
|
||||
f = f[:-1]
|
||||
# fuer Anzahl Spalten
|
||||
for col in f:
|
||||
# leerer String fuer Darstellung
|
||||
s = ""
|
||||
# Zeilen = Anzahl / Spalten
|
||||
row = int(len(t) / col)
|
||||
print('\nSpalten:', col, ', Zeilen:', row)
|
||||
# erstelle Matrix aus Liste
|
||||
m = np.asarray(t).reshape(col, row).T
|
||||
print('Matrix:')
|
||||
# durchlaufe Anzahl der Zeilen
|
||||
for r in range(row):
|
||||
# Ausgabe der Spalteninhalte
|
||||
for c in range(col):
|
||||
print(m[r][c], end=" ")
|
||||
s = s + m[r][c]
|
||||
print()
|
||||
# Ausgabe des moeglichen Klartextes
|
||||
print('\nString:\n' + s)
|
||||
print('\n-----')
|
||||
|
||||
|
||||
get_matrix(t, get_factors(len(text)))
|
||||
|
16
fermat_factorisation.py
Normal file
16
fermat_factorisation.py
Normal file
@ -0,0 +1,16 @@
|
||||
import math
|
||||
import decimal
|
||||
|
||||
|
||||
def factorise(n):
|
||||
x = math.ceil(math.sqrt(n))
|
||||
y = x ** 2 - n
|
||||
while not math.sqrt(y).is_integer():
|
||||
x += 1
|
||||
y = x ** 2 - n
|
||||
return x + math.sqrt(y), x - math.sqrt(y)
|
||||
|
||||
|
||||
n = 1364593
|
||||
print("Die beiden Faktoren p und q lauten:", factorise(n))
|
||||
|
19
mult_inv_phi.py
Normal file
19
mult_inv_phi.py
Normal file
@ -0,0 +1,19 @@
|
||||
def extended_euclidean(a, b):
|
||||
if a == 0:
|
||||
return b, 0, 1
|
||||
else:
|
||||
g, x, y = extended_euclidean(b % a, a)
|
||||
return g, y - (b // a) * x, x
|
||||
|
||||
def mod_inverse(e, phi):
|
||||
g, x, _ = extended_euclidean(e, phi)
|
||||
if g != 1:
|
||||
raise Exception('modular inverse does not exist')
|
||||
else:
|
||||
return x % phi
|
||||
|
||||
e = 13
|
||||
phi = 1362240
|
||||
|
||||
d = mod_inverse(e, phi)
|
||||
print(d)
|
7
multi_inv.py
Normal file
7
multi_inv.py
Normal file
@ -0,0 +1,7 @@
|
||||
|
||||
e = 13
|
||||
n = 1364593
|
||||
|
||||
y = pow(e, -1, n)
|
||||
|
||||
print(y)
|
23
pollard_rho.py
Normal file
23
pollard_rho.py
Normal file
@ -0,0 +1,23 @@
|
||||
#!/usr/bin/python
|
||||
import math
|
||||
|
||||
|
||||
def rho(n):
|
||||
x = 3
|
||||
y = 3
|
||||
d = 1
|
||||
while d<=1:
|
||||
x = (x * x + 23) % n
|
||||
y = (y * y + 23) % n
|
||||
y = (y * y + 23) % n
|
||||
d = math.gcd(x-y,n)
|
||||
return d
|
||||
|
||||
|
||||
def main():
|
||||
result = rho(1364593)
|
||||
print(f"p = {result}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue
Block a user