SHARE
TWEET

Untitled

a guest Mar 26th, 2012 2,888 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #-*- coding: UTF-8 -*-
  2.  
  3. # формирование словаря кода
  4. def form_dict():
  5.     d = {}
  6.     iter = 0
  7.     for i in range(0,127):
  8.         d[iter] = chr(i)
  9.         iter = iter +1
  10.     return d
  11.  
  12. # кодируем слова в буквы  
  13. def encode_val(word):
  14.     list_code = []
  15.     lent = len(word)
  16.    
  17.     d = form_dict() # получаем словарь кода
  18.    
  19.     for w in range(lent):
  20.         for value in d:
  21.             if word[w] == d[value]:
  22.                list_code.append(value)
  23.     return list_code
  24.  
  25. # компаратор 2-х списков
  26. def comparator(value, key):
  27.     len_key = len(key)
  28.     dic = {}
  29.     iter = 0
  30.     full = 0
  31.     for i in value:
  32.         dic[full] = [i,key[iter]]
  33.         full = full + 1
  34.         iter = iter +1
  35.         if (iter >= len_key):
  36.             iter = 0
  37.  
  38.     return dic  
  39.  
  40. # finish full encode  
  41. def full_encode(value, key):
  42.  
  43.     dic = comparator(value, key)
  44.     print 'Compare full encode',dic
  45.  
  46.     lis = []
  47.     d = form_dict()
  48.  
  49.     for v in dic:
  50.         go = (dic[v][0]+dic[v][1]) % len(d)
  51.         lis.append(go)
  52.     return lis
  53.  
  54.  
  55.    
  56. ##### DECODER
  57.  
  58. # decode to numeric values
  59. def full_decode(value, key):
  60.  
  61.     dic = comparator(value, key)
  62.    
  63.     print 'Deshifre=', dic
  64.     d = form_dict() # получаем словарь кода
  65.  
  66.     lis =[]
  67.     for v in dic:
  68.         go = (dic[v][0]-dic[v][1]+len(d)) % len(d)
  69.         lis.append(go)
  70.     return lis
  71.    
  72.  
  73. def decode_val(list_in):
  74.  
  75.     list_code = []
  76.     lent = len(list_in)
  77.  
  78.     d = form_dict() # получаем словарь кода
  79.    
  80.     for i in range(lent):
  81.         for value in d:
  82.             if list_in[i] == value:
  83.                list_code.append(d[value])
  84.     return list_code
  85.  
  86.  
  87. if __name__ == "__main__":
  88.  
  89.     word = 'Hello world'
  90.     key = 'key'
  91.    
  92.     print 'Слово ', word
  93.     print '', key
  94.     print form_dict()
  95.     # Закодировали буквы в цифры
  96.     key_encoded = encode_val(key)
  97.     value_encoded = encode_val(word)
  98.  
  99.     print 'Value=',value_encoded
  100.     print 'Key=', key_encoded
  101.  
  102.     # сдвигаем
  103.     shifre = full_encode(value_encoded, key_encoded)
  104.     print 'Шифр=', ''.join(decode_val(shifre))
  105.  
  106.     decoded = full_decode(shifre, key_encoded)
  107.     print 'Decode list=', decoded
  108.     decode_word_list = decode_val(decoded)
  109.     print 'Word=',''.join(decode_word_list)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top