Verified Commit 3632ce37 authored by Jon Felix Germinian's avatar Jon Felix Germinian
Browse files

add affine

parent 4e4416c4
class Affine:
def egcd(self, a, b):
x, y, u, v = 0, 1, 1, 0
while a != 0:
q, r = b//a, b % a
m, n = x-u*q, y-v*q
b, a, x, y, u, v = a, r, u, v, m, n
gcd = b
return gcd, x, y
def modinv(self, a, m):
gcd, x, y = self.egcd(a, m)
if gcd != 1:
return None # modular inverse does not exist
else:
return x % m
def process_text(self, text):
text = list([val for val in text.upper() if val.isalpha()])
return ''.join(text)
def encrypt(self, plaintext, key):
return ''.join([chr(((key[0]*(ord(t) - ord('A')) + key[1]) % 26) + ord('A')) for t in self.process_text(plaintext)])
def decrypt(self, ciphertext, key):
return ''.join([chr(((self.modinv(key[0], 26)*(ord(c) - ord('A') - key[1])) % 26) + ord('A')) for c in ciphertext])
# text = "nakano miku"
# key = [7, 20]
# a = Affine()
# e = a.encrypt(text, key)
# print(e)
# print(a.decrypt(e, key))
\ No newline at end of file
......@@ -14,6 +14,7 @@ from vigenere import Vigenere
from full_vigenere import FullVigenere
from running_vigenere import RunningVigenere
from playfair import Playfair
from affine import Affine
from util import plaintext5
......@@ -76,6 +77,10 @@ class Ui_MainWIndow(object):
self.playfair.setGeometry(QtCore.QRect(0, 130, 191, 31))
self.playfair.setObjectName("playfair")
self.affine = QtWidgets.QRadioButton(self.cipherType)
self.affine.setGeometry(QtCore.QRect(0, 160, 191, 31))
self.affine.setObjectName("playfair")
self.encryptButton = QtWidgets.QPushButton(self.cipherType)
self.encryptButton.setGeometry(QtCore.QRect(10, 200, 121, 41))
self.encryptButton.setObjectName("encryptButton")
......@@ -161,6 +166,7 @@ class Ui_MainWIndow(object):
self.vigenereFull.setText(_translate("MainWIndow", "Full Vigenere"))
self.vigenereRunning.setText(_translate("MainWIndow", "Running Vigenere"))
self.playfair.setText(_translate("MainWIndow", "Playfair"))
self.affine.setText(_translate("MainWIndow", "Affine"))
self.encryptButton.setText(_translate("MainWIndow", "Encrypt"))
self.decryptButton.setText(_translate("MainWIndow", "Decrypt"))
self.uploadFile.setText(_translate("MainWIndow", "Upload File"))
......@@ -205,6 +211,20 @@ class Ui_MainWIndow(object):
elif self.playfair.isChecked():
pf = Playfair()
ciphertext = pf.encrypt(self.inputBox.toPlainText(), key)
elif self.affine.isChecked():
a = Affine()
key = key.split(",")
if len(key) < 2:
print("key invalid")
print(key)
ciphertext = ''
pass
else :
key1 = [int(key[0]), int(key[1])]
ciphertext = a.encrypt(self.inputBox.toPlainText(), key1)
self.output = ciphertext
self.outputBox.setText(ciphertext)
......@@ -229,6 +249,19 @@ class Ui_MainWIndow(object):
pf = Playfair()
plaintext = pf.decrypt(self.inputBox.toPlainText(), key)
elif self.affine.isChecked():
a = Affine()
key = key.split(",")
if len(key) < 2:
print("key invalid")
print(key)
plaintext = ''
pass
else :
key1 = [int(key[0]), int(key[1])]
plaintext = a.decrypt(self.inputBox.toPlainText(), key1)
self.output = plaintext
self.outputBox.setText(plaintext)
self.plainButton.setChecked(True)
......
......@@ -5,12 +5,3 @@ alphabets = 'HZOIDWBXMRYGFQPTEUSLVACKJN'
class FullVigenere(Vigenere):
def __init__(self):
self.final_table = [alphabets[i:]+alphabets[:i] for i in range(len(alphabets))]
# text = 'nakano miku is so cute'
# key = 'holo'
# fv = FullVigenere()
# ct = fv.encrypt(text, key)
# print(ct)
# print(fv.decrypt(ct,key))
\ No newline at end of file
......@@ -111,12 +111,4 @@ class Playfair:
def process_text(self, text):
text = list([val for val in text.upper() if val.isalpha()])
return ''.join(text)
# pf = Playfair()
# key = 'nakanomiku'
# t = pf.encrypt("takahashi rie is the best seiyuu", key)
# print(t)
# print(pf.decrypt(t, key))
return ''.join(text)
\ No newline at end of file
......@@ -9,13 +9,4 @@ class RunningVigenere(Vigenere):
return super().encrypt(plaintext, self.key_full)
def decrypt(self, ciphertext):
return super().decrypt(ciphertext, self.key_full)
# text = 'nakano miku is so cute'
# rv = RunningVigenere()
# ct = rv.encrypt(text)
# print(ct)
# print(rv.decrypt(ct))
return super().decrypt(ciphertext, self.key_full)
\ No newline at end of file
......@@ -39,18 +39,4 @@ class Vigenere:
text = list([val for val in text.upper() if val.isalpha()])
key = list([val for val in key.upper() if val.isalpha()])
return text, key
# text = "nakano miku"
# key = "holo"
# vigenere = Vigenere()
# ciphertext = vigenere.encrypt(text,key)
# plaintext = vigenere.decrypt(ciphertext,key)
# print(text)
# print()
# print(ciphertext)
# print(plaintext)
return text, key
\ No newline at end of file
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