Skip to content

Новикова ПИ20-3 #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 63 additions & 4 deletions ecryption.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,63 @@
def encrypt(k, m):
return ''.join(map(chr, [x + k for x in map(ord, m)]))

print(encrypt(2, 'Hello!'))
#!/usr/bin/env python
# coding: utf-8

# In[ ]:


from collections import Counter
from random import randint
N = 65536


def encrypt_caesar(k, m):
offset_ords = [(x + k)%N for x in map(ord, m)]
return ''.join(map(chr, offset_ords))


def decrypt_caesar(k, m):
offset_ords = [(x - k)%N for x in map(ord, m)]
return ''.join(map(chr, offset_ords))


def hack(m):
most_common = Counter(m).most_common()[0][0]
key = ord(most_common) - ord(' ')
return decrypt_caesar(key, m)


# шифр вернама
def generate_key(length):
return [randint(0,26) for _ in range(length)]

def xor(char, key):
return chr(ord(char)^key)

def encrypt_v(text):
key = generate_key(len(text))
return ''.join([xor(text[i], key[i]) for i in range(len(text))]), key

def decrypt_v(cryptedtext, key):
return ''.join([xor(cryptedtext[i], key[i]) for i in range(len(cryptedtext))])

TEXT_TO_CHECK = 'The last words of the Roman dictator Julius Caesar are disputed.'
key = int(input('Введите ключ для Цезаря: '))
text = encrypt_caesar(key, TEXT_TO_CHECK)
print('Зашифрованный текст Цезаря:', text)
print(' ')
print('Расшифрованный текст Цезаря:', decrypt_caesar(key, text))
print(' ')
print('Взломанный текст Цезаря:', hack(text))
print(' ')
encrypt_v, key = encrypt_v(TEXT_TO_CHECK)
print('Зашифрованный шифром Вернама текст: ', encrypt_v)
print(' ')
decrypt_v = decrypt_v(encrypt_v, key)
print('Расшифрованный текст: ', decrypt_v)
print(' ')


# In[ ]: