proper refactor as a module: still missing a cleanup of the module itself

This commit is contained in:
tcsenpai 2024-06-10 18:16:51 +02:00
parent 090a5fa6df
commit 2fcc71b8c7
3 changed files with 228 additions and 228 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

13
main.py Normal file
View File

@ -0,0 +1,13 @@
from xsackslib import XSACKS as xsacks
if __name__ == "__main__":
message = "a simple message"
key = "casualkeytouse"
skey = "evenbetter"
cipher = xsacks(key, skey)
ciphered = cipher.cipher(message)
print(ciphered)
print(" ^ Ciphered message and key | Secure key: " + skey)
deciphered = cipher.decipher(ciphered[0])
print(deciphered)
print("^ Deciphered message and key | Secure key: " + skey)

View File

@ -1,228 +1,214 @@
# How it works
class XSACKS:
def __init__(self, key, skey):
class XSACKS: self.skey = skey
def __init__(self, key, skey): self.key = key
self.skey = skey # Preparing needed variables
self.key = key self.message = ""
# Preparing needed variables self.messageCIPHER = []
self.message = "" self.messageCIPHERAscii = []
self.messageCIPHER = [] self.keyCIPHER = []
self.messageCIPHERAscii = [] self.keyCIPHERAscii = []
self.keyCIPHER = [] self.keyDECIPHER = []
self.keyCIPHERAscii = [] self.keyDECIPHERChar = []
self.keyDECIPHER = [] self.keyDECIPHERParts = []
self.keyDECIPHERChar = [] self.keyDECIPHERParts1 = []
self.keyDECIPHERParts = [] self.keyDECIPHERParts2 = []
self.keyDECIPHERParts1 = [] self.messageDECIPHERAscii = []
self.keyDECIPHERParts2 = [] self.messageDECIPHERchar = []
self.messageDECIPHERAscii = []
self.messageDECIPHERchar = [] self.messageASCII = []
self.keyASCII = []
self.messageASCII = [] self.keyASCII1 = []
self.keyASCII = [] self.keyASCII2 = []
self.keyASCII1 = []
self.keyASCII2 = [] self.skeyASCII = []
self.keyPARTS = []
self.skeyASCII = []
self.keyPARTS = [] # Preparing the ascii lists
self.keyDivider()
# Preparing the ascii lists self.keypartsToAscii()
self.keyDivider() self.skeyToAscii()
self.keypartsToAscii()
self.skeyToAscii() # Dividing the key in two parts
def keyDivider(self):
# Dividing the key in two parts n = int(len(self.key) / 2)
def keyDivider(self): partnumber = 0
n = int(len(key) / 2) for i in range(0, len(self.key), n):
partnumber = 0 partnumber += 1
for i in range(0, len(key), n): if not partnumber > 2:
partnumber += 1 self.keyPARTS.append(self.key[i : i + n])
if not partnumber > 2: else:
self.keyPARTS.append(key[i : i + n]) self.keyPARTS[1] = self.keyPARTS[1] + self.key[i : i + n]
else:
self.keyPARTS[1] = self.keyPARTS[1] + key[i : i + n] # Populating the ascii lists for the key parts
def keypartsToAscii(self):
# Populating the ascii lists for the key parts for char in self.keyPARTS[0]:
def keypartsToAscii(self): self.keyASCII1.append(ord(char))
for char in self.keyPARTS[0]: for char in self.keyPARTS[1]:
self.keyASCII1.append(ord(char)) self.keyASCII2.append(ord(char))
for char in self.keyPARTS[1]: for char in self.key:
self.keyASCII2.append(ord(char)) self.keyASCII.append(ord(char))
for char in key:
self.keyASCII.append(ord(char)) # Populating the ascii lists for the skey
def skeyToAscii(self):
# Populating the ascii lists for the skey for char in self.skey:
def skeyToAscii(self): self.skeyASCII.append(ord(char))
for char in self.skey:
self.skeyASCII.append(ord(char)) # Populating the ascii list for the message
def messageToAscii(self):
# Populating the ascii list for the message for char in self.message:
def messageToAscii(self): self.messageASCII.append(ord(char))
for char in self.message:
self.messageASCII.append(ord(char)) def cipher(self, message):
self.message = message
def cipher(self, message): self.messageToAscii()
self.message = message
self.messageToAscii() # Cipher
counterEven = 0
# Cipher counterOdd = 0
counterEven = 0 for i in range(0, len(self.messageASCII)):
counterOdd = 0 if (i % 2) == 0:
for i in range(0, len(self.messageASCII)): # Use the first part of the key
if (i % 2) == 0: counterEven += 1
# Use the first part of the key if counterEven > len(self.keyASCII1) - 1:
counterEven += 1 counterEven = 0
if counterEven > len(self.keyASCII1) - 1: cipherResult = int(self.messageASCII[i]) + int(
counterEven = 0 self.keyASCII1[counterEven]
cipherResult = int(self.messageASCII[i]) + int( )
self.keyASCII1[counterEven] while (cipherResult > 125) or (cipherResult < 32):
) if cipherResult > 125:
while (cipherResult > 125) or (cipherResult < 32): cipherResult = 31 + (cipherResult - 125)
if cipherResult > 125: if cipherResult < 32:
cipherResult = 31 + (cipherResult - 125) cipherResult = 126 - (32 - cipherResult)
if cipherResult < 32: self.messageCIPHER.append(cipherResult)
cipherResult = 126 - (32 - cipherResult) else:
self.messageCIPHER.append(cipherResult) # Use the second part of the key
else: counterOdd += 1
# Use the second part of the key if counterOdd > len(self.keyASCII2) - 1:
counterOdd += 1 counterOdd = 0
if counterOdd > len(self.keyASCII2) - 1: cipherResult = int(self.messageASCII[i]) + int(
counterOdd = 0 self.keyASCII2[counterOdd]
cipherResult = int(self.messageASCII[i]) + int( )
self.keyASCII2[counterOdd] while (cipherResult > 125) or (cipherResult < 32):
) if cipherResult > 125:
while (cipherResult > 125) or (cipherResult < 32): cipherResult = 31 + (cipherResult - 125)
if cipherResult > 125: if cipherResult < 32:
cipherResult = 31 + (cipherResult - 125) cipherResult = 126 - (32 - cipherResult)
if cipherResult < 32: self.messageCIPHER.append(cipherResult)
cipherResult = 126 - (32 - cipherResult)
self.messageCIPHER.append(cipherResult) for char in self.messageCIPHER:
self.messageCIPHERAscii.append(chr(char))
for char in self.messageCIPHER:
self.messageCIPHERAscii.append(chr(char)) # Cipher the key
counterSkey = 0
# Cipher the key for i in range(0, len(self.keyASCII)):
counterSkey = 0 counterSkey += 1
for i in range(0, len(self.keyASCII)): if counterSkey > len(self.skeyASCII) - 1:
counterSkey += 1 counterSkey = 0
if counterSkey > len(self.skeyASCII) - 1: cipherResult = int(self.keyASCII[i]) + int(self.skeyASCII[counterSkey])
counterSkey = 0 while (cipherResult > 125) or (cipherResult < 32):
cipherResult = int(self.keyASCII[i]) + int(self.skeyASCII[counterSkey]) if cipherResult > 125:
while (cipherResult > 125) or (cipherResult < 32): cipherResult = 31 + (cipherResult - 125)
if cipherResult > 125: if cipherResult < 32:
cipherResult = 31 + (cipherResult - 125) cipherResult = 126 - (32 - cipherResult)
if cipherResult < 32: self.keyCIPHER.append(cipherResult)
cipherResult = 126 - (32 - cipherResult)
self.keyCIPHER.append(cipherResult) for char in self.keyCIPHER:
self.keyCIPHERAscii.append(chr(char))
for char in self.keyCIPHER:
self.keyCIPHERAscii.append(chr(char)) # Return a string
messageCIPHERstr = ""
# Return a string for i in self.messageCIPHERAscii:
messageCIPHERstr = "" messageCIPHERstr = messageCIPHERstr + i
for i in self.messageCIPHERAscii: keyCIPHERstr = ""
messageCIPHERstr = messageCIPHERstr + i for i in self.keyCIPHERAscii:
keyCIPHERstr = "" keyCIPHERstr = keyCIPHERstr + i
for i in self.keyCIPHERAscii:
keyCIPHERstr = keyCIPHERstr + i return messageCIPHERstr, keyCIPHERstr
return messageCIPHERstr, keyCIPHERstr def decipher(self, message):
self.message = message
def decipher(self, message): self.messageToAscii()
self.message = message
self.messageToAscii() self.skey = str(self.skey)
self.skey = str(self.skey) # Decipher the key
counterSkey = 0
# Decipher the key for i in range(0, len(self.keyCIPHER)):
counterSkey = 0 counterSkey += 1
for i in range(0, len(self.keyCIPHER)): if counterSkey > len(self.skeyASCII) - 1:
counterSkey += 1 counterSkey = 0
if counterSkey > len(self.skeyASCII) - 1: cipherResult = int(self.keyCIPHER[i]) - int(self.skeyASCII[counterSkey])
counterSkey = 0 while (cipherResult > 125) or (cipherResult < 32):
cipherResult = int(self.keyCIPHER[i]) - int(self.skeyASCII[counterSkey]) if cipherResult > 125:
while (cipherResult > 125) or (cipherResult < 32): cipherResult = 31 + (cipherResult - 125)
if cipherResult > 125: if cipherResult < 32:
cipherResult = 31 + (cipherResult - 125) cipherResult = 126 - (32 - cipherResult)
if cipherResult < 32: self.keyDECIPHER.append(cipherResult)
cipherResult = 126 - (32 - cipherResult)
self.keyDECIPHER.append(cipherResult) for char in self.keyDECIPHER:
self.keyDECIPHERChar.append(chr(char))
for char in self.keyDECIPHER:
self.keyDECIPHERChar.append(chr(char)) # Divide the key deciphered
n = int(len(self.key) / 2)
# Divide the key deciphered partnumber = 0
n = int(len(key) / 2) for i in range(0, len(self.keyDECIPHERChar), n):
partnumber = 0 partnumber += 1
for i in range(0, len(self.keyDECIPHERChar), n): if not partnumber > 2:
partnumber += 1 self.keyDECIPHERParts.append(self.keyDECIPHERChar[i : i + n])
if not partnumber > 2: else:
self.keyDECIPHERParts.append(self.keyDECIPHERChar[i : i + n]) self.keyDECIPHERParts[1] = (
else: self.keyDECIPHERParts[1] + self.keyDECIPHERChar[i : i + n]
self.keyDECIPHERParts[1] = ( )
self.keyDECIPHERParts[1] + self.keyDECIPHERChar[i : i + n] # Convert it to ascii
) for char in self.keyDECIPHERParts[0]:
# Convert it to ascii self.keyDECIPHERParts1.append(ord(char))
for char in self.keyDECIPHERParts[0]: for char in self.keyDECIPHERParts[1]:
self.keyDECIPHERParts1.append(ord(char)) self.keyDECIPHERParts2.append(ord(char))
for char in self.keyDECIPHERParts[1]: # Copy the functions to cipher but to decipher
self.keyDECIPHERParts2.append(ord(char)) counterEven = 0
# Copy the functions to cipher but to decipher counterOdd = 0
counterEven = 0 for i in range(0, len(self.messageCIPHERAscii)):
counterOdd = 0 if (i % 2) == 0:
for i in range(0, len(self.messageCIPHERAscii)): # Use the first part of the key
if (i % 2) == 0: counterEven += 1
# Use the first part of the key if counterEven > len(self.keyDECIPHERParts1) - 1:
counterEven += 1 counterEven = 0
if counterEven > len(self.keyDECIPHERParts1) - 1: cipherResult = ord(self.messageCIPHERAscii[i]) - int(
counterEven = 0 self.keyDECIPHERParts1[counterEven]
cipherResult = ord(self.messageCIPHERAscii[i]) - int( )
self.keyDECIPHERParts1[counterEven] while (cipherResult > 125) or (cipherResult < 32):
) if cipherResult > 125:
while (cipherResult > 125) or (cipherResult < 32): cipherResult = 31 + (cipherResult - 125)
if cipherResult > 125: if cipherResult < 32:
cipherResult = 31 + (cipherResult - 125) cipherResult = 126 - (32 - cipherResult)
if cipherResult < 32: self.messageDECIPHERAscii.append(cipherResult)
cipherResult = 126 - (32 - cipherResult) else:
self.messageDECIPHERAscii.append(cipherResult) # Use the second part of the key
else: counterOdd += 1
# Use the second part of the key if counterOdd > len(self.keyDECIPHERParts2) - 1:
counterOdd += 1 counterOdd = 0
if counterOdd > len(self.keyDECIPHERParts2) - 1: cipherResult = ord(self.messageCIPHERAscii[i]) - int(
counterOdd = 0 self.keyDECIPHERParts2[counterOdd]
cipherResult = ord(self.messageCIPHERAscii[i]) - int( )
self.keyDECIPHERParts2[counterOdd] while (cipherResult > 125) or (cipherResult < 32):
) if cipherResult > 125:
while (cipherResult > 125) or (cipherResult < 32): cipherResult = 31 + (cipherResult - 125)
if cipherResult > 125: if cipherResult < 32:
cipherResult = 31 + (cipherResult - 125) cipherResult = 126 - (32 - cipherResult)
if cipherResult < 32: self.messageDECIPHERAscii.append(cipherResult)
cipherResult = 126 - (32 - cipherResult)
self.messageDECIPHERAscii.append(cipherResult) for char in self.messageDECIPHERAscii:
self.messageDECIPHERchar.append(chr(char))
for char in self.messageDECIPHERAscii:
self.messageDECIPHERchar.append(chr(char)) # Return a string
messageDECIPHERAsciistr = ""
# Return a string for i in self.messageDECIPHERchar:
messageDECIPHERAsciistr = "" messageDECIPHERAsciistr = messageDECIPHERAsciistr + i
for i in self.messageDECIPHERchar: keyDECIPHERstr = ""
messageDECIPHERAsciistr = messageDECIPHERAsciistr + i for i in self.keyDECIPHERChar:
keyDECIPHERstr = "" keyDECIPHERstr = keyDECIPHERstr + i
for i in self.keyDECIPHERChar:
keyDECIPHERstr = keyDECIPHERstr + i return messageDECIPHERAsciistr, keyDECIPHERstr
return messageDECIPHERAsciistr, keyDECIPHERstr
if __name__ == "__main__":
message = "a simple message"
key = "casualkeytouse"
skey = "evenbetter"
cipher = XSACKS(key, skey)
ciphered = cipher.cipher(message)
print(ciphered)
print(" ^ Ciphered message and key | Secure key: " + skey)
deciphered = cipher.decipher(ciphered[0])
print(deciphered)
print("^ Deciphered message and key | Secure key: " + skey)