from __future__ import print_functionimport mathdef main():message = input('Enter message: ')key = int(input('Enter key [2-%s]: ' % (len(message) - 1)))mode = input('Encryption/Decryption [e/d]: ')if mode.lower().startswith('e'):text = encryptMessage(key, message)elif mode.lower().startswith('d'):text = decryptMessage(key, message)# Append pipe symbol (vertical bar) to identify spaces at the end.print('Output:\n%s' %(text + '|'))def encryptMessage(key, message):""">>> encryptMessage(6, 'Harshil Darji')'Hlia rDsahrij'"""cipherText = [''] * keyfor col in range(key):pointer = colwhile pointer < len(message):cipherText[col] += message[pointer]pointer += keyreturn ''.join(cipherText)def decryptMessage(key, message):""">>> decryptMessage(6, 'Hlia rDsahrij')'Harshil Darji'"""numCols = math.ceil(len(message) / key)numRows = keynumShadedBoxes = (numCols * numRows) - len(message)plainText = [""] * numColscol = 0; row = 0;for symbol in message:plainText[col] += symbolcol += 1if (col == numCols) or (col == numCols - 1) and (row >= numRows - numShadedBoxes):col = 0row += 1return "".join(plainText)if __name__ == '__main__':import doctestdoctest.testmod()main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。