Aggiornamento minore

* Migliorato il generatore di numeri casuali;
* Corretto un errore nel namespace;
* Corretti errori vari.
This commit is contained in:
Rnhmjoj 2013-01-05 01:20:04 +01:00
parent 43cf75679b
commit 795e1ceda5
6 changed files with 21 additions and 8 deletions

4
.gitignore vendored
View File

@ -1,2 +1,6 @@
.DS_Store
<<<<<<< HEAD
__pycache__
=======
__pycache__/
>>>>>>> Aggiornamento minore

View File

@ -1,9 +1,9 @@
import crittografia
stringa = crittografia.unicode.codifica("Testo da cifrare.")
b = stringa.rot13()
a = stringa.vigenère(verme = "verme")
a = stringa.rot13()
b = stringa.vigenère(verme = "verme")
c = stringa.vernam()
d = stringa.gödel(primo = 1117)
print(a, b, c, d, sep = "\n - ")
print(a, b, c, d, sep = "\n")

View File

@ -1,3 +1,2 @@
from .alfabeto import *
from .unicode import *
from .alfabeto import *
from .unicode import *

View File

@ -3,7 +3,7 @@ ordinale = dict([(k,i) for i,k in enumerate(alfabeto)]) class codifica:
def __init__(self, valore):
valore = str(valore)
if valore.isnumeric(): self.numero = int(valore)
else: self.stringa = str(valore)
else: self.stringa = valore
def rot13(self): rotazione = str.maketrans("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM") return str.translate(self.stringa, rotazione) def vigenère(self, verme):
self.stringa = self.stringa.lower().replace(" ","") cifrata = "" for i in range(len(self.stringa)): cifrata += alfabeto[(ordinale[self.stringa[i]] + ordinale[verme[i % len(verme)]]) % 26] return cifrata def gödel(self, primo):
self.stringa = self.stringa.lower().replace(" ","") cifrata = 1 sequenza = primi.lista(primo, len(self.stringa)) for i in range(len(self.stringa)): cifrata *= sequenza[i]**(ordinale[self.stringa[i]] + 1) return cifrata def vernam(self):

View File

@ -1 +1,11 @@
import math,random,itertools,pyaudio class primi: def primo(n): for i in range(3, int(math.sqrt(n)) + 1,2): if n % i == 0: return False return True def prossimo(n): if n % 2 == 0: n += 1 while True: n += 2 if primi.primo(n): yield n def lista(p, n): return tuple([p for p in itertools.islice(primi.prossimo(p), n)]) class generatore: def rumore(l): audio = pyaudio.PyAudio() output = audio.open(format = pyaudio.paInt8, channels = 1, output=True, rate = 50000) for i in range(l*2500): output.write(chr(int(random.random() * 25 + 1))) output.close() audio.terminate() def leggirumore(l): audio = pyaudio.PyAudio() input = audio.open(format = pyaudio.paInt16, channels = 2, rate = 44100, input = True, frames_per_buffer = 1024) return bytes(input.read(int(44100 / 1024 * l * 10))) input.close() audio.terminate() def onetimepad(l): dati = generatore.leggirumore(l) numeri = [int(dati[i] * random.random() * l) for i in range(len(dati)) if dati[i] != 0] inizio = int(random.random()*l) return tuple([numeri[i] for i in range(inizio, inizio + l)])
import math,random,itertools,pyaudio class primi: def primo(n): for i in range(3, int(math.sqrt(n)) + 1,2): if n % i == 0: return False return True def prossimo(n): if n % 2 == 0: n += 1 while True: n += 2 if primi.primo(n): yield n def lista(p, n): return tuple([p for p in itertools.islice(primi.prossimo(p), n)]) class generatore: def rumore(l): audio = pyaudio.PyAudio() output = audio.open(format = pyaudio.paInt8, channels = 1, output=True, rate = 50000) for i in range(int(l*100/3)): output.write(chr(int(random.random() * 25 + 1))) output.close() audio.terminate() def leggirumore(inizio, fine, stop):
audio = pyaudio.PyAudio()
input = audio.open(format = pyaudio.paInt16, channels = 2, rate = 44100, input = True, frames_per_buffer = 1024)
dati = input.read(int(44100 / 1024 * stop * 10))
input.close()
audio.terminate()
dati = [i for i in dati if i != 0]
numeri = [(i + random.randint(inizio,fine)) % (fine + abs(inizio)) + inizio for i in dati]
for i in range(stop):
yield numeri[i]
def onetimepad(l): return tuple(generatore.leggirumore(0,2*16,l))

View File

@ -2,7 +2,7 @@ from crittografia.numeri import * max = 2**(32) class codifica:
def __init__(self, valore):
valore = str(valore)
if valore.isnumeric(): self.numero = int(valore)
else: self.stringa = str(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): cifrata = "" 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)