slight pre-refactor to use it as a class

This commit is contained in:
tcsenpai 2024-06-10 18:12:55 +02:00
parent a99da02dee
commit 090a5fa6df

224
xsacks.py
View File

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