Archives for 21 Dec,2009

You are browsing the site archives by date.

Python: Cryptography Caesar shift encryption (shift cipher)

I have always had a keen interest in cryptography and rather than give a brief history of cryptography I will recommend reading Simon Singh’s The code book or for a modern and hands on approach Applied Cryptography by Bruce Schneier (Who also made a brilliant book on security, more of descriptive approach but very interesting Secrets and Lies: Digital Security in a Networked World).

This post aims to detail the creation (in python) of one of the simplest forms of encryption; the simple Caesar shift (or shift cipher). The Caesar shift takes the normal alphabet and maps it to a an identical alphabet with a rotation. The cipher will be written in such a way that it can be easily expanded on to create more complex encryption schemes with little modification.

Caesar shift alphabet

Caesar shift alphabet diagram

The above image shows a diagrammatic representation of a Caesar shift of 3 (alphabet transposed onto a rotation of itself with a displacement of 3). Below shows the entire alphabet in plaintext and in ciphertext, followed by a simple sentence in plaintext and in cipher text.

Plaintext: abcdefghijklmnopqrstuvwxyz
Ciphertext: defghijklmnopqrstuvwxyzabc
Plaintext: the cat sat on the mat
Ciphertext: wkh fdw vdw rq wkh pdw

Below is the code to convert plaintext into ciphertext along with an example of the usage:

def caesar(plaintext,shift):

	alphabet=["a","b","c","d","e","f","g","h","i","j","k","l",
	"m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

	#Create our substitution dictionary
	dic={}
	for i in range(0,len(alphabet)):
		dic[alphabet[i]]=alphabet[(i+shift)%len(alphabet)]

	#Convert each letter of plaintext to the corrsponding
	#encrypted letter in our dictionary creating the cryptext
	ciphertext=""
	for l in plaintext.lower():
		if l in dic:
			l=dic[l]
		ciphertext+=l

	return ciphertext

#Example useage
plaintext="the cat sat on the mat"
print "Plaintext:", plaintext
print "Cipertext:",caesar(plaintext,3)
#This will result in:
#Plaintext: the cat sat on the mat
#Cipertext: wkh fdw vdw rq wkh pdw

Explanation
Here we have written a function that is given the plaintext and an arbitary rotation (including negative) and returns a ciphertext. The function creates a dictionary, mapping each letter to another letter that is ‘shift’ letters away from it in the alphabet (the modulus (%) is used so to wrap the alphabet back to the start if it falls off the end). It then converts each letter of the plain text into the corresponding encrypted letter using the dictionary.

Future
In the next post I will discuss methods to improve the Caesar shift and how to turn it into a full substitution cipher (where the alphabet is shuffled rather than rotated). The Caesar shift is very susceptible to brute forcing and frequency analysis which I shall explain and create a program to defeat these encryptions in a future post.

Read More

Installing custom roms and backing up the T-mobile Pulse (Huawei)

The chaps over at the modaco forums have detailed how to backup and replace the firmware on the android T-mobile pulse. This post aims to consolidate and link all the information in these posts and provide a few explanations.

tmobile pulse

tmobile pulse

Definitions
Rom – a file which contains the image for a particular version of android
Nand memory – the internal memory of the phone which contains android
flashing – replacing the contents of the Nand memory
root – superuser or admin

Gaining root access
This follows the forum post here

  • Download superboot and unzip.
  • Reboot your phone into bootloader mode (Power off your phone then turn it on with volume down and the red button pressed). You should be presented with a blue screen and some information, connect your pulse to your computer via the usb cable.
  • Windows – Run ‘install-superboot-windows.bat’
  • Mac – Run ‘./install-superboot-mac.sh’, You may need to make the script executable by using ‘chmod +x install-superboot-mac.sh’
  • Linux – Same as above ‘./install-superboot-linux.sh’ and ‘chmod +x install-superboot-linux.sh’. These commands will most likely need root access so you may need to prepend these commands with ‘sudo ‘.
  • You should now see your pulse has some text on the screen with information about how much data was transfered.
  • Reboot (by taking the battery out) and you should now have root access on your phone

Installing and running recovery tool
This follows the forum post here.

  • The process is the same as above but using a different zip file: recovery
  • Once rebooted, you will need to install the ‘quickboot’ program from the market app. This will allow you to boot into recovery mode (this require root access which we have previously attained)
  • Using the recovery tool you can backup your android using Nandroid, this will only save the Nand memory not any of your settings.
  • The backup will by default save the backup to your SD card, you can then enable usb transfer in the recovery menu and save this to your computer.

Partitioning your SD card
The SD card may need to be partitioned in preparation for the custom rom.

  • From the recovery menu you can partition your SD card.
  • By default the size of the swap partition (used as “extra”-RAM by android) is 32mb however the current MoDaCo Custom ROM’s will not boot properly if there is a swap drive on the SD card
  • The MoDaCo roms also allow an ext2 partition to be made for applications in order to free internal memory.

Installing Custom Roms
Now the SD card is partitioned we can go about installing the new version of android to our pulse. We simply upload the rom zip onto the SD card and select the update option from the recovery mode. The first boot after this is done will take a while to transfer all the apps to your SD card.

Currently there are two roms from MoDaCo, one Customised Vanilla android 1.5 and a Customised T-mobile 1.5. They contain useful extra tools and include ssh access (with the password for ssh contained in the about screen).

Future
Hopefully there will soon be a rom will be release with a updated version of android 1.6 or 2.0 which can be flashed onto the pulse to bring all the new and improve features described here. Recent discussions have hinted at multi-touch becoming a reality once the kernel sources are released (they are now released)

Read More