Created
February 20, 2025 13:02
-
-
Save grimmo/f9b0da6840446f0f46a749b1316764e3 to your computer and use it in GitHub Desktop.
Semplice sostituzione monoalfabetica per generare crittogrammi stile settimana enigmistica
This file contains hidden or 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
| import string,random,sys | |
| def cripta(testo): | |
| chiaro = list(filter(lambda x:x in string.ascii_uppercase or x in ' ',testo.upper())) | |
| chiaro = ''.join(chiaro) | |
| cifrario = {} | |
| lettere = random.sample(string.ascii_uppercase,26) | |
| numeri = random.sample(range(1,27),26) | |
| [cifrario.update({k:v}) for k,v in zip(lettere,numeri)] | |
| cifrario['-'] = '-' | |
| chiaro = chiaro.replace(' ','-').upper() | |
| cifrato = [cifrario[x] for x in chiaro] | |
| cifrato = list(map(str,cifrato)) | |
| return ' '.join(cifrato),dict(zip(cifrario.values(), cifrario.keys())) | |
| def decripta(cifrato,cifrario): | |
| "Sostituisce i numeri con le lettere corrispondenti nel cifrario, mettendo False al posto dei trattini. Poi converte \ | |
| i False in spazi e poi riconverte tutto in stringa" | |
| chiaro = ''.join(list(map(lambda y: y == False and ' ' or y,[x != '-' and cifrario[int(x)] for x in cifrato.split(' ')]))) | |
| return chiaro | |
| cifrato,cifrario = cripta(sys.argv[1]) | |
| print(f"Testo cifrato:{cifrato}") | |
| print(f"Cifrario:{cifrario}") | |
| print(f"Testo decriptato: {decripta(cifrato,cifrario)}") | |
| #print decripta(sys.argv[1],sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment