Advertisement
j7sx

gamma_sequence

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