Last active
December 19, 2015 01:09
-
-
Save billowdood/5874295 to your computer and use it in GitHub Desktop.
One Time Pad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Descifrado del mensaje | |
def D(c,key): | |
#Alfabeto para descifrar,se utilizo la ref(2) | |
alf = ascii_lowercase | |
alf += ' .' | |
deStr = '' | |
for partC in c: | |
for car in range(len(partC)): | |
deStr += alf[int(partC[car]) - int(key[c.index(partC)][car])] | |
return deStr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python oneTimePad.py alice 1 '16 5 16 18 2 13 7 26 5 5 28 19 18 23 4 1 1 28 5 21 12 17 19 15' | |
mensaje de prueba cripto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Encriptacion del mensaje | |
def E(m,key): | |
#Alfabeto para encriptar,se utilizo la ref(2) | |
alph = ascii_lowercase | |
alph += ' .' | |
enStr = '' | |
for partMess in m: | |
for car in range(len(partMess)): | |
enStr += str(alph.index(partMess[car]) + int(key[m.index(partMess)][car])) | |
enStr += ' ' | |
return enStr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Borrar las lineas utilizadas del archivo que se uso | |
#Se utilizo la ref(1) | |
def deleteLine(ownerFile,name,rawLines): | |
lines = ownerFile.readlines() | |
ownerFile.close() | |
fileOvw = open(name+'.dat','w') | |
for line in lines: | |
if not (line in rawLines): | |
fileOvw.write(line) | |
fileOvw.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python oneTimePad.py bob 0 'mensaje de prueba cripto' | |
Se crearan los archivos porque no existen | |
Cuantas paginas? 25 | |
Longitud de las llaves 5 | |
16 5 16 18 2 13 7 26 5 5 28 19 18 23 4 1 1 28 5 21 12 17 19 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python oneTimePad.py nombre accion 'mensaje' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from random import shuffle | |
from sys import argv | |
from string import ascii_lowercase | |
#Generar los archivos con las llaves si los archivos no existen | |
def generateFiles(numPages,keyLen): | |
aliceF, bobF = open('alice.dat','w'),open('bob.dat','w') | |
for p in range(numPages): | |
key = range(keyLen) | |
shuffle(key) | |
stringKey = lineToWrite(key) | |
aliceF.write(stringKey) | |
bobF.write(stringKey) | |
aliceF.close() | |
bobF.close() | |
return | |
#Las llaves se generan como numeros,se pasa a un string y se agrega el salto de linea | |
def lineToWrite(key): | |
for dig in range(len(key)): | |
key[dig] = str(key[dig]) | |
stringKey = ' '.join(key) | |
stringKey += '\n' | |
return stringKey | |
#Encriptacion del mensaje | |
def E(m,key): | |
#Alfabeto para encriptar,se utilizo la ref(2) | |
alph = ascii_lowercase | |
alph += ' .' | |
enStr = '' | |
for partMess in m: | |
for car in range(len(partMess)): | |
enStr += str(alph.index(partMess[car]) + int(key[m.index(partMess)][car])) | |
enStr += ' ' | |
return enStr | |
#Descifrado del mensaje | |
def D(c,key): | |
#Alfabeto para descifrar,se utilizo la ref(2) | |
alf = ascii_lowercase | |
alf += ' .' | |
deStr = '' | |
for partC in c: | |
for car in range(len(partC)): | |
deStr += alf[int(partC[car]) - int(key[c.index(partC)][car])] | |
return deStr | |
#Borrar las lineas utilizadas del archivo que se uso | |
#Se utilizo la ref(1) | |
def deleteLine(ownerFile,name,rawLines): | |
lines = ownerFile.readlines() | |
ownerFile.close() | |
fileOvw = open(name+'.dat','w') | |
for line in lines: | |
if not (line in rawLines): | |
fileOvw.write(line) | |
fileOvw.close() | |
#Cantidad de llaves a usar para el mensaje | |
def keysNeeded(ownerFile,complMess,action): | |
""" | |
Si es descifrado,se quitan los espacios en blanco entre los digitos | |
y se convierte a un solo array | |
""" | |
if action == 1: | |
complMess = complMess.strip().split(' ') | |
actualLine = ownerFile.readline() | |
key = actualLine.strip().split(' ') | |
#Siempre se lee aunque sea una linea | |
rawLines = [actualLine] | |
keysRead = [key] | |
#Si el mensaje tiene la misma longitud que la llave o es multiplo | |
if len(complMess) % len(key) == 0: | |
keyS = len(complMess) / len(key) | |
#Si no,es mas grande o mas chico | |
else: | |
keyS = len(complMess) / len(key) + 1 | |
cropMess = [] | |
start = 0 | |
end = len(key) | |
for i in range(keyS): | |
cropMess.append(complMess[start:end]) | |
start += len(key) | |
end += len(key) | |
for line in range(1,keyS): | |
rawLines.append(ownerFile.readline()) | |
tRawLines = rawLines[line] | |
keysRead.append(tRawLines.strip().split(' ')) | |
return keysRead,cropMess,rawLines | |
def main(): | |
""" | |
argumento 1: quien usa el programa | |
argumento 2: para encriptar o descifrar | |
argumento 3: mensaje a cifrar o descifrar | |
""" | |
try: | |
owner = argv[1] | |
except: | |
err = 'Primer argumento: alice o bob' | |
return err | |
try: | |
action = int(argv[2]) | |
except: | |
err = 'Segundo argumento: 0 encriptado 1 descifrado' | |
return err | |
try: | |
message = argv[3] | |
except: | |
err = 'Tercer argumento: mensaje (utilizar comillas)' | |
return err | |
try: | |
ownerFile = open(owner+'.dat','r') | |
except: | |
print 'Se crearan los archivos porque no existen' | |
numPag = int(raw_input('Cuantas paginas? ')) | |
lenKey = int(raw_input('Longitud de las llaves ')) | |
generateFiles(numPag,lenKey) | |
ownerFile = open(owner+'.dat','r') | |
""" | |
keys: cantidad de llaves a utilizar | |
cutMessage: mensaje partido en pedazos(depende de la longitud del msg y de las llaves) | |
rawLines: lineas 'crudas' del archivo (con saltos de linea), se utiliza para borrar las lineas | |
""" | |
keys,cutMessage,rawLines = keysNeeded(ownerFile,message,action) | |
if action == 0: | |
altMess = E(cutMessage,keys) | |
if action == 1: | |
altMess = D(cutMessage,keys) | |
deleteLine(ownerFile,owner,rawLines) | |
return altMess | |
print main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment