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

392
xsacks.py
View File

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