-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclaves_cristeros.py
286 lines (261 loc) · 10.3 KB
/
claves_cristeros.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Preamble
def letnum(letter):
return ord(letter) - ord('a')
def add(a,b):
return a+b
def sub(a,b):
return a-b
def bold(string):
return "\033[1m" + str(string) + "\033[0m"
# Cifrado César
def cesar(word='',op=add):
wd = word.lower().split()
wd2 = []
for i in range(len(wd)):
wd1 = [op(letnum(letter),3) for letter in wd[i]]
wd2 += [chr(ord('a')+num%26) for num in wd1]
wd2 += [' '] # Espacio entre palabras
wd2 = ''.join(wd2)
return wd2
def cesar1(word=''):
if word == '':
word = input('Entrada: ')
while word != '':
wd2 = cesar(word,add)
print(".........................................................")
print(bold("In: "),word)
print(bold("Out:"),wd2)
print(".........................................................")
word = input('Entrada: ')
def cesar2(word=''):
if word == '':
word = input('Entrada: ')
while word != '':
wd2 = cesar(word,sub)
print(".........................................................")
print(bold("In: "),word)
print(bold("Out:"),wd2)
print(".........................................................")
word = input('Entrada: ')
# Cifrado Rotacional
def crot(word='',op=add,n=3):
wd = word.lower().split()
wd2 = []
for i in range(len(wd)):
wd1 = [op(letnum(letter),n) for letter in wd[i]] # Rotacíon de n letras
wd2 += [chr(ord('a')+num%26) for num in wd1]
wd2 += [' '] # Espacio entre palabras
wd2 = ''.join(wd2)
return wd2
def crot1(word='',n=3):
if word == '':
word = input('Entrada: ')
while word != '':
wd2 = crot(word,add,n=n)
print(".........................................................")
print(bold("In: "),word)
print(bold("Out:"),wd2)
print(".........................................................")
word = input('Entrada: ')
def crot2(word='',n=3):
if word == '':
word = input('Entrada: ')
while word != '':
wd2 = crot(word,sub,n=n)
print(".........................................................")
print(bold("In: "),word)
print(bold("Out:"),wd2)
print(".........................................................")
word = input('Entrada: ')
# Cifrado por Transposición
def trp(word=''):
wd = word.lower().split()
wd2 = [elm[::-1] for elm in wd]
wd2 = ' '.join(wd2)
return wd2
def trp1(word=''):
if word =='':
word = input('Texto plano: ')
while word != '':
wd2 = trp(word)
print(".........................................................")
print(bold("In: "),word)
print(bold("Out:"),wd2)
print(".........................................................")
word = input('Texto plano: ')
# Cifrado de Vigenere
def vig(key='',word='',op=add):
keyN = [ord(letter) - ord('a') for letter in key] # Clave a números
words = word.lower().split()
wd,KEY,wd2 = [[]]*len(words),[[]]*len(words),[[]]*len(words) # Listas vacías
ct = 0
for i in range(len(words)):
wd[i] = [ord(letter) - 97 for letter in words[i]] # Texto plano a números
for j in range(len(words[i])):
KEY[i] = KEY[i] + [keyN[ct%len(key)]] # Clave dimensiones texto plano
wd[i][j] = op(wd[i][j],KEY[i][j])%26 # Suma/resta para cifrado/descifrado
ct += 1 # Conteo global
wd2[i] = ''.join([chr(ord('a')+num) for num in wd[i]]) # num a let y juntar letras en palabras
wd2 = ' '.join(wd2) # Juntar palabras por medio de espacios
return wd2
def vig1(key="",word=""):
if key == '':
key = input('Clave: ')
if word == '':
word = input('Texto plano: ')
while word != "":
wd2 = vig(key,word,op=add)
print(".........................................................")
print(bold("In: "),word)
print(bold("Out:"),wd2)
print(".........................................................")
word = input("Texto plano: ")
def vig2(key="",word=""):
if word == '':
word = input('Texto plano: ')
if key == '':
key = input('Clave: ')
while key != "":
wd2 = vig(key,word,op=sub)
print(".........................................................")
print(bold("In: "),word)
print(bold("Out:"),wd2)
print(".........................................................")
key = input("Clave: ")
# Clave Inversa
def inv(word=''):
wd = '»'.join(word.lower().split(' '))
wd2 = ''.join([chr(abs(219-ord(letter))) for letter in wd])
wd2 = 'ñ'.join(wd2.split('\x16'))
return wd2
# Clave Murciélago
def mur(word='',n=0,key='murcielago'):
mur_wd = [letter for letter in 'murcielago']
mur_num = [str((num+n)%10) for num in range(10)]
mur_dic = dict(zip(mur_wd+mur_num,mur_num+mur_wd))
wd = ''.join([mur_dic[letter] if (letter in mur_dic) else letter for letter in word.lower()])
return wd
def mur1(word='',n=0,key='murcielago'):
if word == '':
word = input('Texto plano: ')
while word != '':
wd = mur(word,n,key)
print(".........................................................")
print(bold("In: "),word) # Show input word
print(bold("Out:"),wd) # Show resulting cipher
print(".........................................................")
word = input('Texto plano: ')
alf = [chr(ord('a')+num) for num in list(range(0,26))]
morse_let = ['.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--',
'-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..']
morse_num = ['-----','.----','..---','...--','....-','.....','-....','--...','---..','----.']
nums = [str(num) for num in range(0,10)]
morse_dic = dict(zip(alf+nums,morse_let+morse_num))
morse_sym = [('á','.--.-'),('é','..-..'),('ó','---.'),('ä','.-.-'),('ö','---.'),('ü','..--'),('à','.--.-'),
('è','.-..-'),('ç','-.-..'),('Đ','..--.'),('Ĝ','--.-.'),('Ĵ','.---.'),('Š','...-.'),
('Þ','.--..'),('ß','...--..'),('&','.-...'),('.','.-.-.-'),(',','--..--'),(';','-.-.-.'),
(':','---...'),('¿','..-.-'),('?','..--..'),('¡','--...-'),('!','-.-.--'),("'",'.----.'),
('"','.-..-.'),('(','-.--.'),(')','-.--.-'),('/','-..-.'),('-','-....-'),('_','..--.-'),
('+','.-.-.'),('=','-...-'),('$','...-..-'),('@','.--.-.'),('ñ','--.--'),(' ','')]
def addic(tup,dic=morse_dic):
dic[tup[0]] = tup[1]
for i in range(len(morse_sym)):
addic(morse_sym[i])
morse_sen = [('R','...-.'),('E','........'),('T','-.-'),('W','.-...'),
('M','.-.-.'),('F','...-.-'),('B','-.-.-'),('A','-')]
# R - roger
# E - error
# T - invitación a transmitir
# W - esperar
# M - fin de mensaje
# F - fin de transmisión
# B - comienzo de transmisión
# A - atención
for i in range(len(morse_sen)):
addic(morse_sen[i])
def morse(word=''):
wd = [morse_dic[letter] + '/' for letter in word.lower()]
wd2 = ''.join(wd)
return wd2
def morse1(word=''):
if word == '':
word = input('Texto plano: ')
wd2 = morse(word)
while word != '':
print(".........................................................")
print(bold("In: "),word) # Show input word
print(bold("Out:"),wd2) # Show resulting cipher
print(".........................................................")
word = input('Texto plano: ')
morse_dic_inv = dict(zip(morse_let+morse_num,alf+nums))
def addic_inv(tup,dic=morse_dic_inv):
dic[tup[1]] = tup[0]
for i in range(len(morse_sym)):
addic_inv(morse_sym[i])
def morsi(word=''):
wd = word.lower().split('/')
wd = [morse_dic_inv[letter] for letter in wd]
wd2 = ''.join(wd)
return wd2
def morse2(word=''):
if word == '':
word = input('Texto plano: ')
while word != '':
wd2 = morsi(word)
print(".........................................................")
print(bold("In: "),word) # Show input word
print(bold("Out:"),wd2) # Show resulting cipher
print(".........................................................")
word = input('Texto plano: ')
from pydub import AudioSegment
from pydub.playback import play
from pydub.generators import Sine
def morsd(word='',freq=700,unit=350,file=False):
if word == '':
word = input('Texto plano: ')
gen = Sine(freq)
dot = gen.to_audio_segment(duration=unit)
dash = gen.to_audio_segment(duration=3*unit)
p = AudioSegment.silent(duration=unit)
pp = AudioSegment.silent(duration=3*unit)
ppp = AudioSegment.silent(duration=7*unit)
MAD = {'.':dot,'-':dash,'0':p,'/':pp,'//':ppp}
wd = morse(word)
morseplit = '0'.join([sym for sym in wd])
morseplay = [MAD[sym] for sym in morseplit]
out = sum(morseplay)
if file:
file_name = input('Nombre del archivo: ')
out.export(file_name+'.mp3',format = 'mp3')
return out
def morse_tr(word='',freq=700,unit=350):
if word == '':
word = input('Texto plano: ')
word = 'A ' + word + ' M'
gen = Sine(freq)
dot = gen.to_audio_segment(duration=unit)
dash = gen.to_audio_segment(duration=3*unit)
p = AudioSegment.silent(duration=unit)
pp = AudioSegment.silent(duration=3*unit)
ppp = AudioSegment.silent(duration=7*unit)
MAD = {'.':dot,'-':dash,'0':p,'/':pp,'//':ppp}
wd = morse(word)
morseplit = '0'.join([sym for sym in wd])
morseplay = [MAD[sym] for sym in morseplit]
return sum(morseplay)
# Práctica dictado de morse
import string,random,threading
def practice(n = 10,unit=400):
word = ''.join(random.choice(string.ascii_lowercase) for _ in range(n))
wd2 = morsd(word,unit=unit)
def f1():
answer = input('Frase dictada:')
if answer == word:
print('¡Correcto!')
else:
print('Incorrecto')
print('La frase era:',word)
def f2(aud):
play(aud)
threading.Thread(target=f1).start()
threading.Thread(target=f2(wd2)).start()