def decrypt(table, text): numbers = '' text = Polybius.encrypt(table, text) a, b = text[:len(text) / 2], text[len(text) / 2:] numbers = ''.join(a[i] + b[i] for i in range(len(a))) return Polybius.decrypt(table, numbers)
def encrypt(table, words): string = table cipher_row, cipher_col = '', '' for ch in words.upper(): for row in range(len(table)): if ch in table[row]: cipher_row += str(row + 1) cipher_col += str((table[row].index(ch) + 1)) return Polybius.decrypt(table, cipher_row + cipher_col)
return numbers if __name__ == '__main__': parser = optparse.OptionParser(version=__version__, usage='Usage: %prog [options] [args]', description='Trifid cipher is a cipher which combines the Polybius square with transposition, and uses fractionation to achieve diffusion.') parser.add_option('-d', dest='decrypt', action='store_true', default=False) parser.add_option('-k', dest='key', action='store', default='', type='string') parser.add_option('-V', dest='verbose', action='store_true', default=False) parser.add_option('-f', dest='file', action='store', default=False) (options, args) = parser.parse_args() array = Polybius.generate_array(key=options.key) Polybius.display_array(array) if options.file: print '' print ' IN> ' + options.file try: file_stream = codecs.open(options.file, 'r', 'dbcs') file_output = codecs.open('_' + options.file, 'w', 'dbcs') for line in file_stream: if options.decrypt: file_output.write(Polybius.decode( transform_back( Polybius.encode(line, array)), array))
a, b = text[:len(text) / 2], text[len(text) / 2:] numbers = ''.join(a[i] + b[i] for i in range(len(a))) return Polybius.decrypt(table, numbers) if __name__ == '__main__': # 本例推算见 http://en.wikipedia.org/wiki/Bifid_cipher # 初始化棋盘(也可以用 polybius.generate_table 来生成一个随机的) table = [['B', 'G', 'W', 'K', 'Z'], ['Q', 'P', 'N', 'D', 'S'], ['I', 'O', 'A', 'X', 'E'], ['F', 'C', 'L', 'U', 'M'], ['T', 'H', 'Y', 'V', 'R']] # 输出棋盘 Polybius.print_table(table) # 密文 text = "F L E E A T O N C E" # 使用棋盘加密字符串 ciphertext = encrypt(table, text) # 密文 print('密文: ' + ciphertext) # 解密字符串 print('解密: ' + decrypt(table, ciphertext))
print '' print 'Szyfr Delastelle\'a' print '' print ' Autor: ' + __author__ if options.decrypt: print ' Tryb: Deszyfrowanie' else: print ' Tryb: Szyfrowanie' if options.verbose: print 'Tryb verbose: Tak' else: print 'Tryb verbose: Nie' array = Polybius.generate_array(key=options.key) print ' Szachownica:' Polybius.display_array(array) if options.file: print '' print ' IN> ' + options.file try: file_stream = codecs.open(options.file, 'r', 'dbcs') file_output = codecs.open('_' + options.file, 'w', 'dbcs') for line in file_stream: if options.decrypt: file_output.write(Polybius.decode( transform_back(
numbers = ''.join(a[i] + b[i] for i in range(len(a))) return Polybius.decrypt(table, numbers) if __name__ == '__main__': # 本例推算见 http://en.wikipedia.org/wiki/Bifid_cipher # 初始化棋盘(也可以用 polybius.generate_table 来生成一个随机的) table = [['B', 'G', 'W', 'K', 'Z'], ['Q', 'P', 'N', 'D', 'S'], ['I', 'O', 'A', 'X', 'E'], ['F', 'C', 'L', 'U', 'M'], ['T', 'H', 'Y', 'V', 'R']] # 输出棋盘 Polybius.print_table(table) # 密文 text = "F L E E A T O N C E" # 使用棋盘加密字符串 ciphertext = encrypt(table, text) # 密文 print('密文: ' + ciphertext) # 解密字符串 print('解密: ' + decrypt(table, ciphertext))