Last active
September 9, 2015 05:24
-
-
Save SalahAdDin/56142248cff3612df2bf to your computer and use it in GitHub Desktop.
First project for Crypto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fractions import gcd | |
from source import * | |
__author__ = 'tulipan' | |
def vigenere(decode, key, clear): | |
encrypted = [] | |
output = [] | |
clear = clear.replace(' ', '') | |
# clear = clear.rstrip() | |
stringLength = len(clear) | |
key = compute_key(key, clear) | |
keyList = text_transform(key, True) | |
stringList = text_transform(clear, False) | |
i = 0 | |
count = 0 | |
if decode: | |
while count < stringLength: | |
encrypted.append((stringList[i] - keyList[i]) % 26) | |
count += 1 | |
i += 1 | |
for n in encrypted: | |
output.append(alphabet[n]) | |
else: | |
while count < stringLength: | |
encrypted.append((stringList[i] + keyList[i]) % 26) | |
count += 1 | |
i += 1 | |
for n in encrypted: | |
output.append(alphabet[n]) | |
string = ''.join(output) | |
return string | |
def substitution(decode, key, clear): | |
encrypted = [] | |
output = [] | |
clear = clear.replace(' ', '') | |
# clear = clear.rstrip() | |
stringLength = len(clear) | |
try: | |
check_valid_key(key) | |
except: | |
return None | |
key = key.upper() | |
keyList = list(key) | |
if decode: | |
for n in clear: | |
number = keyList.index(n.upper()) | |
encrypted.append(number) | |
for n in encrypted: | |
output.append(alphabet[n]) | |
else: | |
for n in clear: | |
number = alphabet.index(n.upper()) | |
encrypted.append(number) | |
for n in encrypted: | |
output.append(keyList[n]) | |
string = ''.join(output) | |
return string | |
def affine(decode, a, b, clear): | |
encrypted = [] | |
output = [] | |
clear = clear.replace(' ', '') | |
# clear = clear.rstrip() | |
stringLength = len(clear) | |
stringList = text_transform(clear, False) | |
i = 0 | |
count = 0 | |
if decode: | |
if gcd(a, 26) == 1: | |
# m = gcd(a, 26) | |
m = fmi(a, 26) | |
while count < stringLength: | |
encrypted.append((m * (stringList[i] - b)) % 26) | |
count += 1 | |
i += 1 | |
for n in encrypted: | |
output.append(alphabet[n]) | |
elif gcd(a, 26) != 1: | |
print("The key is wrong!") | |
else: | |
if gcd(a, 26) == 1: | |
while count < stringLength: | |
encrypted.append((a * stringList[i] + b) % 26) | |
count += 1 | |
i += 1 | |
for n in encrypted: | |
output.append(alphabet[n]) | |
elif gcd(a, 26) != 1: | |
print("The key is wrong!") | |
string = ''.join(output) | |
return string | |
def caesar(decode, key, clear): | |
encrypted = [] | |
output = [] | |
clear = clear.replace(' ', '') | |
# clear = clear.rstrip() | |
stringLength = len(clear) | |
stringList = text_transform(clear, False) | |
i = 0 | |
count = 0 | |
if decode: | |
while count < stringLength: | |
encrypted.append((stringList[i] - key) % 26) | |
count += 1 | |
i += 1 | |
for n in encrypted: | |
output.append(alphabet[n]) | |
else: | |
while count < stringLength: | |
encrypted.append((stringList[i] + key) % 26) | |
count += 1 | |
i += 1 | |
for n in encrypted: | |
output.append(alphabet[n]) | |
string = ''.join(output) | |
return string | |
def multiply(decode, a, clear): | |
encrypted = [] | |
output = [] | |
clear = clear.replace(' ', '') | |
# clear = clear.rstrip() | |
stringLength = len(clear) | |
stringList = text_transform(clear, False) | |
i = 0 | |
count = 0 | |
if decode: | |
if gcd(a, 26) == 1: | |
# m = gcd(a, 26) | |
m = fmi(a, 26) | |
while count < stringLength: | |
encrypted.append((m * stringList[i]) % 26) | |
count += 1 | |
i += 1 | |
for n in encrypted: | |
output.append(alphabet[n]) | |
elif gcd(a, 26) != 1: | |
print("The key is wrong!") | |
else: | |
if gcd(a, 26) == 1: | |
while count < stringLength: | |
encrypted.append((a * stringList[i]) % 26) | |
count += 1 | |
i += 1 | |
for n in encrypted: | |
output.append(alphabet[n]) | |
elif gcd(a, 26) != 1: | |
print("The key is wrong!") | |
string = ''.join(output) | |
return string | |
def hill(decode, matrix, clear): | |
encrypted = [] | |
output = [] | |
clear = clear.replace(' ', '') | |
# clear = clear.rstrip() | |
stringLength = len(clear) | |
stringList = text_transform(clear, False) | |
if not invertible(matrix): | |
print("Non invertible matrix") | |
if stringLength % 2 != 0: | |
message = clear + 'X' | |
text_transform(message, False) | |
if decode: | |
matrix = inverse_matrix(matrix) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Form implementation generated from reading ui file 'maincrypto.ui' | |
# | |
# Created by: PyQt5 UI code generator 5.4.1 | |
# | |
# WARNING! All changes made in this file will be lost! | |
from PyQt5 import QtCore, QtGui, QtWidgets | |
try: | |
_fromUtf8 = QtCore.QString.fromUtf8 | |
except AttributeError: | |
def _fromUtf8(s): | |
return s | |
try: | |
_encoding = QtGui.QApplication.UnicodeUTF8 | |
def _translate(context, text, disambig): | |
return QtGui.QApplication.translate(context, text, disambig, _encoding) | |
except AttributeError: | |
def _translate(context, text, disambig): | |
return QtGui.QApplication.translate(context, text, disambig) | |
class Ui_MainCrypto(object): | |
def setupUi(self, MainCrypto): | |
MainCrypto.setObjectName("MainCrypto") | |
MainCrypto.resize(800, 600) | |
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) | |
sizePolicy.setHorizontalStretch(0) | |
sizePolicy.setVerticalStretch(0) | |
sizePolicy.setHeightForWidth(MainCrypto.sizePolicy().hasHeightForWidth()) | |
MainCrypto.setSizePolicy(sizePolicy) | |
MainCrypto.setMaximumSize(QtCore.QSize(800, 600)) | |
MainCrypto.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedStates)) | |
self.centralwidget = QtWidgets.QWidget(MainCrypto) | |
self.centralwidget.setMaximumSize(QtCore.QSize(800, 600)) | |
self.centralwidget.setObjectName("centralwidget") | |
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget) | |
self.verticalLayoutWidget.setGeometry(QtCore.QRect(600, 20, 171, 401)) | |
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget") | |
self.optionsLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget) | |
self.optionsLayout.setContentsMargins(0, 0, 0, 0) | |
self.optionsLayout.setObjectName("optionsLayout") | |
self.optionsBox = QtWidgets.QGroupBox(self.verticalLayoutWidget) | |
self.optionsBox.setEnabled(True) | |
self.optionsBox.setAlignment(QtCore.Qt.AlignCenter) | |
self.optionsBox.setFlat(False) | |
self.optionsBox.setCheckable(False) | |
self.optionsBox.setObjectName("optionsBox") | |
self.decodeCheckBox = QtWidgets.QCheckBox(self.optionsBox) | |
self.decodeCheckBox.setGeometry(QtCore.QRect(10, 30, 97, 22)) | |
self.decodeCheckBox.setCheckable(True) | |
self.decodeCheckBox.setChecked(False) | |
self.decodeCheckBox.setObjectName("decodeCheckBox") | |
self.algorithmsGroupBox = QtWidgets.QGroupBox(self.optionsBox) | |
self.algorithmsGroupBox.setGeometry(QtCore.QRect(10, 60, 141, 261)) | |
self.algorithmsGroupBox.setObjectName("algorithmsGroupBox") | |
self.gridLayoutWidget_2 = QtWidgets.QWidget(self.algorithmsGroupBox) | |
self.gridLayoutWidget_2.setGeometry(QtCore.QRect(0, 20, 141, 192)) | |
self.gridLayoutWidget_2.setObjectName("gridLayoutWidget_2") | |
self.gridLayout_2 = QtWidgets.QGridLayout(self.gridLayoutWidget_2) | |
self.gridLayout_2.setContentsMargins(0, 0, 0, 0) | |
self.gridLayout_2.setObjectName("gridLayout_2") | |
self.rsaRadioButton = QtWidgets.QRadioButton(self.gridLayoutWidget_2) | |
self.rsaRadioButton.setEnabled(False) | |
self.rsaRadioButton.setObjectName("rsaRadioButton") | |
self.gridLayout_2.addWidget(self.rsaRadioButton, 5, 0, 1, 1) | |
self.caesarRadioButton = QtWidgets.QRadioButton(self.gridLayoutWidget_2) | |
self.caesarRadioButton.setObjectName("caesarRadioButton") | |
self.gridLayout_2.addWidget(self.caesarRadioButton, 3, 0, 1, 1) | |
self.multiplyRadioButton = QtWidgets.QRadioButton(self.gridLayoutWidget_2) | |
self.multiplyRadioButton.setObjectName("multiplyRadioButton") | |
self.gridLayout_2.addWidget(self.multiplyRadioButton, 1, 0, 1, 1) | |
self.substitutionRadioButton = QtWidgets.QRadioButton(self.gridLayoutWidget_2) | |
self.substitutionRadioButton.setObjectName("substitutionRadioButton") | |
self.gridLayout_2.addWidget(self.substitutionRadioButton, 0, 0, 1, 1) | |
self.vigenereRadioButton = QtWidgets.QRadioButton(self.gridLayoutWidget_2) | |
self.vigenereRadioButton.setObjectName("vigenereRadioButton") | |
self.gridLayout_2.addWidget(self.vigenereRadioButton, 4, 0, 1, 1) | |
self.affineRadioButton = QtWidgets.QRadioButton(self.gridLayoutWidget_2) | |
self.affineRadioButton.setObjectName("affineRadioButton") | |
self.gridLayout_2.addWidget(self.affineRadioButton, 2, 0, 1, 1) | |
self.hillRadioButton = QtWidgets.QRadioButton(self.gridLayoutWidget_2) | |
self.hillRadioButton.setEnabled(False) | |
self.hillRadioButton.setObjectName("hillRadioButton") | |
self.gridLayout_2.addWidget(self.hillRadioButton, 6, 0, 1, 1) | |
self.okPushButton = QtWidgets.QPushButton(self.optionsBox) | |
self.okPushButton.setGeometry(QtCore.QRect(40, 360, 97, 26)) | |
self.okPushButton.setObjectName("okPushButton") | |
self.clearPushButton = QtWidgets.QPushButton(self.optionsBox) | |
self.clearPushButton.setGeometry(QtCore.QRect(40, 320, 97, 26)) | |
self.clearPushButton.setObjectName("clearPushButton") | |
self.optionsLayout.addWidget(self.optionsBox) | |
self.formLayoutWidget = QtWidgets.QWidget(self.centralwidget) | |
self.formLayoutWidget.setGeometry(QtCore.QRect(40, 20, 551, 609)) | |
self.formLayoutWidget.setObjectName("formLayoutWidget") | |
self.textLayout = QtWidgets.QVBoxLayout(self.formLayoutWidget) | |
self.textLayout.setContentsMargins(0, 0, 0, 0) | |
self.textLayout.setObjectName("textLayout") | |
self.groupInputTextBox = QtWidgets.QGroupBox(self.formLayoutWidget) | |
self.groupInputTextBox.setObjectName("groupInputTextBox") | |
self.verticalLayout = QtWidgets.QVBoxLayout(self.groupInputTextBox) | |
self.verticalLayout.setObjectName("verticalLayout") | |
self.inputTextLabel = QtWidgets.QLabel(self.groupInputTextBox) | |
self.inputTextLabel.setObjectName("inputTextLabel") | |
self.verticalLayout.addWidget(self.inputTextLabel) | |
self.inputTextEdit = QtWidgets.QTextEdit(self.groupInputTextBox) | |
self.inputTextEdit.setEnabled(True) | |
self.inputTextEdit.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedStates)) | |
self.inputTextEdit.setObjectName("inputTextEdit") | |
self.verticalLayout.addWidget(self.inputTextEdit) | |
self.keysGridLayout = QtWidgets.QGridLayout() | |
self.keysGridLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint) | |
self.keysGridLayout.setObjectName("keysGridLayout") | |
self.aKeyLineEdit = QtWidgets.QLineEdit(self.groupInputTextBox) | |
self.aKeyLineEdit.setObjectName("aKeyLineEdit") | |
self.keysGridLayout.addWidget(self.aKeyLineEdit, 1, 1, 1, 1) | |
self.bKeyLabel = QtWidgets.QLabel(self.groupInputTextBox) | |
self.bKeyLabel.setObjectName("bKeyLabel") | |
self.keysGridLayout.addWidget(self.bKeyLabel, 1, 2, 1, 1) | |
self.aKeyLabel = QtWidgets.QLabel(self.groupInputTextBox) | |
self.aKeyLabel.setObjectName("aKeyLabel") | |
self.keysGridLayout.addWidget(self.aKeyLabel, 1, 0, 1, 1) | |
self.keyLabel = QtWidgets.QLabel(self.groupInputTextBox) | |
self.keyLabel.setObjectName("keyLabel") | |
self.keysGridLayout.addWidget(self.keyLabel, 0, 0, 1, 1) | |
self.bKeyLineEdit = QtWidgets.QLineEdit(self.groupInputTextBox) | |
self.bKeyLineEdit.setObjectName("bKeyLineEdit") | |
self.keysGridLayout.addWidget(self.bKeyLineEdit, 1, 3, 1, 1) | |
self.keyTextEdit = QtWidgets.QLineEdit(self.groupInputTextBox) | |
self.keyTextEdit.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedStates)) | |
self.keyTextEdit.setObjectName("keyTextEdit") | |
self.keysGridLayout.addWidget(self.keyTextEdit, 0, 1, 1, 3) | |
self.verticalLayout.addLayout(self.keysGridLayout) | |
self.textLayout.addWidget(self.groupInputTextBox) | |
self.groupOutputTextBox = QtWidgets.QGroupBox(self.formLayoutWidget) | |
self.groupOutputTextBox.setObjectName("groupOutputTextBox") | |
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.groupOutputTextBox) | |
self.verticalLayout_2.setObjectName("verticalLayout_2") | |
self.outputTextLabel = QtWidgets.QLabel(self.groupOutputTextBox) | |
self.outputTextLabel.setObjectName("outputTextLabel") | |
self.verticalLayout_2.addWidget(self.outputTextLabel) | |
self.outputTextEdit = QtWidgets.QPlainTextEdit(self.groupOutputTextBox) | |
self.outputTextEdit.setReadOnly(True) | |
self.outputTextEdit.setObjectName("outputTextEdit") | |
self.verticalLayout_2.addWidget(self.outputTextEdit) | |
self.textLayout.addWidget(self.groupOutputTextBox) | |
MainCrypto.setCentralWidget(self.centralwidget) | |
self.statusbar = QtWidgets.QStatusBar(MainCrypto) | |
self.statusbar.setObjectName("statusbar") | |
MainCrypto.setStatusBar(self.statusbar) | |
self.menubar = QtWidgets.QMenuBar(MainCrypto) | |
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25)) | |
self.menubar.setObjectName("menubar") | |
self.menuCipher_Texts = QtWidgets.QMenu(self.menubar) | |
self.menuCipher_Texts.setObjectName("menuCipher_Texts") | |
MainCrypto.setMenuBar(self.menubar) | |
self.menubar.addAction(self.menuCipher_Texts.menuAction()) | |
self.retranslateUi(MainCrypto) | |
QtCore.QMetaObject.connectSlotsByName(MainCrypto) | |
def retranslateUi(self, MainCrypto): | |
_translate = QtCore.QCoreApplication.translate | |
MainCrypto.setWindowTitle(_translate("MainCrypto", "Cipher texts")) | |
self.optionsBox.setTitle(_translate("MainCrypto", "Options")) | |
self.decodeCheckBox.setText(_translate("MainCrypto", "Decode")) | |
self.algorithmsGroupBox.setTitle(_translate("MainCrypto", "Algorithms")) | |
self.rsaRadioButton.setText(_translate("MainCrypto", "RSA")) | |
self.caesarRadioButton.setText(_translate("MainCrypto", "Caesar")) | |
self.multiplyRadioButton.setText(_translate("MainCrypto", "Multiply")) | |
self.substitutionRadioButton.setText(_translate("MainCrypto", "Substitution")) | |
self.vigenereRadioButton.setText(_translate("MainCrypto", "Vigenere")) | |
self.affineRadioButton.setText(_translate("MainCrypto", "Affine")) | |
self.hillRadioButton.setText(_translate("MainCrypto", "Hill")) | |
self.okPushButton.setText(_translate("MainCrypto", "Ok")) | |
self.clearPushButton.setText(_translate("MainCrypto", "Clear")) | |
self.groupInputTextBox.setTitle(_translate("MainCrypto", "Input Text")) | |
self.inputTextLabel.setText(_translate("MainCrypto", "Clear/Cipher Text")) | |
self.inputTextEdit.setPlaceholderText(_translate("MainCrypto", "Put here the text that you want code and decode!")) | |
self.aKeyLineEdit.setPlaceholderText(_translate("MainCrypto", "Put the a key value here!")) | |
self.bKeyLabel.setText(_translate("MainCrypto", "B")) | |
self.aKeyLabel.setText(_translate("MainCrypto", "A")) | |
self.keyLabel.setText(_translate("MainCrypto", "Key")) | |
self.bKeyLineEdit.setPlaceholderText(_translate("MainCrypto", "Put the b key value here!")) | |
self.keyTextEdit.setPlaceholderText(_translate("MainCrypto", "Put here your code key!")) | |
self.groupOutputTextBox.setTitle(_translate("MainCrypto", "Output Text")) | |
self.outputTextLabel.setText(_translate("MainCrypto", "Clear/Cipher Text")) | |
self.outputTextEdit.setPlaceholderText(_translate("MainCrypto", "Here will be result text!")) | |
self.menuCipher_Texts.setTitle(_translate("MainCrypto", "Cipher Texts")) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'tulipan' | |
import sys | |
from PyQt5 import QtCore, QtGui, QtWidgets | |
from frame import Ui_MainCrypto | |
from algorithms import * | |
class StartCipherQt5(QtWidgets.QMainWindow): | |
def __init__(self, parent=None): | |
QtWidgets.QWidget.__init__(self, parent) | |
# self.ui = Ui_MainCrypto() | |
# self.ui.setupUi(self) | |
self.window=Ui_MainCrypto() | |
self.window.setupUi(self) | |
# self.connect(self.window.okPushButton, QtCore.SIGNAL("clicked()"), self.cipher_text) | |
self.window.okPushButton.clicked.connect(self.cipher_text) | |
self.window.clearPushButton.clicked.connect(self.clear_inputs) | |
self.key = self.window.keyTextEdit.text() | |
self.a = self.window.aKeyLineEdit.text() | |
self.b = self.window.bKeyLineEdit.text() | |
self.clear = self.window.inputTextEdit.toPlainText() | |
self.decode = self.window.decodeCheckBox.isChecked() | |
self.option = self.get_option() | |
def clear_inputs(self): | |
del self.key | |
del self.a | |
del self.b | |
del self.clear | |
self.window.keyTextEdit.clear() | |
self.window.aKeyLineEdit.clear() | |
self.window.bKeyLineEdit.clear() | |
self.window.inputTextEdit.clear() | |
self.window.outputTextEdit.clear() | |
def cipher_text(self): | |
self.window.outputTextEdit.clear() | |
self.key = self.window.keyTextEdit.text() | |
try: | |
self.a = int(self.window.aKeyLineEdit.text()) | |
self.b = int(self.window.bKeyLineEdit.text()) | |
except: | |
self.a = 0 | |
self.b = 0 | |
self.clear = self.window.inputTextEdit.toPlainText() | |
self.decode = self.window.decodeCheckBox.isChecked() | |
self.option = self.get_option() | |
# print("key: %s \n a: %d \n b: %d \n clear: %s" % (self.key, int(self.a), int(self.b), self.clear)) | |
# print("----- Cipher Function Log ----- \nkey: %s \nclear: %s \n----- # -----" % (self.key, self.clear)) | |
print("----- Cipher Function Log ----- \nkey: %s \n a: %d \n b: %d \n clear: %s \n----- # -----" | |
% (self.key, int(self.a), int(self.b), self.clear)) | |
if self.option == 4: | |
try: | |
self.key | |
self.window.outputTextEdit.setPlainText(vigenere(self.decode, self.key, self.clear)) | |
except: | |
self.error("We need a key for code/decode the text! ") | |
elif self.option == 1: | |
try: | |
self.a | |
self.window.outputTextEdit.setPlainText(caesar(self.decode, self.a, self.clear)) | |
except: | |
self.error("We need an a key for code/decode the text!") | |
elif self.option == 5: | |
try: | |
self.a | |
self.b | |
try: | |
self.window.outputTextEdit.setPlainText(affine(self.decode, self.a, self.b, self.clear)) | |
except: | |
self.error("The key is wrong! A and B aren't coprimes.") | |
except: | |
self.error("We need an a/b key for code/decode the text!") | |
elif self.option == 2: | |
try: | |
self.a | |
self.window.outputTextEdit.setPlainText(multiply(self.decode, self.a, self.clear)) | |
except: | |
self.error("We need an a key for code/decode the text!") | |
elif self.option == 3: | |
try: | |
self.key | |
try: | |
self.window.outputTextEdit.setPlainText(substitution(self.decode, self.key, self.clear)) | |
except: | |
self.error("The key is wrong! The key must have the same number of letters your alphabet.") | |
except: | |
self.error("We need a key for code/decode the text! ") | |
output = [] | |
encrypted = [] | |
print("Encrypted: ", encrypted) | |
print("Output: ", output) | |
def error(self, text): | |
title = "Error" | |
content = text + u".\n :(" | |
QtWidgets.QMessageBox.warning(self, title, content) | |
def get_option(self): | |
options = self.window.gridLayout_2.layout() | |
for i in range(0, options.count()): | |
widget = options.itemAt(i).widget() | |
if (widget!=0) and (type(widget) is QtWidgets.QRadioButton): | |
if widget.isChecked(): | |
# print(i) | |
return i | |
if __name__ == "__main__": | |
app = QtWidgets.QApplication(sys.argv) | |
mycipher = StartCipherQt5() | |
mycipher.show() | |
sys.exit(app.exec_()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from fractions import gcd | |
""" | |
Global variables for cryptographic algorithms | |
""" | |
output = [] | |
encrypted = [] | |
keyList = [] | |
stringList = [] | |
alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", | |
"V", "W", "X", "Y", "Z"] | |
def text_transform(text, key): | |
for i in text: | |
number = alphabet.index(i.upper()) | |
if key: | |
keyList.append(number) | |
else: | |
stringList.append(number) | |
if key: | |
return keyList | |
else: | |
return stringList | |
def check_valid_key(key): | |
key = key.upper() | |
keyList = list(key) | |
keyList.sort() | |
alphabet.sort() | |
if keyList != alphabet: | |
print('There is an error in the key or symbol set.') | |
def get_random_key(): | |
key = alphabet | |
random.shuffle(key) | |
return ''.join(key) | |
def compute_key(key, clear): | |
keyLength = len(key) | |
stringLength = len(clear) | |
overLap = stringLength % keyLength | |
leftOvers = key[:overLap] | |
random = stringLength - overLap | |
random = stringLength / keyLength | |
key = (int(random) * key) + leftOvers | |
return key | |
def fmi(a, m): | |
# Returns the modular inverse of a % m, which is | |
# the number x such that a*x % m = 1 | |
if gcd(a, m) != 1: | |
return None # no mod inverse if a & m aren't relatively prime | |
# Calculate using the Extended Euclidean Algorithm: | |
u1, u2, u3 = 1, 0, a | |
v1, v2, v3 = 0, 1, m | |
while v3 != 0: | |
q = u3 // v3 # // is the integer division operator | |
v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3 | |
return u1 % m | |
def determinant(matrix): | |
return (matrix[0][0] * matrix[1][1]) - \ | |
(matrix[1][0] * matrix[0][1]) | |
def invertible(matrix): | |
""" | |
Return True if a 2*2 matrix is inversible in Z26. | |
""" | |
determinant = matrix[0][0] * matrix[1][1] - \ | |
matrix[1][0] * matrix[0][1] | |
return gcd(determinant, 26) == 1 | |
def inverse_matrix(matrix): | |
""" | |
Inverse a 2*2 matrix. | |
""" | |
if not invertible(matrix): | |
return "Non invertible matrix" | |
result = [i[:] for i in matrix] | |
result[0][0] = matrix[1][1] | |
result[1][1] = matrix[0][0] | |
result[1][0] = (-matrix[1][0]) % 26 | |
result[0][1] = (-matrix[0][1]) % 26 | |
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from algorithms import * | |
clear = "If a man is offered a fact which goes against his instincts" | |
key = 'LFWOAYUISVKMNXPBDCRJTQEGHZ' | |
substitution(False, key, clear) | |
clear = 'SYLNLXSRPYYACAOLYLWJEISWIUPARLULSXRJISRSXRJSXWJR' | |
key = 'LFWOAYUISVKMNXPBDCRJTQEGHZ' | |
substitution(True, key, clear) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment