Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # James programme!
- http://gcsecompsci.weebly.com/uploads/3/0/9/3/30931869/2016_unit_1_traditional_scenario.pdf
- import random
- def mainmenu():
- print("1. Encrypt a message")
- print("2. Decrypt a message")
- print("3. Extended Encryption")
- print("4. exit\n")
- def mkoffset(): # makes the offset key
- key = ""
- tot = 0
- for i in range(8):
- seed = random.randint(33,126)
- tot += seed
- ASCIIchar = chr(seed)
- key += ASCIIchar
- print(key)
- offset = (tot//8) - 32
- return offset
- def convertoffset(key): #convert offset key
- tot = 0
- for i in range(len(key)):
- ASCIInum = ord(key[i])
- tot += ASCIInum
- offset = (tot//8) - 32
- return offset
- def encrypt(text): # encrypt a message
- codedmsg = ""
- for i in range (len(text)):
- if text[i] == " ":
- codedmsg += " "
- elif ord(text[i]) + offst > 126:
- codedmsg += chr(ord(text[i]) + offst - 94)
- else:
- codedmsg += chr(ord(text[i]) + offst)
- return codedmsg
- def decrypt(text): # decrypt a message
- decodedmsg = ""
- for i in range(len(text)):
- if text[i] == " ":
- decodedmsg += " "
- elif ord(text[i]) - offsetkey < 33:
- decodedmsg += chr(ord(text[i]) - offsetkey + 94)
- else:
- decodedmsg += chr(ord(text[i]) - offsetkey)
- return decodedmsg
- def enhancedencrypt(text): # batch the message into groups of 5's
- codedmsg = ""
- for i in range (len(text)):
- if text[i] == " ":
- pass # ie do nothing, strip the spaces
- elif ord(text[i]) + offst > 126:
- codedmsg += chr(ord(text[i]) + offst - 94)
- if len(codedmsg)%5 == 0 and i != len(text):
- codedmsg += " "
- else:
- codedmsg += chr(ord(text[i]) + offst)
- if len(codedmsg)%5 == 0 and i != len(text):
- codedmsg += " "
- return codedmsg
- mainmenu()
- option = int(input("Please select your option >> "))
- if option == 1:
- filename = input("Please enter the file name to be encrpted, includng file extension >> ")
- file = open(filename,"r")
- fileconts = file.read()
- file.close()
- print(fileconts)
- offst = mkoffset() # generates offset key
- print(offst)
- newmssg = encrypt(fileconts)
- print(newmssg)
- savefilename = input("Please enter the name the encrypted file should be saved as, with extension >> ")
- file = open(savefilename,"w")
- file.write(newmssg)
- file.close()
- elif option == 2:
- filename = input("Please enter the file name of the encrypted file, including file extension >> ")
- file = open(filename,"r")
- fileconts = file.read()
- file.close()
- key = input("please enter the 8 digit key >> ")
- offsetkey = (convertoffset(key))
- decodedmessage = decrypt(fileconts)
- print(decodedmessage)
- elif option == 3:
- filename = input("Please enter the file name to be encrpted, includng file extension >> ")
- file = open(filename,"r")
- fileconts = file.read()
- file.close()
- print(fileconts)
- offst = mkoffset() # generates offset key
- print(offst)
- newmssg = enhancedencrypt(fileconts)
- print(newmssg)
- savefilename = input("Please enter the name the encrypted file should be saved as, with extension >> ")
- file = open(savefilename,"w")
- file.write(newmssg)
- file.close()
- else:
- print("exiting . . ")
- quit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement