78 lines
1.9 KiB
Python
78 lines
1.9 KiB
Python
from crittografia.numeri import *
|
|
|
|
max = 2**(16)
|
|
|
|
class codifica:
|
|
|
|
def __init__(self, valore):
|
|
valore = str(valore)
|
|
if valore.isnumeric():
|
|
self.numero = int(valore)
|
|
else: self.stringa = valore
|
|
|
|
def rot13(self):
|
|
rotazione = str.maketrans(
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
|
|
)
|
|
return str.translate(self.stringa, rotazione)
|
|
|
|
def vigenère(self, verme):
|
|
cifrata = ""
|
|
for i in range(len(self.stringa)):
|
|
cifrata += chr((ord(self.stringa[i]) + ord(verme[i % len(verme)])) % max)
|
|
return cifrata
|
|
|
|
def gödel(self, primo):
|
|
cifrata = 1
|
|
sequenza = primi.lista(primo, len(self.stringa))
|
|
for i in range(len(self.stringa)):
|
|
cifrata *= sequenza[i]**(ord(self.stringa[i]) + 1)
|
|
return cifrata
|
|
|
|
def vernam(self, pad=()):
|
|
cifrata = ""
|
|
if pad is ():
|
|
pad = generatore.onetimepad(len(self.stringa))
|
|
for i in range(len(self.stringa)):
|
|
cifrata += chr((ord(self.stringa[i]) + pad[i]) % max)
|
|
return cifrata, pad
|
|
|
|
class decodifica:
|
|
|
|
def __init__(self, valore):
|
|
valore = str(valore)
|
|
if valore.isnumeric():
|
|
self.numero = int(valore)
|
|
else:
|
|
self.stringa = str(valore)
|
|
|
|
def rot13(self):
|
|
rotazione = str.maketrans(
|
|
"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM",
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
)
|
|
return str.translate(self.stringa, rotazione)
|
|
|
|
def vigenère(self, verme):
|
|
decifrata = ""
|
|
for i in range(len(self.stringa)):
|
|
decifrata += chr((ord(self.stringa[i]) - ord(verme[i % len(verme)])) % max)
|
|
return decifrata
|
|
|
|
def gödel(self, primo, n):
|
|
decifrata = ""
|
|
for i in primi.lista(primo,n):
|
|
lettera = -1
|
|
while self.numero % i == 0:
|
|
self.numero /= i
|
|
lettera += 1
|
|
decifrata += chr(lettera % max)
|
|
return decifrata
|
|
|
|
def vernam(self, pad):
|
|
cifrata = ""
|
|
for i in range(len(self.stringa)):
|
|
cifrata += chr((ord(self.stringa[i]) - pad[i]) % max)
|
|
return cifrata
|