Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. # Libraries
  2.  
  3. from xlsxwriter.workbook import Workbook
  4. from openpyxl import load_workbook
  5. import random as ran
  6.  
  7. # Variables
  8.  
  9. counter = 0
  10.  
  11. # Excel tools
  12.  
  13. filepath = "encrypt.xlsx"
  14. wb = load_workbook(filepath)
  15. ws = wb.active
  16.  
  17. workbook = Workbook('encrypt.xlsx')
  18. worksheet = workbook.add_worksheet()
  19.  
  20.  
  21. # Encrypt and Decrypt functions
  22.  
  23. def encrypt(key, msg):
  24.     encryped = []
  25.     for i, c in enumerate(msg):
  26.         key_c = ord(key[i % len(key)])
  27.         msg_c = ord(c)
  28.         encryped.append(chr((msg_c + key_c) % 127))
  29.     return ''.join(encryped)
  30.  
  31.  
  32. def decrypt(key, encryped):
  33.     msg = []
  34.     for i, c in enumerate(encryped):
  35.         key_c = ord(key[i % len(key)])
  36.         enc_c = ord(c)
  37.         msg.append(chr((enc_c - key_c) % 127))
  38.     return ''.join(msg)
  39.  
  40.  
  41. # If there is no message made then this will make you input one.
  42.  
  43. if ws['A3'].value is None:
  44.     msg = input("Please input your message.\n")
  45.     worksheet.write('A3', str(msg))
  46.  
  47. message = str(ws['A3'].value)
  48.  
  49. # Creates Keys if none are made.
  50.  
  51. if ws['A1'].value is None:
  52.     print("Creating new Keys...")
  53.  
  54.     # Inputs the texts into the said cells
  55.  
  56.     worksheet.write('A1', "Private Key")
  57.     worksheet.write('B1', "Public Key")
  58.  
  59.     # Randomly generates the key.
  60.  
  61.     key = str(ran.randint(498572983475, 19287348917248938247))
  62.  
  63.     # Splits the key in half for the private and public key.
  64.  
  65.     privateKey = key[:len(key) // 2]
  66.     publicKey = key[len(key) // 2:]
  67.  
  68.     # Inputs the keys into the said cells.
  69.  
  70.     worksheet.write('A2', str(privateKey))
  71.     worksheet.write('B2', str(publicKey))
  72.  
  73.     # Closes/Saves the excel document.
  74.  
  75.     workbook.close()
  76.  
  77.     print("Finished.")
  78.  
  79. # Key Variable
  80.  
  81. privateKey = str(ws['A2'].value)
  82. publicKey = str(ws['B2'].value)
  83.  
  84. tempKey = privateKey + publicKey
  85.  
  86. fullKey = tempKey.replace(" ", "")
  87.  
  88. encMsg = encrypt(fullKey, message)
  89.  
  90. # Entering key section
  91.  
  92. while counter < 3:
  93.     tried = input("Input the keys:\n")
  94.     if tried == fullKey:
  95.         counter = 3
  96.     counter = counter + 1
  97.  
  98. # If succeeded in inputting the key.
  99.  
  100. if tried == fullKey:
  101.     print("Here his the message:\n", decrypt(fullKey, encMsg))
  102.  
  103. # If failed to get the key right.
  104.  
  105. if tried != fullKey:
  106.     print("Invalid input.")
  107.  
  108. # Stops the CMD prompt from closing down when code is done.
  109.  
  110. input()
  111.  
  112. # Made by Shadow/Tyler
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement