示例#1
0
文件: cli.py 项目: ssnd/ciphers
def decode(cipher: str, key: Union[int, str], input_file, output_file):
    """Use this command to decode the text (read from stdio or a file)
       using the available ciphers and display the decrypted text or write
       it to a file.
    """

    input_file_name = input_file.name if input_file is not None else None
    output_file_name = output_file.name if output_file is not None else None

    io_handler = IOHandler(input_file_name, output_file_name)

    input_text = io_handler.read()

    if cipher == "caesar":
        key = Caesar.check_key_type(key, int)
        c = Caesar(key=key)
        decrypted_str = c.decrypt(input_text)

    if cipher == "vigenere":
        v = Vigenere(key=key)
        decrypted_str = v.decrypt(input_text)

    if cipher == "vernam":
        if input_file is None:
            print(("An input file is required for the Vernam cipher"
                   " to work correctly. Exitting..."))
            sys.exit()
        vernam = Vernam(key=key)
        decrypted_str = vernam.decrypt(input_text)

    io_handler.write(decrypted_str)
示例#2
0
class vigenereFrame(frameTemplate):
    def __init__(self):
        super().__init__()
        self.vg = Vigenere()
        self.initUI()

    def initUI(self):
        super().initUI()

        self.definition.insertPlainText(vigenere_txt)

        self.cb_method.addItem("Encrypt")
        self.cb_method.addItem("Decrypt")
        self.cb_method.currentIndexChanged.connect(self.selectionChange)
        self.btn_encrypt.clicked.connect(self.encrypt)

        self.label_key = QLabel()
        self.label_key.setText('Ključ:')

        self.key_input = QLineEdit(self)

        regex = QtCore.QRegExp("^[a-zA-Z]+$")
        validator = QtGui.QRegExpValidator(regex, self.key_input)
        self.key_input.setValidator(validator)

        text_validator = QtGui.QRegExpValidator(regex, self.plaintext)
        self.plaintext.setValidator(validator)

        self.checkbox_autokey = QCheckBox("Autokey")
        self.checkbox_autokey.setChecked(False)
        self.checkbox_autokey.stateChanged.connect(self.stateChange)

        self.encryption_v_box.addWidget(self.label_key)
        self.encryption_v_box.addWidget(self.key_input)
        self.encryption_v_box.addWidget(self.checkbox_autokey)

    def selectionChange(self, index):
        if (self.cb_method.itemText(index) == "Encrypt"):
            self.label_plaintext.setText("Plaintext:")
            self.label_ciphertext.setText("Ciphertext:")

            if(self.checkbox_autokey.isChecked()):
                self.btn_encrypt.clicked.disconnect()
                self.btn_encrypt.clicked.connect(self.autokeyEncrypt)
            else:
                self.btn_encrypt.clicked.disconnect()
                self.btn_encrypt.clicked.connect(self.encrypt)

            self.btn_encrypt.setText("Encrypt")
            self.plaintext.clear()
            self.ciphertext.clear()

        elif (self.cb_method.itemText(index) == "Decrypt"):
            self.label_plaintext.setText("Ciphertext:")
            self.label_ciphertext.setText("Plaintext:")

            if(self.checkbox_autokey.isChecked()):
                self.btn_encrypt.clicked.disconnect()
                self.btn_encrypt.clicked.connect(self.autokeyDecrypt)
            else:
                self.btn_encrypt.clicked.disconnect()
                self.btn_encrypt.clicked.connect(self.decrypt)

            self.btn_encrypt.setText("Decrypt")
            self.plaintext.clear()
            self.ciphertext.clear()

    def stateChange(self):
        self.btn_encrypt.clicked.disconnect()

        if(self.checkbox_autokey.isChecked()):
            if (str(self.cb_method.currentText()) == "Encrypt"):
                self.btn_encrypt.clicked.connect(self.autokeyEncrypt)
            else:
                self.btn_encrypt.clicked.connect(self.autokeyDecrypt)

        else:
            if (str(self.cb_method.currentText()) == "Encrypt"):
                self.btn_encrypt.clicked.connect(self.encrypt)
            else:
                self.btn_encrypt.clicked.connect(self.decrypt)

    def encrypt(self):
        if(self.validateKey()):
            text = self.vg.encrypt(self.plaintext.text(), self.key_input.text())
            self.ciphertext.setText(text)
            self.key_input.setStyleSheet('QLineEdit { border-color: #1e1e1e }')

    def decrypt(self):
        if(self.validateKey()):
            text = self.vg.decrypt(self.plaintext.text(), self.key_input.text())
            self.ciphertext.setText(text)
            self.key_input.setStyleSheet('QLineEdit { border-color: #1e1e1e }')

    def autokeyEncrypt(self):
        if(self.validateKey()):
            text = self.vg.autokeyEncrypt(self.plaintext.text(), self.key_input.text())
            self.ciphertext.setText(text)
            self.key_input.setStyleSheet('QLineEdit { border-color: #1e1e1e }')

    def validateKey(self):
        if(self.key_input.text() == ''):
            self.key_input.setPlaceholderText("Key is required")
            self.key_input.setStyleSheet('QLineEdit { border-color: #EC0505 }')
            return False

        return True

    def autokeyDecrypt(self):
        if(self.validateKey()):
            text = self.vg.autokeyDecrypt(self.plaintext.text(), self.key_input.text())
            self.ciphertext.setText(text)
            self.key_input.setStyleSheet('QLineEdit { border-color: #1e1e1e }')
示例#3
0
# K2
from ciphers.vigenere import VigenereTableau, Vigenere

kryptos_alphabet = "KRYPTOSABCDEFGHIJLMNQUVWXZ"
vtableau = VigenereTableau(alphabet=kryptos_alphabet, row_fill=0, col_fill=4)
cipher = Vigenere(tableau=vtableau)

ciphertext = "VFPJUDEEHZWETZYVGWHKKQETGFQJNCEGGWHKK?DQMCPFQZDQMMIAGPFXHQRLGTIMVMZJANQLVKQEDAGDVFRPJUNGEUNAQZGZLECGYUXUEENJTBJLBQCRTBJDFHRRYIZETKZEMVDUFKSJHKFWHKUWQLSZFTIHHDDDUVH?DWKBFUFPWNTDFIYCUQZEREEVLDKFEZMOQQJLTTUGSYQPFEUNLAVIDXFLGGTEZ?FKZBSFDQVGOGIPUFXHHDRKFFHQNTGPUAECNUVPDJMQCLQUMUNEDFQELZZVRRGKFFVOEEXBDMVPNFQXEZLGREDNQFMPNZGLFLPMRJQYALMGNUVPDXVKPDQUMEBEDMHDAFMJGZNUPLGESWJLLAETG"
keyword = "ABSCISSA"
plaintext = cipher.decrypt(ciphertext, keyword)

print "K2"
print ciphertext
print plaintext
示例#4
0
def test_vigenere_algorithm_dec():
    c = Vigenere(key="42")
    assert c.decrypt("bcdø") == "PSRø"
示例#5
0
def test_vigenere_algorithm_dec_enc(a):
    c = Vigenere(key="42")
    assert c.encrypt(c.decrypt(a)) == a