24/12/2013

[Windows] Install WIM images with ImageX

To perform quick Windows installations or reinstalls, if you have .wim images, you may use the ImageX application.

You'll firstly need to copy the ImageX files on a bootable USB drive - or whatever you prefer - and the .wim images on a support accessible from your PC at boot time (network drive, system partition, USB disk, etc...)

Now reboot the system and configure the bootloader to load the ImageX support first; you'll be brought to a command line shell you can interact with.
Note: If you need to alter the disk partitioning, run the diskpart command first.

To install the image(s) simply run the following command:

PATH_TO_IMAGEX_DRIVE\imagex.exe /apply PATH_TO_IMAGES\IMAGE_NAME WIM_VOLUME_NUMBER DRIVE_LETTER

eg:

G:\imagex.exe /apply H:\images\win7.wim 1 C:\

Copying the image onto the specified destination may take some time, so just sit back and wait. Here's a full list of ImageX's command line options.

[Windows] Create bootable USB drive

To make a USB disk bootable on Windows, you can use the diskpart application. From an administrator command line, run diskpart then type the following commands:

  1. list disk - to find which one is the USB flash drive you want to make bootable
  2. select disk X - where X is the USB drive disk number you found from the previous command
  3. clean - to delete all data from it
  4. create part pri - to create a new primary partition
  5. select part 1 - to select the newly created partition
  6. format fs=Y quick - where Y is either ntfs or fat32 to perform a quick format to the desired filesystem
  7. active - to render said partition active
  8. exit
Now copy whatever you need in the root of the USB stick and you're set.

21/12/2013

[iPhone] Manage contacts and import/export vCards

iTunes may have some good syncing capabilities, but the contact management is abysmal; for example, the very basic vCard import/export functionality is missing.

Luckily, there's a third-party app for that: Wondershare TunesGo. Just install, connect to your device and select the contact menu to start managing them.

[iPhone] Set custom ringtone with longer duration than Apple's limit

iTunes allows you to upload custom ringtones to your iPhone only if their duration is less than 30 seconds. An easy way to circumvent that limitation, without jailbreaking the device, is to trick iTunes itself.

Begin with following the standard procedure to add a valid song (any .m4r shorter than the duration limit) to your Tones library, but be sure to name it exactly as the ringtone you really want to add. Follow all the steps but stop BEFORE syncing with the device.

Now create a .m4r version of the ringtone you actually want (from iTunes, create AAC version, then manually rename it to .m4r) and save it somewhere. From the filesystem explorer, browse to the folder containing the short ringtone and rename it .bak (or whatever), then copy the full length version in that same folder; make sure that the two files are named the same way, except for the .bak extension.

Finally, you can start syncing. iTunes will load the full length ringtone instead of the short one, and will not complain about it.

17/12/2013

[Java] Compute discrete logarithm

The discrete logarithm is a widely used concept in cryptography since it's pretty easy and fast to compute when knowing the crypto secrets, but it's otherwise really hard to crack by normal means.

In this example, from Coursera's cryptography I course, we'll try to compute it for some 153-digits numbers using a MITM technique. The exercise is as such:

Your goal this week is to write a program to compute discrete log modulo a prime p. Let g be some element in Z*p and suppose you are given h in Z*p such that h = g^x where x is between 1 and 2^40. A basic brute force attack is to just try all possible values in 2^40 time, but our MITM attack will take just 2^20 time.
Let B=2^20. Since x is less than B^2 we can write the unknown x base B as x=x0 B+x1 where x0,x1 are in the range [0,B-1]. Then h = g^x = g^(x0 B+x1) = (g^B)^x0·g^x1 in Zp. And then we have h/(g^x1) = (g^B) ^ x0 in Zp.
The variables in this equation are x0,x1 and everything else is known: you are given g,h and B=2^20. Since the variables x0 and x1 are now on different sides of the equation we can find a solution using meet in the middle:

  • First build a hash table of all possible values of the left hand side h/(g^x1) for x1=0,1,…,2^20. 
  • Then for each value x0=0,1,2,…,2^20 check if the right hand side (g^B)^x0 is in this hash table. If so, then you have found a solution (x0,x1) from which you can compute the required x as x=x0B+x1.
We will now find x such that h = g^x.

[Java] Hashing with SHA-256

As for the cryptography algorithms, Java offers native libraries for hashing too.

In this example I'll describe how to use apply the SHA-256 hashing to a file. However, following the example found in Coursera's cryptography I course, we won't compute the whole file's hash at once; instead, we'll break the file into 1KB blocks (1024 bytes). Then we'll compute the hash of the last block and appended said value to the second to last block. Then again we'll compute the hash of this augmented second to last block and the resulting hash is appended to the third block from the end, and so on..

Update: Hashing this way doesn't make the hash stronger or weaker, but allows us to stream the file while hashing instead of having to wait for the whole operation to complete.

As usual, auxiliary functions such as string2Hex can be found here.
The basic imports are:
 import java.io.ByteArrayInputStream;  
import java.io.DataInputStream;  
import java.io.File;  
import java.security.MessageDigest;  
import java.util.LinkedList;  
import java.util.List;  
import javax.xml.bind.DatatypeConverter;  
import org.apache.commons.io.FileUtils;  

Also note, we're using Apache commons library to handle our File operations

[Java] AES encryption and decryption

To perform AES encryption and decryption, without implementing your own algorithms - since it is usually unsafe - you may use the standard Java crypto libraries. A more complete crypto reference is found in the JCA reference guide.