Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- import os, sys
- ALPHA = range(256) # наш алфвит -- вся ASCII-таблица.
- KEY = 1 # 1 % 33
- def get_symbol(filename):
- with open(filename, 'rb') as f:
- while True:
- b = f.read(1)
- if b:
- yield b
- else:
- break
- def _key():
- while True:
- for k in ALPHA:
- yield k
- x = ALPHA.pop() # ключ -- сдвиг алфавита на 1
- ALPHA.insert(0, x)
- _KEY = _key()
- def vigenere(symbol, is_decode):
- code = ord(symbol)
- if is_decode:
- new_symbol = (code - _KEY.next() + 256) % 256
- else:
- new_symbol = (code + _KEY.next()) % 256
- if new_symbol > 255:
- new_symbol = abs(255 - new_symbol)
- if new_symbol < 0:
- new_symbol = abs(255 + new_symbol)
- return chr(new_symbol)
- if __name__ == '__main__':
- filename = os.path.abspath(sys.argv[1])
- decode = False
- if len(sys.argv) > 2:
- decode = sys.argv[2] == '-d'
- for ch in get_symbol(filename):
- sys.stdout.write(vigenere(ch, decode))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement