Skip to content

Instantly share code, notes, and snippets.

@hkarasek
Last active August 29, 2015 14:10
Show Gist options
  • Save hkarasek/d26caa0152111ecaf57b to your computer and use it in GitHub Desktop.
Save hkarasek/d26caa0152111ecaf57b to your computer and use it in GitHub Desktop.
Homofonní šifra
from pprint import pprint as print
import random
import string
class Crypter:
# iniciace
NAMESPACE_LENGH = 60
NAMESPACE = string.ascii_lowercase
def __init__(self, text, table=False):
self.text = text
if table:
self.table = table
else:
self.table = self.gen_table()
# vytvoření šifrovací tabulky
def gen_table(self):
res = {}
range_shuffled = list(range(self.NAMESPACE_LENGH))
namespace_shuffled = list(self.NAMESPACE)
random.shuffle(range_shuffled)
random.shuffle(namespace_shuffled)
while len(range_shuffled) > 0:
for char in namespace_shuffled:
if len(range_shuffled) == 0:
break
else:
if char not in res:
res[char] = []
res[char].append(range_shuffled.pop())
return res
# zašifrování znaku
def encrypt_char(self, char):
return random.choice(self.table[char])
# zašifrovat celý řetězec
def encrypt(self):
res = []
for char in self.text:
res.append(str(self.encrypt_char(char)))
return res
def __str__(self):
return ' '.join(self.encrypt())
c = Crypter("gymbk")
print(c.table)
print(str(c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment