Archive for June, 2010

WWDC 2010 Keynote

I need to prefix everything I am about to say with a disclaimer:  I am not an Apple hater; I just honestly don’t understand the hype.

Apple is a great hardware company; their products are truly stunning.  I use my MacBook Pro more than any of my other computers, and I’m the defacto Mac-guy at work.  I don’t personally own an iPad/iPhone/iPod (and never have), but everybody else at my house does.   I don’t dislike Apple or their products … I very much dislike a certain rabid population of their fan base.

As a software company, I think that Apple is about average.  It’s easier to engineer software that will only run on your hardware than it is to attempt to make software that will try to handle whatever you throw at it.  Apple products (hardware/software combined) work quite well together, but there’s two sides to that coin.

So, anyway, back to the topic…

Every year, just like every other geek on the planet, I get sucked into the WWDC Keynote.  This year I was a little disappointed.

iOS

We already knew about iOS 4.  We knew what it was going to offer and quite frankly, this update is not about innovation as much as it is catching up.  I don’t always buy the quote “we didn’t do it first, but we do it best”.  Things like folders and multitasking aren’t innovative (or impressive) on a smart phone … even if you “do them better”.

iPhone 4

It’s pretty slick, but it’s not a game changer.  If it were on my carrier of choice, I might be tempted to get one.  Maybe it’s not all that impressive because we’ve seen it before … no real surprises there.

FaceTime

Really, come on…  Sure, it’s cool that this is on a cell phone, but it is really the next big thing?  I don’t know about you, but I’ve been video conferencing for years.  I’m also pretty damn sure I’d rather do it with a laptop then by holding my iPhone in my hand too.  And what if my grandmom doesn’t have an iPhone?

Now, Apple, develop an open video calling/conferencing spec that we could implement on our phones, computers, TV’s, and toasters  and I’ll jump on the bandwagon.  iPhone users video conferencing with iPhone user’s isn’t overly impressive.  I don’t know, if it were truly about changing the way we communicate, it wouldn’t just be about changing the way iPhone users communicate.

Mac Products

Maybe it’s just me, but it seems that Apple is ignoring it’s laptop/desktop line lately.  To quote Steve Jobs, not everybody needs a truck … but those of us that drive trucks would like a to be thrown a bone once in a while.  Let’s not forget that (at least for now), there is no iPhone/iPad without the developers sitting behind their MacBooks (that is, until the iPad get Xcode).

It’s quite possible though, that the Mac line isn’t in need of updating.  They are not really the company’s cash cow anyway … They’re just the incubator that hatches the little baby cash cows.

Maybe next year, the current crop will be sufficiently lagging behind to deserve a bit of an update.


All-in-all, I know all that sounds hateful, but I miss the days when Apple made me actually want to buy their products.  Today, I was just left with a “so what” attitude.

Encrypting and Decrypting Files with Java

This information is old and crufty. There’s still some decent stuff here, but check up the updated version here: https://cryptofreek.org/2012/12/10/encrypting-and-decrypting-java-part-2/

I’m working on a little app that can do simple and portable file encryption across different operating systems.  Something I can put on a thumb drive to encrypt files so that when I inevitably loose the drive, all my documents aren’t public knowledge.

Most modern operating systems offer file encryption … a lot of new external drives and flash drives come with programs that will encrypt the filesystem … but all of those things are a huge pain when you live on multiple platforms.  I move files around between my Windows, Mac, and Solaris computers at work and my Linux boxes at home.  I need something simple to get the job done that works on all of the above.  Java lets me do this pretty easily, let’s take a look …

Cryptography is still a bit of a black art, even when looking at high level API’s like we’ll see in this example.  There are any number of different ways to do this using different types of keys and algorithms.  In this example, I’ll be using a simple password based symmetric key with Triple DES (DESede) encryption (PKCS #5, RFC2898).  Please see the RFC doc for a definition of terminology.

Creating the cipher

The first step is generating an encryption key and using it to create a cipher:

public static Cipher createCipher( char[] password,
                                   int nMode,
                                   byte[] baSalt,
                                   int nInterations ) throws NoSuchAlgorithmException,
                                                     InvalidKeySpecException,
                                                     InvalidKeyException,
                                                     InvalidAlgorithmParameterException,
                                                     NoSuchPaddingException
{
  Cipher cipher = null;

  PBEParameterSpec pbeParamSpec = new PBEParameterSpec( baSalt, nInterations );

  PBEKeySpec pbeKeySpec = new PBEKeySpec( password );
  SecretKeyFactory keyFac = SecretKeyFactory.getInstance( "PBEWithSHA1AndDESede" );
  SecretKey key = keyFac.generateSecret( pbeKeySpec );

  cipher = Cipher.getInstance( "PBEWithSHA1AndDESede" );
  cipher.init( nMode, key, pbeParamSpec );

  return cipher;
}

Encrypting/Decrypting with IO Streams

Once the cipher is created, the crypto operation is as easy as reading and writing to IO streams. These could be any streams you could think of, but in this case we are using file streams:

public static void doCrypto( InputStream in, OutputStream out, Cipher cipher ) throws IOException
{
  CipherOutputStream cos = new CipherOutputStream( out, cipher );
  byte[] baChunck = new byte[1024];

  for( int nRead = 0; nRead >= 0; nRead = in.read( baChunck ) )
  {
    cos.write( baChunck, 0, nRead );
  }

  in.close();
  out.close();
  cos.close();
}

Done!

That’s it, here’s a full working example:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

public class CCryptoUtils
{
  public static Cipher createCipher( char[] password,
                                     int nMode,
                                     byte[] baSalt,
                                     int nInterations ) throws NoSuchAlgorithmException,
                                                       InvalidKeySpecException,
                                                       InvalidKeyException,
                                                       InvalidAlgorithmParameterException,
                                                       NoSuchPaddingException
  {
    Cipher cipher = null;

    PBEParameterSpec pbeParamSpec = new PBEParameterSpec( baSalt, nInterations );

    PBEKeySpec pbeKeySpec = new PBEKeySpec( password );
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance( "PBEWithSHA1AndDESede" );
    SecretKey key = keyFac.generateSecret( pbeKeySpec );

    cipher = Cipher.getInstance( "PBEWithSHA1AndDESede" );
    cipher.init( nMode, key, pbeParamSpec );

    return cipher;
  }

  public static void doCrypto( InputStream in, OutputStream out, Cipher cipher ) throws IOException
  {
    CipherOutputStream cos = new CipherOutputStream( out, cipher );
    byte[] baChunck = new byte[128];

    for( int nRead = 0; nRead >= 0; nRead = in.read( baChunck ) )
    {
      cos.write( baChunck, 0, nRead );
    }

    in.close();
    cos.close();
  }

  public static void doFileOperation( File fileIn,
                                      File fileOut,
                                      int nMode,
                                      char[] password,
                                      byte[] baSalt,
                                      int nIterations ) throws InvalidKeyException,
                                                       NoSuchAlgorithmException,
                                                       InvalidKeySpecException,
                                                       InvalidAlgorithmParameterException,
                                                       NoSuchPaddingException,
                                                       FileNotFoundException,
                                                       IOException
  {
    Cipher cipher = createCipher( password, nMode, baSalt, nIterations );
    doCrypto( new FileInputStream( fileIn ),
              new FileOutputStream( fileOut ),
              cipher );
  }

  public static void main( String[] args )
  {
    char[] password = "mypassword".toCharArray();
    byte[] baSalt = "'~7&03~/.".getBytes();
    int nIterations = 1;

    File fileToEncrypt = new File( "/Users/cryptofreek/Desktop/test.txt" );
    File fileEncrypted = new File( "/Users/cryptofreek/Desktop/test.txt.encrypted" );
    File fileDecrypted = new File( "/Users/cryptofreek/Desktop/test.txt.decrypted" );

    try
    {
      doFileOperation( fileToEncrypt,
                       fileEncrypted,
                       Cipher.ENCRYPT_MODE,
                       password,
                       baSalt,
                       nIterations );

      doFileOperation( fileEncrypted,
                       fileDecrypted,
                       Cipher.DECRYPT_MODE,
                       password,
                       baSalt,
                       nIterations );
    }
    catch( Exception e )
    {
      e.printStackTrace();
    }
  }
}

Update:  The real beauty of using IO streams like this is that you do not have to worry as much about Java’s heap space.  Your memory usage rarely grows past the chunk size specified in the “doCrypto()” function; even if you are operating on huge files.