Commit b31ccfaf authored by Linus Jahn's avatar Linus Jahn 🍙

Add Vigenere

parent 49e1253a
#!/usr/bin/env python3
import sys
from PySide2.QtCore import QObject, Signal, Slot, Property, QUrl
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_=-+{}[]\"|\'/?.>,<~` ÄÜÖäüö°^°âêôẑûî"
class Backend(QObject):
def __init__(self):
QObject.__init__(self)
@staticmethod
def translate(input, key, encrypt = True):
if not input or not key:
return ""
i = 0
output = ""
for c in input:
if encrypt:
output += ALPHABET[(ALPHABET.index(c) + ALPHABET.index(key[i])) % len(ALPHABET)]
else:
output += ALPHABET[(ALPHABET.index(c) - ALPHABET.index(key[i])) % len(ALPHABET)]
i = (i + 1) % len(key)
return output
@Slot(str, str)
def encrypt(self, message, key):
return self.translate(message, key)
@Slot(str, str)
def decrypt(self, cipher, key):
return self.translate(cipher, key, encrypt = False)
#self.inputVal = ""
#self.keyVal = ""
#self.resultVal = ""
#self.encrypt = True
#def getInput(self) -> str:
#return self.inputVal
#def setInput(self, input: str):
#self.inputVal = input
#self.inputChanged.emit()
#def getKey(self) -> str:
#return self.keyVal
#def setKey(self, key: str):
#self.keyVal = key
#self.keyChanged.emit()
#def getEncrypt(self) -> bool:
#return self.encryptVal
#def setEncrypt(self, encrypt: bool):
#self.encryptVal = encrypt
#self.encryptChanged.emit()
#def getResult(self) -> str:
#return self.resultVal
#def setResult(self, result: str):
#self.resultVal = result
#self.resultChanged.emit()
#inputChanged = Signal()
#input = Property(str, getInput, setInput, notify=inputChanged)
#keyChanged = Signal()
#key = Property(str, getKey, setKey, notify=keyChanged)
#encryptChanged = Signal()
#encrypt = Property(bool, getEncrypt, setEncrypt, notify=encryptChanged)
#resultChanged = Signal()
#result = Property(str, getResult, setResult, notify=resultChanged)
if __name__ == "__main__":
backend = Backend()
app = QGuiApplication(sys.argv);
engine = QQmlApplicationEngine();
engine.rootContext().setContextProperty("backend", backend);
engine.load("main.qml");
sys.exit(app.exec_());
import QtQuick 2
import QtQuick.Controls 2 as Controls
import QtQuick.Layouts 1
Controls.ApplicationWindow {
visible: true
Column {
// leftPadding: 5
// rightPadding: 5
// topPadding: 5
// bottomPadding: 5
anchors.fill: parent
Controls.Switch {
id: encryptSwitch
}
Controls.Label {
text: "Eingabe:"
}
Controls.TextArea {
id: inputField
width: 500
height: 100
text: backend.input
}
Controls.Label {
text: "Schlüssel:"
}
Controls.TextField {
id: keyField
text: backend.key
}
Controls.Label {
text: "Ausgabe:"
}
Controls.Label {
text: {
if (encryptSwitch.checked) {
return backend.encrypt(inputField.text, keyField.text);
} else {
return backend.decrypt(inputField.text, keyField.text);
}
}
}
}
}
#!/usr/bin/env python3
"""
Vigene-Verschluesselung
MIT License
Copyright (C) 2019 Linus Jahn <lnj@kaidan.im>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import tkinter as tk
from vigenere import Vigenere
class VigenereApplication(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.buildWindow()
self.model = Vigenere()
def buildWindow(self):
tk.Label(self, text="Eingabe:").pack()
self.inputField = tk.Text(self)
self.inputField.pack()
tk.Label(self, text="Schluessel:").pack()
self.keyField = tk.Entry(self)
self.keyField.pack()
self.encryptVar = tk.IntVar()
tk.Radiobutton(self,
text = "Verschluesseln",
command = self.updateOutput,
variable = self.encryptVar,
value = 1).pack()
tk.Radiobutton(self,
text = "Entschlueseln",
variable = self.encryptVar,
value = 0,
command = self.updateOutput).pack()
tk.Button(self,
text="Verschluesseln/Entschluesseln",
command=self.updateOutput).pack()
tk.Label(self, text="Ausgabe:").pack()
self.outputVar = tk.StringVar()
self.outputLabel = tk.Message(self, textvariable=self.outputVar, width=80)
self.outputLabel.pack()
def updateOutput(self):
msg = self.inputField.get("1.0", "end-1c")
key = self.keyField.get()
output = self.model.translate(msg, key, bool(self.encryptVar.get()))
self.outputVar.set(output)
if __name__ == "__main__":
root = tk.Tk()
app = VigenereApplication(master=root)
app.mainloop()
#!/usr/bin/env python3
"""
Vigene-Verschluesselung
MIT License
Copyright (C) 2019 Linus Jahn <lnj@kaidan.im>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_=-+{}[]\"|\'/?.>,<~` ÄÜÖäüö°^°âêôẑûî"
class Vigenere:
def translate(self, input, key, encrypt = True):
i = 0
output = ""
for c in input:
if encrypt:
output += ALPHABET[(ALPHABET.index(c) + ALPHABET.index(key[i])) % len(ALPHABET)]
else:
output += ALPHABET[(ALPHABET.index(c) - ALPHABET.index(key[i])) % len(ALPHABET)]
i = (i + 1) % len(key)
return output
def encrypt(self, message, key):
return self.translate(message, key)
def decrypt(self, cipher, key):
return self.translate(cipher, key, encrypt = False)
if __name__ == "__main__":
print(encrypt("1234", "Hallo"))
print(encrypt("Guten Tag, mein Name ist Marcel Davis und wir gehen erst wieder, wenn der Anschluss läuft!", "Ich habe einen SEHR guhten Schlüssel GeAählt111!!t!TT!1elf"))
print(decrypt("SGFWEEOMQ", "KEY"))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment