Advertisement
timber101

James Encryption

Jan 22nd, 2022
1,265
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #  James programme!
  2. http://gcsecompsci.weebly.com/uploads/3/0/9/3/30931869/2016_unit_1_traditional_scenario.pdf
  3.  
  4. import random
  5.  
  6. def mainmenu():
  7.     print("1. Encrypt a message")
  8.     print("2. Decrypt a message")
  9.     print("3. Extended Encryption")
  10.     print("4. exit\n")
  11.    
  12. def mkoffset():  # makes the offset key
  13.     key = ""
  14.     tot = 0
  15.     for i in range(8):
  16.         seed = random.randint(33,126)
  17.         tot += seed
  18.         ASCIIchar = chr(seed)
  19.         key += ASCIIchar
  20.     print(key)
  21.     offset = (tot//8) - 32
  22.     return offset
  23.  
  24. def convertoffset(key): #convert offset key
  25.     tot = 0
  26.     for i in range(len(key)):
  27.         ASCIInum = ord(key[i])
  28.         tot += ASCIInum
  29.     offset = (tot//8) - 32
  30.     return offset
  31.  
  32. def encrypt(text): # encrypt a message
  33.     codedmsg = ""
  34.     for i in range (len(text)):
  35.         if text[i] == " ":
  36.             codedmsg += " "
  37.         elif ord(text[i]) + offst > 126:
  38.             codedmsg += chr(ord(text[i]) + offst - 94)
  39.         else:
  40.             codedmsg += chr(ord(text[i]) + offst)
  41.     return codedmsg
  42.  
  43. def decrypt(text): # decrypt a message
  44.     decodedmsg = ""
  45.     for i in range(len(text)):
  46.         if text[i] == " ":
  47.             decodedmsg += " "
  48.         elif ord(text[i]) - offsetkey < 33:
  49.             decodedmsg += chr(ord(text[i]) - offsetkey + 94)
  50.         else:
  51.             decodedmsg += chr(ord(text[i]) - offsetkey)
  52.     return decodedmsg
  53.  
  54. def enhancedencrypt(text): # batch the message into groups of 5's
  55.     codedmsg = ""
  56.     for i in range (len(text)):
  57.         if text[i] == " ":
  58.             pass # ie do nothing, strip the spaces
  59.         elif ord(text[i]) + offst > 126:
  60.             codedmsg += chr(ord(text[i]) + offst - 94)
  61.             if len(codedmsg)%5 == 0 and i != len(text):
  62.                 codedmsg += " "
  63.         else:
  64.             codedmsg += chr(ord(text[i]) + offst)
  65.             if len(codedmsg)%5 == 0 and i != len(text):
  66.                 codedmsg += " "  
  67.     return codedmsg
  68.    
  69.  
  70. mainmenu()
  71. option = int(input("Please select your option >> "))
  72.  
  73. if option == 1:
  74.     filename = input("Please enter the file name to be encrpted, includng file extension >> ")
  75.     file = open(filename,"r")
  76.     fileconts = file.read()
  77.     file.close()
  78.     print(fileconts)
  79.     offst = mkoffset() # generates offset key
  80.     print(offst)
  81.     newmssg = encrypt(fileconts)
  82.     print(newmssg)
  83.     savefilename = input("Please enter the name the encrypted file should be saved as, with extension >> ")
  84.     file = open(savefilename,"w")
  85.     file.write(newmssg)
  86.     file.close()
  87.    
  88. elif option == 2:
  89.     filename = input("Please enter the file name of the encrypted file, including file extension >> ")
  90.     file = open(filename,"r")
  91.     fileconts = file.read()
  92.     file.close()
  93.     key = input("please enter the 8 digit key >> ")
  94.     offsetkey = (convertoffset(key))
  95.     decodedmessage = decrypt(fileconts)
  96.     print(decodedmessage)
  97.    
  98. elif option == 3:
  99.     filename = input("Please enter the file name to be encrpted, includng file extension >> ")
  100.     file = open(filename,"r")
  101.     fileconts = file.read()
  102.     file.close()
  103.     print(fileconts)
  104.     offst = mkoffset() # generates offset key
  105.     print(offst)
  106.     newmssg = enhancedencrypt(fileconts)
  107.     print(newmssg)
  108.     savefilename = input("Please enter the name the encrypted file should be saved as, with extension >> ")
  109.     file = open(savefilename,"w")
  110.     file.write(newmssg)
  111.     file.close()
  112.    
  113. else:
  114.     print("exiting . . ")
  115.     quit
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement