Wednesday, October 2, 2013

Python-gnupg on a Raspberry Pi

Installing Python-gnupg ( on a Raspberry Pi shouldn't be much different than installing it on any other *nix system.  I'm documenting it here just for my own personal notes.

Install gnupg
This goes pretty much like you'd expect:

sudo apt-get install gnupg

Install Python-gnupg
First we need pip (and setup_tools, ...):
sudo apt-get install python-dev
wget -O - | sudo python
curl -O
sudo python

Now we can get gnupg:
sudo pip install python-gnupg

The following script creates a key using gnupg and installs that key in ~/.gnupg:
import gnupg

gpg = gnupg.GPG()

# generate a key
# first we need the input data
# really we shouldn't use key lengths shorter than 2048
input_data = gpg.gen_key_input(key_type="RSA", 
                               name_comment="Test key",
key = gpg.gen_key(input_data)
print key.fingerprint

Not Fast Enough
The script above works (and doesn't do much), but takes a long time to run.

pi@3-14159 ~/gnupgTesting $ time python 

real 6m4.396s
user 0m6.270s
sys 0m0.140s

This can be sped up by install rng-tools, however rng-tools requires a hardware random number source.

The Raspberry Pi has a hardware component for random number generation, but the software for it was just recently released.  (

You may need to completely update your Pi (see the instructions in the link above), but I was able to get away with just this (and adding bcm2708-rng to /etc/modules):
sudo modprobe bcm2708-rng
sudo apt-get install rng-tools

Now performance is much better.

pi@3-14159 ~/gnupgTesting $ time python 

real 0m5.524s
user 0m4.070s
sys 0m1.060s

Encrypting Data
In this example, '' is the intended recipient (we've previously imported Bob's key) and "hello world" is the data we're going to encrypt.

>>> import gnupg
>>> gpg = gnupg.GPG()
>>> encrypted_data = gpg.encrypt("hello world", "")
>>> print encrypted_data
Version: GnuPG v1.4.12 (GNU/Linux)


Decrypting Data
Here we decrypt the data sent to Bob (we must have Bob's private key installed for this to work).  Note the use of str() around 'encrypted_data'. That's because .encrypt doesn't return a string, but rather something that can be turned in to a string.

>>> decrypted_data = gpg.decrypt(str(encrypted_data))
>>> print decrypted_data
hello world

Pesky Details
In this example I had the private keys for both parties installed on my system. That's not realistic, but it's enough to become familiar with the software before starting to much around across systems.  Key distribution is often hard.  Regular gpg can be used to do the key management if you don't need to do it in python. It's probably easier that way.

Tuesday, April 16, 2013

ABA: Arduino + Bluetooth + Android

The Bluetooth module for my Arduino (the JY-MCU) arrived a few weeks ago.  It was really inexpensive ($8.20), but took a long time to ship (3 weeks or so).  I picked it up from Deal Extreme (


I followed some of the instructions from that showed I had to connect VCC to the 5V pin, Ground to Ground (duh), the Bluetooth module's TXD to Arduino pin D10, and the Bluetooth module's RXD to Arduino pin D11.  In the end it looked like this:

Be sure you hook it up correctly! One of the disadvantages of this Bluetooth module is that it apparently doesn't have any power protection.  So you could easily short it out and release the magic smoke.

Once it was powered on I was able to scan for it using my Android phone and pair with the Bluetooth module (It shows up as 'linvor' and the pairing code is 1234).

Programming the Arduino
The next order of business is programming the thing.  I'm not looking to do exactly what this instructable did, but it's a good place to start:

I used this code for inspiration (I'm not using the same pins as the instructable [it interferes with the normal serial port on the arduino] so I needed to use the software serial port instead): and wound up with this:

#include <SoftwareSerial.h>
#define BT_SERIAL_TX_DIO 10
#define BT_SERIAL_RX_DIO 11

SoftwareSerial BluetoothSerial(BT_SERIAL_TX_DIO, BT_SERIAL_RX_DIO);
const int SENS_PIN = A0;
const int LED_PIN = 13;

int sensorValue = 0;
boolean toggle = true;
void setup() {

void loop() {
    if (BluetoothSerial.available()) {;
        BluetoothSerial.println("hello world");
        digitalWrite(LED_PIN, toggle); // toggle the LED
        toggle = !toggle;

Programming Android
Following the instructable I installed:

For python for android I installed 'Python3ForAndroid_r6.apk'.  Once it's installed  you actually have to start the application (called 'Python3 for Android') and click 'Install' to let it install whatever it needs before you can go any further.

Now I don't have the sensor they're using, so I'm just using this as a quick example to get an LED blinking.

I also followed the instructions and  put the code from in to a file named androino.html and the code from in to a file named

I started SL4A on my Android (to make sure it could create its directories before I uploaded anything).  Then I plugged the phone in to my computer and copied the androino files above to /sdcard/sl4a/scripts/.

Blink Lights

Open the SL4A app and click the 'get sensor data' button.  The LED should toggle (and 'hello world' should show up on the webpage on your Android)

It ain't pretty and definitely needs some work, but it was pretty easy (took maybe 30 minutes total).  Now I can blink an LED on the Arduino wirelessly using Bluetooth.  That's not much, but triggering other hardware should be pretty similar.  That's for next time.

Tuesday, March 5, 2013


I just purchased an Arduino Nano (  The photo in that link shows the 2.2 version, but I received the 3.0 version (pictured below).  I have a number of ideas for what I'd like to do with it, but the first order of business is getting anything to run.

Arduino Nano 3.0

My platform is Windows, so I'm basically following the instructions here.

Some things to note:

  1. The instructions say that the power LED is green, except that on my Arduino Nano the power LED is actually blue.
  2. The driver installation on Windows 8 'just worked', I didn't need to install any drivers, Windows found them automatically and installed the Arduino as a USB Serial Port on COM3.
  3. The instructions for the example say that the LED labeled 'L' will blink orange, on my Nano, that LED is white and so it just blinks white.
That was basically it.  After following those instructions (which were pretty simple) I was able to get code running on the Arduino.  It took all of 15 minutes.

Now I just need to get some of the other hardware so I can actually do something with it.