Created
October 22, 2015 15:29
-
-
Save Kanst/67edd2123bce6954001a to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
from collections import deque | |
import logging | |
import sys | |
## | |
text = list('10110110') | |
key = list('1111111100') | |
rashirenie = [4, 1, 2, 3, 2, 3, 4, 1] | |
p_perestanov = [2, 4, 3, 1] | |
s1 = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 1]] | |
s2 = [[1, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 0, 1, 3]] | |
## | |
p_pryamoi = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] | |
p_sjatie = [6, 3, 7, 4, 8, 5, 10, 9] | |
nach = [2, 6, 3, 1, 4, 8, 5, 7] | |
con = [4, 1, 3, 5, 7, 2, 8, 6] | |
# log | |
log = logging.getLogger('log') | |
log.setLevel(logging.DEBUG) | |
_format = logging.Formatter("%(levelname)s\t%(message)s") | |
_handler = logging.StreamHandler(stream=sys.stdout) | |
_handler.setFormatter(_format) | |
_handler.setLevel(logging.DEBUG) | |
log.addHandler(_handler) | |
# | |
def peres(s, p): | |
result = [] | |
for x in p: | |
result.append(int(s[x-1])) | |
return result | |
def join(x): | |
return ''.join([str(i) for i in x]) | |
def gen_key(): | |
log.debug("Генерация ключей для ключа: \t%s" % ''.join(key)) | |
pp = peres(key, p_pryamoi) | |
log.debug("После перестановки: \t\t%s" % join(pp)) | |
L = deque(pp[0:5]) | |
L.rotate(-1) | |
R = deque(pp[5:]) | |
R.rotate(-1) | |
L_R = ''.join([str(i) for i in L]) + ''.join([str(i) for i in R]) | |
log.debug("После сдвига: \t\t\tL=%s R=%s" % (L_R[:5], L_R[5:])) | |
key1 = peres(L_R, p_sjatie) | |
log.info("Ключ раунда 1: \t\t\t%s" % ''.join([str(i) for i in key1])) | |
L.rotate(-2) | |
R.rotate(-2) | |
L_R = ''.join([str(i) for i in L]) + ''.join([str(i) for i in R]) | |
log.debug("После сдвига: \t\t\tL=%s R=%s" % (L_R[:5], L_R[5:])) | |
key2 = peres(L_R, p_sjatie) | |
log.info("Ключ раунда 2: \t\t\t%s\n" % ''.join([str(i) for i in key2])) | |
return key1, key2 | |
def xor(a, b): | |
n = len(a) | |
a = int(a, 2) | |
b = int(b, 2) | |
x = bin(a ^ b) | |
x = x[2:] | |
for i in range(n-len(x)): | |
x = '0' + x | |
return x | |
def s_block(s): | |
k = int(str(s[1]) + str(s[2]), 2) | |
v = int(str(s[0]) + str(s[3]), 2) | |
tmp1 = s1[v][k] | |
tmp1 = bin(tmp1)[2:] | |
for x in range(2-len(tmp1)): | |
tmp1 = '0' + tmp1 | |
k = int(str(s[5]) + str(s[6]), 2) | |
v = int(str(s[4]) + str(s[7]), 2) | |
tmp2 = s2[v][k] | |
tmp2 = bin(tmp2)[2:] | |
for x in range(2-len(tmp2)): | |
tmp2 = '0' + tmp2 | |
tmp = tmp1 + tmp2 | |
return tmp | |
def shifr(key1, key2): | |
log.info("Шифрование") | |
nach_perest = peres(text, nach) | |
log.info("Начальная перестановка(IP): \t%s" % join(nach_perest)) | |
log.info("Раунд 1") | |
R0 = join(nach_perest[4:]) | |
log.debug("R0: \t\t\t\t%s" % R0) | |
r = join(peres(nach_perest[4:], rashirenie)) | |
log.debug("Расширение: \t\t\t%s" % r) | |
log.debug("\t\t\t\t\t⊕") | |
key1 = ''.join([str(i) for i in key1]) | |
log.debug("Ключ раунда 1: \t\t\t%s" % key1) | |
xo = xor(r, key1) | |
log.debug("\t\t\t\t%s" % xo) | |
s_bl = s_block(xo) | |
log.debug("\t\t\t\t%s" % s_bl) | |
rash = peres(s_bl, p_perestanov) | |
log.debug("Перестановка \t\t\t%s" % join(rash)) | |
log.debug("\t\t\t\t\t⊕") | |
L0 = join(nach_perest[:4]) | |
log.debug("L0: \t\t\t\t%s" % L0) | |
R1 = xor(join(rash), L0) | |
log.debug("R1: \t\t\t\t%s" % R1) | |
L1 = R0 | |
log.info("L1 R1: \t\t\t\t%s %s" % (L1, R1)) | |
log.info("Раунд 2") | |
log.debug("R1: \t\t\t\t%s" % R1) | |
r = join(peres(R1, rashirenie)) | |
log.debug("Расширение: \t\t\t%s" % r) | |
log.debug("\t\t\t\t\t⊕") | |
key2 = ''.join([str(i) for i in key2]) | |
log.debug("Ключ раунда 2: \t\t\t%s" % key2) | |
xo = xor(r, key2) | |
log.debug("\t\t\t\t%s" % xo) | |
s_bl = s_block(xo) | |
log.debug("\t\t\t\t%s" % s_bl) | |
rash = peres(s_bl, p_perestanov) | |
log.debug("Перестановка \t\t\t%s" % join(rash)) | |
log.debug("\t\t\t\t\t⊕") | |
L1 = join(L1) | |
log.debug("L1: \t\t\t\t%s" % L1) | |
L2 = xor(join(rash), L1) | |
log.debug("L2: \t\t\t\t%s" % L2) | |
R2 = R1 | |
log.info("L2 R2: \t\t\t\t%s %s" % (L2, R2)) | |
con_perest = peres(L2 + R2, con) | |
log.info("Конечная перестановка(IP-1): \t%s" % join(con_perest)) | |
return con_perest | |
def deshifr(s, key1, key2): | |
log.info("Дешифрование") | |
nach_perest = peres(s, nach) | |
log.info("Начальная перестановка(IP): \t%s" % join(nach_perest)) | |
log.info("Раунд 2") | |
R2 = join(nach_perest[4:]) | |
log.debug("R2: \t\t\t\t%s" % R2) | |
r = join(peres(nach_perest[4:], rashirenie)) | |
log.debug("Расширение: \t\t\t%s" % r) | |
log.debug("\t\t\t\t\t⊕") | |
key2 = ''.join([str(i) for i in key2]) | |
log.debug("Ключ раунда 2: \t\t\t%s" % key2) | |
xo = xor(r, key2) | |
log.debug("\t\t\t\t%s" % xo) | |
s_bl = s_block(xo) | |
log.debug("\t\t\t\t%s" % s_bl) | |
rash = peres(s_bl, p_perestanov) | |
log.debug("Перестановка \t\t\t%s" % join(rash)) | |
log.debug("\t\t\t\t\t⊕") | |
L2 = join(nach_perest[:4]) | |
log.debug("L2: \t\t\t\t%s" % L2) | |
R1 = xor(join(rash), L2) | |
log.debug("R1: \t\t\t\t%s" % R1) | |
L1 = R2 | |
log.info("L1 R1: \t\t\t\t%s %s" % (L1, R1)) | |
log.info("Раунд 1") | |
log.debug("R1: \t\t\t\t%s" % R1) | |
r = join(peres(R1, rashirenie)) | |
log.debug("Расширение: \t\t\t%s" % r) | |
log.debug("\t\t\t\t\t⊕") | |
key1 = ''.join([str(i) for i in key1]) | |
log.debug("Ключ раунда 1: \t\t\t%s" % key1) | |
xo = xor(r, key1) | |
log.debug("\t\t\t\t%s" % xo) | |
s_bl = s_block(xo) | |
log.debug("\t\t\t\t%s" % s_bl) | |
rash = peres(s_bl, p_perestanov) | |
log.debug("Перестановка \t\t\t%s" % join(rash)) | |
log.debug("\t\t\t\t\t⊕") | |
L1 = join(L1) | |
log.debug("L1: \t\t\t\t%s" % L1) | |
L0 = xor(join(rash), L1) | |
log.debug("L0: \t\t\t\t%s" % L0) | |
R0 = R1 | |
log.info("L2 R2: \t\t\t\t%s %s" % (L0, R0)) | |
con_perest = peres(L0 + R0, con) | |
log.info("Конечная перестановка(IP-1): \t%s" % join(con_perest)) | |
return con_perest | |
def main(): | |
key1, key2 = gen_key() | |
zashifrovan = shifr(key1, key2) | |
log.info("\nЗашифрованный текст: \t\t%s\n" % join(zashifrovan)) | |
de = deshifr(zashifrovan, key1, key2) | |
log.info("\nДешифрованный текст: \t\t%s\n" % join(de)) | |
if join(text) == join(de): | |
log.info("Исходный текст: \t\t%s" % join(text)) | |
log.info("Зашифрованный текст: \t\t%s" % join(zashifrovan)) | |
log.info("Расшифрованный текст: \t\t%s" % join(de)) | |
if (__name__ == "__main__"): | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment