0

I was trying to run this decrypt.py code on windows 10 in terminal, I have installed Python 3 in my Windows in Drive C, and this code is in Drive D. The terminal is run in Visual Studio Code.

#!/usr/bin/python3

from Crypto import Random
from Crypto.Cipher import AES
import os
import os.path
from os import listdir
from os.path import isfile, join
import time


class Encryptor:
    def __init__(self, key):
        self.key = key

    def pad(self, s):
        return s + b"\0" * (AES.block_size - len(s) % AES.block_size)

    def encrypt(self, message, key, key_size=256):
        message = self.pad(message)
        iv = Random.new().read(AES.block_size)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        return iv + cipher.encrypt(message)

    def encrypt_file(self, file_name):
        with open(file_name, 'rb') as fo:
            plaintext = fo.read()
        enc = self.encrypt(plaintext, self.key)
        with open(file_name + ".enc", 'wb') as fo:
            fo.write(enc)
        os.remove(file_name)

    def decrypt(self, ciphertext, key):
        iv = ciphertext[:AES.block_size]
        cipher = AES.new(key, AES.MODE_CBC, iv)
        plaintext = cipher.decrypt(ciphertext[AES.block_size:])
        return plaintext.rstrip(b"\0")

    def decrypt_file(self, file_name):
        with open(file_name, 'rb') as fo:
            ciphertext = fo.read()
        dec = self.decrypt(ciphertext, self.key)
        with open(file_name[:-4], 'wb') as fo:
            fo.write(dec)
        os.remove(file_name)

    def getAllFiles(self):
        dir_path = os.path.dirname(os.path.realpath(__file__))
        dirs = []
        for dirName, subdirList, fileList in os.walk(dir_path):
            for fname in fileList:
                if (fname != 'script.py' and fname != 'mykeys.txt.enc'):
                    dirs.append(dirName + "\\" + fname)
        return dirs

    def encrypt_all_files(self):
        dirs = self.getAllFiles()
        for file_name in dirs:
            self.encrypt_file(file_name)

    def decrypt_all_files(self):
        dirs = self.getAllFiles()
        for file_name in dirs:
            self.decrypt_file(file_name)


key = b'[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e'
enc = Encryptor(key)
clear = lambda: os.system('cls')

if os.path.isfile('mykeys.txt.enc'):

    while True: 
        mykeys = str(input("Enter keys: "))
        enc.decrypt_file("mykeys.txt.enc")
        p = ''
        with open("mykeys.txt", "r") as f:
            p = f.readlines()
        if p[0] == mykeys:
            enc.encrypt_file("mykeys.txt")
            break

    while True:
        clear()
        choice = int(input(
            "1. Press '1' to encrypt file.\n2. Press '2' to decrypt file.\n3. Press '3' to Encrypt all files in the directory.\n4. Press '4' to decrypt all files in the directory.\n5. Press '0' to exit.\n"))
        clear()
        if choice == 1:
            enc.encrypt_file(str(input("Enter name of file to encrypt: ")))
        elif choice == 2:
            enc.decrypt_file(str(input("Enter name of file to decrypt: ")))
        elif choice == 3:
            enc.encrypt_all_files()
        elif choice == 4:
            enc.decrypt_all_files()
        elif choice == 0:
            exit()
        else:
            print("Please select a valid option!")

else: print("You don't have permission to read file.")

However, when I run it, Terminal gave me this response

PS D:\project\temp-and-humidity> & C:/Users/bleac/AppData/Local/Microsoft/WindowsApps/python.exe d:/project/temp-and-humidity/decrypt.py
You don't have permission to read file.
PS D:\project\temp-and-humidity> 

What do I need to do to gain permissions to run this file on Windows 10? I was searching the way to run it similar to sudo but I am confused at how to use the runas command. Also, when I right click the file, it have no "Run as administrator" option.

enter image description here

edit: I will post the file in here, the file is in this zip in Google Drive.

https://drive.google.com/open?id=1sOYeu_XLGuYhV-FRPHtLKNTPDtPyCybY

2 Answers2

0

Open terminal on the path. and write this code python decrypt.py press ENTER

0

You are installed Python 3 from Windows Store, that's why you got limited permission when trying to run scripts. Try to uninstall it, and install the Python from the official webiste instead

https://www.python.org/downloads/


Btw, here is the related question you might interested #59148628

Smankusors
  • 377
  • 2
  • 8
  • 21
  • Result: It works! But then the code tell me `ModuleNotFoundError: No module named 'Crypto'` so I run `pip install pycryptodome` and run the code again with both `python decrypt.py` and `C:/Users/bleac/AppData/Local/Programs/Python/Python38-32/python.exe d:/project/temp-and-humidity/decrypt.py` but this times it tells me again that I do not have permissions. What could possibly go wrong? – Chakrit Hinanawi Teerawijit Mar 11 '20 at 05:56
  • wait, how about `pycrypto` instead of `pycryptodome`? – Smankusors Mar 11 '20 at 06:17
  • I try `pycrypto` but it gave me the result as if I don't have it. The code must be specifically referencing `pycryptodome` in it. Still, I wonder what could be th source of the permissions problem since the Python path already got configured to `.../Local/Programs/...` – Chakrit Hinanawi Teerawijit Mar 11 '20 at 06:23
  • you might want to follow [this guide](https://www.pycryptodome.org/en/latest/src/installation.html#windows-from-sources-python-3-5-and-newer) – Smankusors Mar 11 '20 at 06:27
  • I followed the guide. It seems that my problem with pycryptodome has ended. Problem is, I keep getting a result of `You don't have permission to read file.` llike before I fix it again. I searched the way to fix it by disabling Windows Store Python(even after I uninstall it). And there is no more old location of Python reference in my Windows Environment Variables, only new one. Now I am very confused at it. – Chakrit Hinanawi Teerawijit Mar 11 '20 at 06:54
  • 1
    Update: I finally fixed it. It was about file requirement in the coding. It was my fault thinking it was other problems. Sorry for extending it. Now my problem got solved. Thank you! – Chakrit Hinanawi Teerawijit Mar 11 '20 at 07:00