Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # -*-coding: utf-8 -*-
- # алгоритм гаммирования: http://altaev-aa.narod.ru/security/XOR.html
- from itertools import cycle
- alpha = {"А":1, "Б":2, "В":3, "Г":4, "Д":5, "Е":6, "Ё":7, "Ж":8, "З":9, "И":10, "Й":11, "К":12, "Л":13, "М":14, "Н":15, "О":16, "П":17, "Р":18, "С":19, "Т":20, "У":21, "Ф":22, "Х":23, "Ц":24, "Ч":25, "Ш":26, "Щ":27, "Ъ":28, "Ы":29, "Ь":30, "Э":31, "Ю":32, "Я":33, " ":34, "0":35, "1":36, "2":37, "3":38, "4":39, "5":40, "6":41, "7":42, "8":43, "9":44,
- "A":45, "B":46, "C":47, "D":48, "E":49, "F":50, "G":51, "H":52, "I":53,
- "J":54, "K":55, "L":56, "M":57, "N":58, "O":59, "P":60, "Q":61, "R":62,
- "S":63, "T":64, "U":65, "V":66, "W":67, "X":68, "Y":69, "Z":70, ",":71,
- ".":72, "!":73, "?":74 }
- alpha2 = {1:"А", 2:"Б", 3:"В", 4:"Г", 5:"Д", 6:"Е", 7:"Ё", 8:"Ж", 9:"З", 10:"И", 11:"Й", 12:"К", 13:"Л", 14:"М", 15:"Н", 16:"О", 17:"П", 18:"Р", 19:"С", 20:"Т", 21:"У", 22:"Ф", 23:"Х", 24:"Ц", 25:"Ч", 26:"Ш", 27:"Щ", 28:"Ъ", 29:"Ы", 30:"Ь", 31:"Э", 32:"Ю", 33:"Я", 34:" ", 35:"0", 36:"1", 37:"2", 38:"3", 39:"4", 40:"5", 41:"6", 42:"7", 43:"8", 44:"9",
- 45:"A", 46:"B", 47:"C", 48:"D", 49:"E", 50:"F", 51:"G", 52:"H", 53:"I",
- 54:"J", 55:"K", 56:"L", 57:"M", 58:"N", 59:"O", 60:"P", 61:"Q", 62:"R",
- 63:"S", 64:"T", 65:"U", 66:"V", 67:"W", 68:"X", 69:"Y", 70:"Z", 71:",",
- 72:".", 73:"!", 74:"?" }
- class Gamma:
- def __init__(self):
- self.N = len(alpha)
- def crypt(self, source, g_seq):
- sum_ = ''
- crypt_sum = []
- crypted = ''
- source = source.upper()
- g_seq = g_seq.upper()
- for (i,j) in zip(source, cycle(g_seq)):
- if not i:
- break
- sum_ = (alpha[i] + alpha[j])%self.N
- if sum_ == 0:
- sum_ = self.N
- crypt_sum.append(str(sum_))
- for c in crypt_sum:
- crypted+=alpha2[int(c)]
- return crypted.lower()
- def decrypt(self, crypted_msg, g_seq):
- crypted_msg = crypted_msg.upper()
- g_seq = g_seq.upper()
- pre_dec_str = ''
- converted_str = []
- decrypted_str = ''
- for (i,j) in zip(crypted_msg, cycle(g_seq)):
- if not i:
- break
- pre_dec_str = (alpha[i] - alpha[j] + self.N)%self.N
- if pre_dec_str == 0:
- pre_dec_str = self.N
- converted_str.append(str(pre_dec_str))
- for c in converted_str:
- decrypted_str +=alpha2[int(c)]
- return decrypted_str.lower()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement