Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Libraries
- from xlsxwriter.workbook import Workbook
- from openpyxl import load_workbook
- import random as ran
- # Variables
- counter = 0
- # Excel tools
- filepath = "encrypt.xlsx"
- wb = load_workbook(filepath)
- ws = wb.active
- workbook = Workbook('encrypt.xlsx')
- worksheet = workbook.add_worksheet()
- # Encrypt and Decrypt functions
- def encrypt(key, msg):
- encryped = []
- for i, c in enumerate(msg):
- key_c = ord(key[i % len(key)])
- msg_c = ord(c)
- encryped.append(chr((msg_c + key_c) % 127))
- return ''.join(encryped)
- def decrypt(key, encryped):
- msg = []
- for i, c in enumerate(encryped):
- key_c = ord(key[i % len(key)])
- enc_c = ord(c)
- msg.append(chr((enc_c - key_c) % 127))
- return ''.join(msg)
- # If there is no message made then this will make you input one.
- if ws['A3'].value is None:
- msg = input("Please input your message.\n")
- worksheet.write('A3', str(msg))
- message = str(ws['A3'].value)
- # Creates Keys if none are made.
- if ws['A1'].value is None:
- print("Creating new Keys...")
- # Inputs the texts into the said cells
- worksheet.write('A1', "Private Key")
- worksheet.write('B1', "Public Key")
- # Randomly generates the key.
- key = str(ran.randint(498572983475, 19287348917248938247))
- # Splits the key in half for the private and public key.
- privateKey = key[:len(key) // 2]
- publicKey = key[len(key) // 2:]
- # Inputs the keys into the said cells.
- worksheet.write('A2', str(privateKey))
- worksheet.write('B2', str(publicKey))
- # Closes/Saves the excel document.
- workbook.close()
- print("Finished.")
- # Key Variable
- privateKey = str(ws['A2'].value)
- publicKey = str(ws['B2'].value)
- tempKey = privateKey + publicKey
- fullKey = tempKey.replace(" ", "")
- encMsg = encrypt(fullKey, message)
- # Entering key section
- while counter < 3:
- tried = input("Input the keys:\n")
- if tried == fullKey:
- counter = 3
- counter = counter + 1
- # If succeeded in inputting the key.
- if tried == fullKey:
- print("Here his the message:\n", decrypt(fullKey, encMsg))
- # If failed to get the key right.
- if tried != fullKey:
- print("Invalid input.")
- # Stops the CMD prompt from closing down when code is done.
- input()
- # Made by Shadow/Tyler
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement