The encryption script:
import random
splitArray = []
def takeInput():
rawText = raw_input("Type something to be encrypted: \n")
password = raw_input("Please type a numerical password: \n")
encrypt(rawText, int(password))
def encrypt(rawText, password):
for c in rawText:
divide(c, password)
def divide(charToDivide, password):
asciiValue = ord(charToDivide)
a = 0
b = 0
c = 0
passa = 0
passb = 0
passc = 0
a = random.randrange(a, asciiValue)
b = random.randrange(b, asciiValue - a)
c = asciiValue - (a+b)
passa = random.randrange(passa, password)
passb = random.randrange(passb, password-passa)
passc = password - (passa+passb)
if isinstance(password, str):
print "Please enter a number"
takeInput()
else:
a += passa
b += passb
c += passc
splitArray.append(str(a))
splitArray.append(str(b))
splitArray.append(str(c))
takeInput()
f = open("myEncryptorTest", 'w')
arrayDelimiterString = "."
encryptedString = arrayDelimiterString.join(splitArray)
encryptedString = "." + encryptedString
f.write(encryptedString)
f.close
Decryption:
#XECryption decryption program
#Each character is a set of three numbers delimited by dots
#Decryption works by adding these three numbers together, subtracting the ASCII for a space and using that number to decypher
#the rest of the array.
#User is prompted to see if the message makes sense
f = open('myEncryptorTest')
encryptedString = f.read()
f.close()
#separate sets of three numbers into an array
def sort():
sortedCharArray = []
charBuffer = ""
for c in encryptedString:
if c == '.' and charBuffer != "":
sortedCharArray.append(charBuffer)
charBuffer = ""
elif c != '.':
charBuffer += c
#if the buffer is not empty (e.g. last number), put it on the end
if charBuffer != "":
sortedCharArray.append(charBuffer)
charBuffer = ""
crack(sortedCharArray)
#add sets of three numbers together and insert into an array decryption
def crack(charArray):
charBuffer = 0
arrayCount = 1
decypheredArray = []
for c in charArray:
if arrayCount % 3 == 0:
arrayCount = arrayCount + 1
charBuffer = charBuffer + int(c)
decypheredArray.append(charBuffer)
charBuffer = 0
else:
arrayCount = arrayCount + 1
charBuffer = charBuffer + int(c)
decypher(decypheredArray)
#subtract ASCII value of a space, use this subtracted value as a temporary password
def decypher(decypheredArray):
space = 32
subtractedValue = 0
arrayBuffer = []
try:
for c in decypheredArray:
subtractedValue = c - space
for c in decypheredArray:
asciicharacter = c - subtractedValue
arrayBuffer.append(asciicharacter)
answerFromCheck = check(arrayBuffer)
if answerFromCheck == "y":
#print value of password if user states correct decryption
print "Password: "
print subtractedValue
raise StopIteration()
else:
arrayBuffer = []
except StopIteration:
pass
#does the temporary password above produce an output that makes sense?
def check(arrayBuffer):
decypheredText = ""
stringArray = []
try:
for c in arrayBuffer:
try:
stringArray.append(chr(c))
except ValueError:
pass
print decypheredText.join(stringArray)
inputAnswer = raw_input("Is this correct?")
if inputAnswer == "y":
return inputAnswer
else:
stringArray = []
return inputAnswer
except StopIteration:
return
sort()
f.close()
As I say, I'm looking for advice on how to improve my code and writing code in general. I'm aware that my code is probably an affront against programmers everywhere but I want to improve. These two scripts are for the hackthissite.org Realistic 6 mission. I won't be using them for encrypting anything of great importance.