Advertisement
Jhynjhiruu

Manager

Mar 31st, 2018
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.94 KB | None | 0 0
  1. import base64
  2. import ast
  3. import datetime
  4.  
  5. errorMsg = ""
  6.  
  7. def extractPasscode(encPasscode):
  8.     passcode_split = splitPasscode(encPasscode)
  9.     try:
  10.         decPassword = str(base64.b64decode(passcode_split)).strip("b''")
  11.         extractedData = stringToArray(decPassword)
  12.     except:
  13.         raise ValueError
  14.     return extractedData
  15.  
  16. def splitPasscode(encPasscode):
  17.     split_ = encPasscode.split("_")
  18.     if len(split_) != 7:
  19.         while True:
  20.             doContinue = input("Invalid metadata. Continue?\nY/N\n").upper()
  21.             if doContinue == "Y":
  22.                 break
  23.             elif doContinue == "N":
  24.                 global errorMsg
  25.                 errorMsg = "Invalid metadata - check the start and end of the passcode"
  26.                 raise NameError
  27.             else:
  28.                 print("Invalid choice:", doContinue)  
  29.     global date
  30.     date = split_[0]
  31.     for i in split_:
  32.         if len(i) > 200:
  33.             return i
  34.     errorMsg = "General array is too short!"
  35.     raise ValueError
  36.  
  37. def stringToArray(inp):
  38.     arr = []
  39.     split = str(inp).split(",")
  40.     arr.append(split[0])
  41.     for i in range(1, 8):
  42.         arr.append(int(split[i]))
  43.     arr.append(itemString(split[8]))
  44.     arr.append(indexString(split[9]))
  45.     arr.append(keyString(split[10]))
  46.     arr.append(int(split[11]))
  47.     if int(split[12]) < 3840:
  48.         split[12] = 3840
  49.     arr.append(int(split[12]))
  50.     for i in range(13, len(split)):
  51.         arr.append(int(split[i]))
  52.     return str(arr)
  53.  
  54. def itemString(arr):
  55.     split_ = arr.split("_")
  56.     row0 = []
  57.     row1 = []
  58.     row2 = []
  59.     for i in range(0, len(split_)):
  60.         if i < 9:
  61.             row0.append(int(split_[i]))
  62.         elif i < 18:
  63.             row1.append(int(split_[i]))
  64.         elif i < 27:
  65.             row2.append(int(split_[i]))
  66.     return [row0, row1, row2]
  67.  
  68. def indexString(arr):
  69.     genArr = []
  70.     trueIndex = 0
  71.     falseIndex = 0
  72.     for i in range(0, len(arr)):
  73.         if arr[i] == "ABCDEFGHIJKLM01234"[trueIndex]:
  74.             genArr.append(True)
  75.             trueIndex = (trueIndex + 1) % 18
  76.         elif arr[i] == "NOPQRSTUVWXYZ56789"[falseIndex]:
  77.             genArr.append(False)
  78.             falseIndex = (falseIndex + 1) % 18
  79.         else:
  80.             global errorMsg
  81.             errorMsg = "General array contains invalid character: " + arr[i]
  82.             raise ValueError
  83.     return genArr
  84.  
  85. def keyString(arr):
  86.     split_ = arr.split("_")
  87.     keys = []
  88.     for i in range(0, len(split_)):
  89.         if split_[i] == "-1":
  90.             keys.append(False)
  91.         elif split_[i] != "":
  92.             keys.append(split_[i])
  93.     return keys
  94.            
  95. def formatAndEncode(decPasscode):
  96.     decPasscode = ast.literal_eval(decPasscode)
  97.     decPasscode[8] = invToString(decPasscode[8][0]) + invToString(decPasscode[8][1]) + invToString(decPasscode[8][2])
  98.     decPasscode[9] = genToString(decPasscode[9])
  99.     decPasscode[10] = keysToString(decPasscode[10])
  100.     formatted = ""
  101.     for i in decPasscode:
  102.         formatted += str(i) + ","
  103.     date_time = str(datetime.datetime.now()).split(" ")
  104.     time = date_time[1].split(":")    
  105.     formatted = "PHOENOTOPIA-" + date_time[0] + "-" + time[0] + ":" + time[1] + "-___" + str(base64.b64encode(formatted.rstrip(",").encode("ascii"))).strip("b''") + "___"
  106.     return formatted
  107.  
  108.  
  109. def invToString(invArray):
  110.     invString = ""
  111.     for i in invArray:
  112.         invString += str(i) + "_"
  113.     return invString
  114.  
  115. def keysToString(keysArray):
  116.     keyString = ""
  117.     for i in keysArray:
  118.         if i:
  119.             keyString += i + "_"
  120.         else:
  121.             keyString += "-1_"
  122.     return keyString
  123.  
  124. def genToString(genArray):
  125.    
  126.     genString = ""
  127.     trueIndex = 0
  128.     falseIndex = 0
  129.     for i in genArray:
  130.         if i:
  131.             genString += "ABCDEFGHIJKLM01234"[trueIndex]
  132.             trueIndex = (trueIndex + 1) % 18
  133.         else:
  134.             genString += "NOPQRSTUVWXYZ56789"[falseIndex]
  135.             falseIndex = (falseIndex + 1) % 18
  136.     return genString
  137.  
  138.  
  139. mode = 0
  140. menu = True
  141.  
  142. while True:
  143.     modeSelect = input("Would you like to:\n\t1.\tDecode a save file\n\tor\n\t2.\tEncode a save file\n\tor\n\t3.\t(Re)build metadata\n?\n")
  144.    
  145.     try:
  146.         mode = int(modeSelect)
  147.         if mode not in [1, 2, 3]:
  148.             raise NotImplementedError
  149.         elif mode == 1:
  150.             while True:
  151.                 try:
  152.                     inputPasscode = input("Please input a Phoenotopia 1 encoded passcode:\n")
  153.                     print("Output:\n\n\n" + extractPasscode(inputPasscode), "\n\n\nDone!")
  154.                     break
  155.                    
  156.                 except:
  157.                     if errorMsg == "":
  158.                         errorMsg = "Base 64 decode error (likely incorrect passcode)"
  159.                        
  160.                     print("Invalid passcode:", errorMsg)
  161.                        
  162.         elif mode == 2:
  163.             while True:
  164.                 try:
  165.                     inputPasscode = str(input("Please input a Phoenotopia 1 decoded formatted passcode:\n"))
  166.                     print("Output:\n\n\n" + formatAndEncode(inputPasscode), "\n\n\nDone!")
  167.                     break
  168.                
  169.                 except:
  170.                     if errorMsg == "":
  171.                         errorMsg = "Base 64 encode error (something messed up, please send me the passcode)"
  172.                        
  173.                     print("Invalid passcode:", errorMsg)
  174.         else:
  175.             run = True
  176.             while run:
  177.                 try:
  178.                     inputPasscode = str(input("Please input a Phoenotopia 1 encoded passcode:\n"))
  179.                     if len(inputPasscode.split("_")) == 7:
  180.                         while True:
  181.                             doContinue = input("The inputted passcode's metadata is (probably) valid! Continue?\nY/N\n").upper()
  182.                             if doContinue == "Y":
  183.                                 inputPasscode = inputPasscode.split("_")[3]
  184.                                 break
  185.                             elif doContinue == "N":
  186.                                 run = False
  187.                                 break
  188.                             else:
  189.                                 print("Invalid choice:", doContinue)
  190.                     if run:  
  191.                         run = False
  192.                         try:                          
  193.                             completePasscode = "PHOENOTOPIA-0000-00-00-00:00-___" + inputPasscode + "___"
  194.                             verif = formatAndEncode(extractPasscode(completePasscode))
  195.                             print("Output:\n\n\n" + verif, "\n\n\nDone!")
  196.                         except:
  197.                             print(inputPasscode, "isn't a valid passcode!")
  198.                        
  199.                        
  200.                 except:
  201.                     print("Unknown error! Please talk to me")
  202.     except:
  203.         print("Invalid choice! Please choose a valid option...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement