Skip to content

Instantly share code, notes, and snippets.

@dlyapun
Last active August 29, 2015 14:21
Show Gist options
  • Save dlyapun/9a585bd6ddab3a19e9b6 to your computer and use it in GitHub Desktop.
Save dlyapun/9a585bd6ddab3a19e9b6 to your computer and use it in GitHub Desktop.
Python lab
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, time
def main():
menu()
# Ввод начальных параметров
secter_key = raw_input("Введите ключ - ")
text = raw_input("Введите текст - ")
if len(secter_key) > len(text):
print "Не логично вводить ключ длинее сообщения, пока!"
exit()
# Подготовка параметров к шифорвания
count_element = 0 # Сколько добавим звездочек
while 1:
if (len(text) % int(len(secter_key))):
text = text + '*'
count_element = count_element + 1
else:
break
srez = int(len(text)) - count_element
secret_text = cript(text, secter_key)
print "Шифрование сообщения:"
# time.sleep(1)
for x in xrange(1,int(len(text))):
# time.sleep(0.2)
# print "*"
pass
print "Зашифрованный текст - " + str(secret_text)
text = uncript(secret_text, secter_key)
print "Расшифровка сообщения:"
# time.sleep(1)
for x in xrange(1,int(len(text))):
# time.sleep(0.2)
# print "*"
pass
print "Обработанный текст - " + str(text[:srez])
def cript(text, secter_key):
column = int(len(secter_key))
count = column
secret_text = text[0]
secret_array = {}
iteration_loop = 0
while iteration_loop != column:
secret_text = secret_text + text[count]
count = count + column
if count >= len(text):
primary_key = secter_key.upper()[iteration_loop]
secret_array[primary_key] = secret_text
print primary_key, secret_array
iteration_loop = iteration_loop + 1
count = iteration_loop
secret_text = ""
secret_text = secret_word(secret_array)
return secret_text
def secret_word(secret_array):
secret_text = ""
for key in sorted(secret_array.iterkeys()):
# print "%s: %s" % (key, secret_array[key])
secret_text = secret_text + secret_array[key]
return secret_text
def uncript(secret_text, secter_key):
count = len(secret_text) / len(secter_key) # 4 символов в массиве
key = []
for q in secter_key.upper():
key = key + [q]
key_sorted = sorted(key)
column = int(len(secter_key)) # 3 букв в слове
text = ""
secret_array = {}
iteration_loop = 0
y = 0
z = count
while iteration_loop != column:
for x in range(y,z):
text = text + secret_text[x]
primary_key = key_sorted[iteration_loop]
secret_array[primary_key] = text
iteration_loop = iteration_loop + 1
y = y + count
z = z + count
text = ""
for x in range(0, column):
text = text + secret_array[key[x]]
# Обработка
column = int(len(secter_key))
column = count
secret_text = text[0]
iteration_loop = 0
while iteration_loop != column:
secret_text = secret_text + text[count]
count = count + column
if count >= len(text):
iteration_loop = iteration_loop + 1
count = iteration_loop
return secret_text
def menu():
print "======================================="
print "=======ШИФРОВЩИК СООБЩЕНИЙ 2000!======="
print "=========Автор - Дмитрий Ляпун========="
print "======================================="
print "======================================="
print "Загрузка:"
time.sleep(1)
for x in xrange(1,10):
# time.sleep(0.2)
# print "*"
pass
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment