Saturday, June 29, 2013

Encryption using openssl

Inspired from a school assignment from http://cit.dixie.edu/it/2400/labs/security_lab.php
using openssl to encrypt a file. Don't know that this post really needs to be written since I found a very good howto at http://osxdaily.com/2012/01/30/encrypt-and-decrypt-files-with-openssl/ but I might as well give people another one.

openssl can be used on the command line to encrypt/decrypt different files. The command structure is something like this:

openssl [type of encryption to use] -in [input filename] -out [output filename]

Then if you would like to decrypt a file you just add the -d flag

openssl [type of encryption to use] -d -in [input filename] -out [output filename]

and then of course the input file would be the encrypted file, which would then output the plaintext (after you provide the correct passphrase/key)

I got curious about trying to make a tar.gz/bz2 file out of various files you would like to encrypt. I haven't tryed this on very large files, but I have read somewhere that it is not recommended and may not work. Although for throwing a few documents together, mainly text or maybe a few pics, it works fine. I actually learned a whole lot about shell scripting with this project. Here is the code:
########################
#! /bin/bash
USAGE=$'Usage: ./encrypt <output_filename> <file1> <file2> <...>\n-h\tshow help\n-o\toverwrite file\n-d\tdecrypt\n-v\tshow version'
FLAGS=()
FILES=()
FILENAME=()
if [ "$#" -lt 2 ]
then
echo "$USAGE"
exit 1
fi
for p in "$@"
do
if [ -f "$p" ]
then
FILES=( "${FILES[@]}" "$p" )
elif [ -d "$p" ]
then
FILES=( "${FILES[@]}" "$p" )
elif [ "${p:0:1}" == "-" ]
then
FLAGS=( "${FLAGS[@]}" "$p" )
else
FILENAME=( "${FILENAME[@]}" "$p" )
fi
done
len=${#FILENAME[*]}
if [ "$len" -gt 1 ]
then
echo "${FILENAME[@]}"
echo "Command not recognized: ${FILENAME[0]}"
echo "$USAGE"
exit 1
fi
OVERWRITE=0
DECRYPT=0
if [ ${#FLAGS[*]} -gt 0 ]
then
for flag in "$FLAGS"
do
if [ $flag == "-o" ]
then
OVERWRITE=1
elif [ $flag == "-d" ]
then
DECRYPT=1
elif [ "$flag" == "-h" ] || [ "$flag" == "--help" ]
then
echo "$USAGE"
exit 1
elif [ "$flag" == "-v" ]
then
echo "encrypt v1.0"
exit 1
else
echo "Flag not recognized: $flag"
echo "$USAGE"
exit 1
fi
done
fi
if [ $DECRYPT -eq 0 ]
then
tar cvjf ${FILENAME[0]}.tar.bz2 ${FILES[@]}
openssl enc -aes-256-cbc -salt -in ${FILENAME[0]}.tar.bz2 -out ${FILENAME[0]}
if [ $OVERWRITE -gt 0 ]
then
rm ${FILES[@]}
fi
rm ${FILENAME[0]}.tar.bz2
exit 1
fi
openssl enc -d -aes-256-cbc -in ${FILES[0]} -out ${FILENAME[0]}.tar.bz2
tar xvjf ${FILENAME[0]}.tar.bz2
rm ${FILENAME[0]}.tar.bz2
if [ $OVERWRITE -gt 0 ]
then
rm ${FILES[0]}
fi
#############################

This can also be used to decrypt the file. I used aes-256 encryption because it seemed to be one of the most secure. Most of the code is probably for processing the command line flags, the actual code to create a tar.bz2 and encrypt it is pretty simple. This is probably about as far as I got in working with openssl. I am fascinated by cryptography and encryption, but don't really have much use for it(yet).

Controlling VLC remotely. (using vlc on ubuntu 12.10 and remote for vlc on android, or a web browser)

This is simply another resource for anyone wanting to remote control vlc on their computer. Some of the info I got from here. And some from various other blogs/articles about the same thing. First things first.. you need to have vlc installed. Use "sudo apt-get install vlc" to install it if you don't have it already. While most of the sites I've seen have shown you how to enable vlc's web interface from the gui program, for using this as more of something like a daemon/service, I personally have found it much more useful to just have it run in the background since when you close the gui it stops the service as well. Press Alt-F2 to open up the run dialog and simply type:

vlc -I http

which just starts the vlc web (or http) interface. At this point you can access this interface by opening a browser and typing "localhost:8080" in the location entry.


 You can control and use vlc from here. Although this isn't particularly useful at this point since I can do everything this interface does and more by just opening up the vlc gui. To be able to control vlc remotely, you're going to need to edit a few lines of a file. It is most likely located here: /usr/share/vlc/lua/http/.hosts
Now, using vim(or another preffered text editor), and as root(or with sudo privileges) open the file:

vim /usr/share/vlc/lua/http/.hosts

There should be a private addresses section that is all commented out. If your network is like mine and the ip addresses of all your computers are 192.168.x.x, then uncomment the line with 192.168.0.0/16. If the ip addresses on your network don't look like mine then find the line that looks like your ip and uncomment the corresponding line. Also, another option is that if you know the ip(s) of the device(s) you will be using to control vlc you can simply add it/them to the file(right after the private addresses section is fine).
The reason for all of this is that by default vlc's web interface is only accessible from localhost, or the same computer you're running it on. (which isn't very useful)

Once you have this set up you should be able to connect and control it from another device. If you are trying to access it from another computer just open up a web browser and type in http://192.168.x.x:8080 (replacing the ip address with the address of the computer you have the vlc server running on). You should get the same interface as earlier.
Controlling it from another computer can be useful, but I find it even more useful to be able to control vlc from my android smartphone. I used Remote for VLC by Peter Baldwin. Available in the play store(free). There are various others and I tried most of them, but this one seemed to be the most stable and I liked the setup the best. To connect to your computer with your phone, start the Remote for VLC app. If it doesn't automatically give you the option to add a server then go to the settings:



Click on 'Add VLC server' then enter the ip address of your computer, then type 8080 into the port(unless you specified a different port). It will connect and you can then search your music library via the 'Library' tab (it is more of a file explorer). Find the song or folder you would like to play



After selecting Play or clicking on the mp3 go to the 'Playing' tab. There you can use the media controls. For some reason the playlist just shows about all of your music, and you can't really just clear it when you want to play something else which is for me the only real downside of this app.(but I still chose it because it worked the best for me for what I wanted to do)