示例#1
0
文件: cli.py 项目: ssnd/ciphers
def encode(cipher: str, key: Union[int, str], input_file, output_file):
    """Use this command to encode the text (read from stdio or a file)
       using the available ciphers and display the encrypted text or write
       it to a file.
    """
    input_path = input_file.name if input_file is not None else None
    output_path = output_file.name if output_file is not None else None

    io_handler = IOHandler(input_path, output_path)

    input_text = io_handler.read()

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

    if cipher == "vigenere":
        vigenere = Vigenere(key=key)
        encrypted_str = vigenere.encrypt(input_text)

    if cipher == "vernam":
        if output_file is None:
            print(("An output file is required for the Vernam cipher"
                   " to work correctly. Exitting..."))
            sys.exit()
        vernam = Vernam(key=key)
        encrypted_str = vernam.encrypt(input_text)

    io_handler.write(encrypted_str)
示例#2
0
def caesar():
    if request.method == 'POST':
        key = int(request.form['key'])
        message = request.form['message']
        caesar = Caesar(message, key)
        user_option = request.form['option']

        if user_option == 'encrypt':
            new_message = caesar.encrypt()
        elif user_option == 'decrypt':
            new_message = caesar.decrypt()
        return render_template("caesar.html", new_message=new_message)

    return render_template("caesar.html")
示例#3
0
class caesarFrame(frameTemplate):
    def __init__(self):
        super().__init__()
        self.cs = Caesar()
        self.initUI()

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

        self.definition.insertPlainText(caesar_txt)

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

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

        self.btn_encrypt.clicked.connect(self.encrypt)

    def selectionChange(self, index):
        self.btn_encrypt.clicked.disconnect()

        if (self.cb_method.itemText(index) == "Encrypt"):
            self.label_plaintext.setText("Plaintext:")
            self.label_ciphertext.setText("Ciphertext:")
            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:")
            self.btn_encrypt.clicked.connect(self.decrypt)
            self.btn_encrypt.setText("Decrypt")
            self.plaintext.clear()
            self.ciphertext.clear()

    def encrypt(self):
        text = self.cs.encrypt(self.plaintext.text())
        self.ciphertext.setText(text)

    def decrypt(self):
        text = self.cs.decrypt(self.plaintext.text())
        self.ciphertext.setText(text)
示例#4
0
# -*- coding: utf-8 -*-
import sys

sys.path.insert(0, '..')

from ciphers.caesar import Caesar

txt_in = input('Texto a ser codificado: ')
chave = input('Chave:')
if chave:
    chave = int(chave)
else:
    chave = 3
cifra = Caesar()
cifrado = cifra.encrypt(txt_in, chave)
print('Texto cifrado: ', cifrado)
print('  Texto plano: ', cifra.decrypt(cifrado, chave))
示例#5
0
def test_caesar_algorithm_enc():
    c = Caesar(key=1)
    assert c.encrypt("abc\n") == "bcd\n"
示例#6
0
def test_caesar_algorithm_dec_enc(a):
    c = Caesar(key=42)
    assert c.encrypt(c.decrypt(a)) == a
示例#7
0
def write_file(name, string):
    file = open(name, 'w')
    file.write(string)
    file.close()


print('Encrypting files...')

plain_list = read_file('caesar_plain.txt')
output = ''
for key in [3, 6, 12]:
    output += 'Key = ' + str(key) + '\n'
    for plain in plain_list:
        caesar = Caesar(plain, key)
        output += caesar.encrypt() + '\n'
    output += '\n'
write_file('caesar_cipher.txt', output)

plain_list = read_file('playfair_plain.txt')
output = ''
for key in ['rats', 'archangel']:
    output += 'Key = ' + key + '\n'
    for plain in plain_list:
        playfair = Playfair(plain, key)
        output += playfair.encrypt() + '\n'
    output += '\n'
write_file('playfair_cipher.txt', output)

plain_list = read_file('hill_plain_2x2.txt')
output = ''