Welcome to [GHB] - GAmEhAcKbAsTaRdS Forum

Welcome to [GHB] - GAmEhAcKbAsTaRdS Forum, like most online communities you must register to view or post in our community, but don't worry this is a simple free process that requires minimal information for you to signup. Be apart of [GHB] - GAmEhAcKbAsTaRdS Forum by signing in or creating an account.
  • Start new topics and reply to others
  • Subscribe to topics and forums to get email updates
  • Get your own profile page and make new friends
  • Send personal messages to other members.

Sign in to follow this  
iCode

[Python] Encrypt your data with a USB

Recommended Posts

First of all, I'm using Linux and these scripts was developed for Linux.

 

 

So, let's say you have some stuff on your PC/laptop that you want to protect at any cost. A file that contains passwords, some nasty code you wrote or your girlfriend's nudes, you name it.

 

The codes below will automatically decrypt your data when you put your USB in your computer, and encrypt it when you remove it.

 

You can use this Python script to generate a key:

#!/usr/bin/python
import getpass, hashlib

passwd = getpass.getpass("Password: ")
key = hashlib.sha256(passwd).digest()

print("Your key: ", key)

Put this script in "/etc/init.d/" and don't forget to use "chmod +x <filename>" on it:

#!/bin/bash
# USB ID and NAME must match, if one of the two is not right, it wont work!
usb_id="05dc:a209" # Can be found with "lsusb" command
usb_name="my_usb"

function encrypt {
  python /home/$USER/enc.py
}

function decrypt {
  python /media/$USER/$usb_name/dec.py
}

while [ True ]; do
  if lsusb | grep $usb_id >/dev/null; then
    if [ -d "/media/$USER/$usb_name" ]; then
      if [ -f /home/$USER/enc.py ]; then
        echo "Waiting for USB removal..."
      else
        cp /media/$USER/$usb_name/enc.py /home/$USER/; decrypt
      fi
      sleep 3
    fi
  else
    if [ -f /home/$USER/enc.py ]; then
      encrypt; rm /home/$USER/enc.py
    fi
    sleep 3
  fi
done

Edit the "usb_id" and "usb_name".

 

Put these files on your USB:

 

enc.py

#!/usr/bin/python3
import os, sys, getpass
try:
    from Crypto import Random
    from Crypto.Cipher import AES
except:
    print('[!] Missing Python-crypto - Installing')
    os.system('sudo apt-get install python-crypto -y')
    print('[!] Done - Try again')

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

def encrypt(message, key, key_size=256):
    message = 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(file_name, key):
    with open(file_name, 'rb') as f:
        plaintext = f.read()
    enc = encrypt(plaintext, key)
    with open(file_name + ".rip", 'wb') as f:
        f.write(enc)

key = "7\x13:\xee\x95O\xe4\x8b\x02\x88Ol\xe0\[email protected]\x9e\xa9dm\x97\x00'\xc4eO\xc8Z\xac$\x00\xf1"

try:
    p = "/home/%s/Documents/" % getpass.getuser()
    counter = 0
    for path, subdirs, files in os.walk(p):
        for name in files:
            if name.endswith(".rip"):
                print("[ Skipped ] %s" % name)
            elif "." in name:
                encrypt_file(os.path.join(path, name), key)
                print("[ Encrypting ] %s" % name)
                counter = counter+1
                os.remove(os.path.join(path, name))
    print("\n[ DONE ] Encrypted %i files" % counter)
    sys.exit()
except KeyboardInterrupt:
    print("\nInterrupted!\n")
    sys.exit()
except Exception as e:
    print("\n[ ERROR ] %s" % e)
    sys.exit()

dec.py

#!/usr/bin/python3
import os, sys, getpass
try:
    from Crypto import Random
    from Crypto.Cipher import AES
except:
    print('[!] Missing Python-crypto - Installing')
    os.system('sudo apt-get install python-crypto -y')
    print('[!] Done - Try again')

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

def decrypt(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("\0")

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

key = "7\x13:\xee\x95O\xe4\x8b\x02\x88Ol\xe0\[email protected]\x9e\xa9dm\x97\x00'\xc4eO\xc8Z\xac$\x00\xf1"

try:
    p = "/home/%s/Documents/" % getpass.getuser()
    counter = 0
    for path, subdirs, files in os.walk(p):
        for name in files:
            if name.endswith(".rip"):
                decrypt_file(os.path.join(path, name), key)
                print("[ Decrypting ] %s" % name)
                counter = counter+1
                os.remove(os.path.join(path, name))
            else:
                print("[ Skipped ] %s"% name)
    print("\n[ DONE ] Decrypted %i files" % counter)
    sys.exit()
except KeyboardInterrupt:
    print("\nInterrupted!\n")
    sys.exit()
except Exception as e:
    print("\n[ ERROR ] %s" % e)
    sys.exit()

Edit the file path in both of these files to what ever you prefer to encrypt. It's the Documents directory by default.

 

Encryption may protect your data, but not you from losing the USB with the script on it.

 

I hope you enjoyed reading this, bb :)

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.