Archive for the ‘Java’ Category

Base64 Encoding and Decoding in Java

Base64 encoding is a convenient way to represent binary data as plain text. This allows for transport of said binary data over a medium that is traditionally more text friendly. In the general case, simply storing binary data as text is quite handy sometimes.

The most common ways to accomplish this are either Base64 encoding, or to HEX. Base64 encoding takes 3 bytes of binary data and turns it into 4 bytes of text. HEX encoding takes 1 byte of binary data, and turns it into 2 bytes of text. 3 bytes of binary data would be transformed into 6 bytes with HEX. While the difference between 4 and 6 bytes isn’t that bad, if you multiply that difference millions of times, the savings really become significant. Therefore, Base64 encoding is more efficient in storage/transport than HEX.

So, here’s my shot at a simple Base64 encoding and decoding in Java. I hammered it out in about 15 minutes; hopefully it’s not horrendously bad. There are tons of other tried/tested Base64 encoders out there; this was an academic exercise at best … for now anyway …

/*
 * Copyright (c) 2014
 * Cole Barnes [cryptofreek{at}gmail{dot}com]
 * https://cryptofreek.org/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package org.cryptofreek.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

public class CBase64Utils
{
  private static final String BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

  public static String encode(byte[] ba)
  {
    String strB64 = null;

    if (ba == null)
    {
      strB64 = null;
    }
    else
    {
      try
      {
        ByteArrayInputStream in = new ByteArrayInputStream(ba);
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        encode(in, out);

        out.flush();
        out.close();
        in.close();

        strB64 = new String(out.toByteArray());
      }
      catch (IOException e)
      {
        strB64 = null;
        e.printStackTrace();
      }
    }

    return strB64;
  }

  public static void encode(InputStream in, OutputStream out) throws IOException
  {
    if (in == null || out == null)
    {
      // nothing to do
    }
    else
    {
      byte[] buff = new byte[3];
      int nRead = 0;
      int nPaddingChars = 0;

      while ((nRead = in.read(buff)) > 0)
      {
        if (nRead == 1)
        {
          // pad last 2 bytes
          buff[1] = 0;
          buff[2] = 0;
          nPaddingChars = 2;
        }
        else if (nRead == 2)
        {
          // pad last byte
          buff[2] = 0;
          nPaddingChars = 1;
        }
        else
        {
          // buffer full, no need to pad
        }

        // a little help here from:
        //    http://www.wikihow.com/Encode-a-String-to-Base64-With-Java
        int n = ((buff[0] & 0xff) << 16) + ((buff[1] & 0xff) << 8) + (buff[2] & 0xff);

        byte[] baTmp = new byte[4];
        baTmp[0] = (byte) BASE64_CHARS.charAt((n >> 18) & 0x3f);
        baTmp[1] = (byte) BASE64_CHARS.charAt((n >> 12) & 0x3f);

        if (nPaddingChars == 2)
        {
          baTmp[2] = (byte) '=';
        }
        else
        {
          baTmp[2] = (byte) BASE64_CHARS.charAt((n >> 6) & 0x3f);
        }

        if (nPaddingChars == 1 || nPaddingChars == 2)
        {
          baTmp[3] = (byte) '=';
        }
        else
        {
          baTmp[3] = (byte) BASE64_CHARS.charAt(n & 0x3f);
        }

        out.write(baTmp);
        out.flush();
      }
    }
  }

  public static byte[] decode(String strB64)
  {
    byte[] ba = null;

    if (strB64 == null)
    {
      ba = null;
    }
    else if (strB64.length() == 0)
    {
      ba = new byte[0];
    }
    else
    {
      try
      {
        byte[] baB64 = strB64.getBytes("UTF-8");
        ByteArrayInputStream in = new ByteArrayInputStream(baB64);
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        decode(in, out);

        out.flush();
        out.close();
        in.close();

        ba = out.toByteArray();
      }
      catch (UnsupportedEncodingException e)
      {
        ba = null;
        e.printStackTrace();
      }
      catch (IOException e)
      {
        ba = null;
        e.printStackTrace();
      }
    }

    return ba;
  }

  public static void decode(InputStream in, OutputStream out) throws IOException
  {
    if (in == null || out == null)
    {
      // nothing to do
    }
    else
    {
      byte[] buff = new byte[4];
      int nBytesRead = 0;

      while ((nBytesRead = in.read(buff)) > 0)
      {
        if (nBytesRead != 4) { throw new IOException("The input was not a valid Base64 encoding; block read was less than 4 bytes."); }

        char c0 = (char) buff[0];
        char c1 = (char) buff[1];
        char c2 = (char) buff[2];
        char c3 = (char) buff[3];

        byte b0 = (byte) BASE64_CHARS.indexOf(c0);
        byte b1 = (byte) BASE64_CHARS.indexOf(c1);
        byte b2 = (c2 == '=')?0:(byte) BASE64_CHARS.indexOf(c2);
        byte b3 = (c3 == '=')?0:(byte) BASE64_CHARS.indexOf(c3);

        int n = ((b0 & 0x3f) << 18) + ((b1 & 0x3f) << 12) + ((b2 & 0x3f) << 6) + ((b3 & 0x3f));

        out.write((byte) (n >> 16) & 0xff);
        if( c2!='=' ) out.write((byte) (n >> 8) & 0xff);
        if( c3!='=' ) out.write((byte) (n) & 0xff);
      }
    }
  }

  public static void main(String[] args)
  {
    // example taken from:
    //    http://en.wikipedia.org/wiki/Base64
    String strKnown = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
    String strKnownB64 = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";

    byte[] ba = strKnown.getBytes();

    String strB64 = encode(ba);
    System.out.println("Calculated Base64 string:  " + strB64);
    System.out.println("     Known Base64 string:  " + strKnownB64);
    System.out.println("                   Match:  " + strB64.equals(strKnownB64));

    System.out.println();

    byte[] baData = decode(strB64);
    String str = new String(baData);
    System.out.println("Decoded string:  " + str);
    System.out.println("  Known string:  " + strKnown);
    System.out.println("         Match:  " + str.equals(strKnown));
  }
}

Encrypting and Decrypting Java – Part 2

A few years ago, I wrote a post regarding file encryption with Java here: https://cryptofreek.org/2010/06/04/encrypting-and-decrypting-files-with-java/

To my amazement, that post still gets tons of hits and it’s linked to from lots of different places. The problem, it’s a bit crusty and I really don’t like it that much. So, here’s a much needed update:


I’m not going to spend much time explaining everything here. Hopefully the comments in the code will do that. This example uses AES, but could be easily modified to use other encryption algorithms.

Generating secure keys is beyond the scope of this post, but take a look here for a decent starting point: https://cryptofreek.org/2012/11/29/pbkdf2-pure-java-implementation/

If you have any questions, let me know!

/*
 * Copyright (c) 2012 Cole Barnes [cryptofreek{at}gmail{dot}com]
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Formatter;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class CEncryptExample
{
  /**
   * Encrypts the data received from the specified InputStream; encrypted data
   * is written to the specified OutputStream.
   *
   * @param key          The secret key used to encrypt the data.
   * @param baIv         The initialization vector to be used in encryption.
   * @param in           InputStream for reading data to encrypt.
   * @param out          OutputStream for writing encrypted data.
   * @param strCipherAlg The encryption algorithm to use; this must correspond
   *                     to something that makes sense with the specified
   *                     secret key.
   * @throws Exception
   */
  public static void encrypt(SecretKey key, byte[] baIv, InputStream in, OutputStream out, String strCipherAlg) throws Exception
  {
    doCrypto(key, baIv, in, out, Cipher.ENCRYPT_MODE, strCipherAlg);
  }

  /**
   * Decrypts the data received from the specified InputStream; decrypted data
   * is written to the specified OutputStream.
   *
   * @param key          The secret key used to decrypt the data.
   * @param baIv         The initialization vector to be used in decryption.
   * @param in           InputStream for reading encrypted data.
   * @param out          OutputStream for writing decrypted data.
   * @param strCipherAlg The encyption algorithm to use; this must correspond
   *                     to something that makes sense with the specified
   *                     secret key.
   * @throws Exception
   */
  public static void decrypt(SecretKey key, byte[] baIv, InputStream in, OutputStream out, String strCipherAlg) throws Exception
  {
    doCrypto(key, baIv, in, out, Cipher.DECRYPT_MODE, strCipherAlg);
  }

  /**
   * Performs the cryptographic operation on the specified IO Streams.  This
   * function is private and is not meant to be called directly.
   *
   * @param key          The secret key used in the crypto operation.
   * @param baIv         The initialization vector used in the crypto operation.
   * @param in           InputStream to from which data is read.
   * @param out          OutputStream to where data is written.
   * @param nMode        Operation mode; either Cipher.DECRYPT_MODE or Cipher.ENCRYPT_MODE
   * @param strCipherAlg The encryption algorithm; this must correspond to
   *                     something that makes sense with the specified secret key
   * @throws Exception
   */
  private static void doCrypto(SecretKey key, byte[] baIv, InputStream in, OutputStream out, int nMode, String strCipherAlg) throws Exception
  {
    Cipher cipher = Cipher.getInstance(strCipherAlg);
    cipher.init(nMode, key, new IvParameterSpec(baIv, 0, cipher.getBlockSize()));

    CipherInputStream cis = new CipherInputStream(in, cipher);

    byte[] baBuff = new byte[1024];
    int nBytesRead = -1;

    while ((nBytesRead = cis.read(baBuff)) != -1)
    {
      out.write(baBuff, 0, nBytesRead);
    }

    cis.close();
    in.close();

    out.flush();
    out.close();
  }

  /* There's only static utility/test functions from here down. */

  private static void doFileCryptoTest(SecretKey key, byte[] baIv, String strCipherAlg) throws Exception
  {
    String strFile = "/Users/cbarnes/Desktop/test.txt";
    String strEncryptedFile = "/Users/cbarnes/Desktop/test.txt.encrypted";
    String strDecryptedFile = "/Users/cbarnes/Desktop/test.txt.decrypted";

    encrypt(key, baIv, new FileInputStream(strFile), new FileOutputStream(strEncryptedFile), strCipherAlg);
    decrypt(key, baIv, new FileInputStream(strEncryptedFile), new FileOutputStream(strDecryptedFile), strCipherAlg);

    System.out.println("Encrypted File Hash:  " + calcDigest(new FileInputStream(strEncryptedFile)));
    System.out.println("Decrypted File Hash:  " + calcDigest(new FileInputStream(strDecryptedFile)));
    System.out.println("     Orig File Hash:  " + calcDigest(new FileInputStream(strFile)));
    System.out.println();
  }

  private static void doByteCryptoTest(SecretKey key, byte[] baIv, String strCipherAlg) throws Exception
  {
    byte[] baData = "This is some data.".getBytes();
    ByteArrayInputStream baisData = new ByteArrayInputStream( baData );
    ByteArrayOutputStream baosEncrytpedData = new ByteArrayOutputStream();

    encrypt(key, baIv, baisData, baosEncrytpedData, strCipherAlg);
    byte[] baEncrytpedData = baosEncrytpedData.toByteArray();

    ByteArrayInputStream baisEncryptedData = new ByteArrayInputStream(baEncrytpedData);
    ByteArrayOutputStream baosDecryptedData = new ByteArrayOutputStream();

    decrypt(key, baIv, baisEncryptedData, baosDecryptedData, strCipherAlg);
    byte[] baDecryptedData = baosDecryptedData.toByteArray();

    System.out.println("Encrypted Data Hash:  " + calcDigest(new ByteArrayInputStream(baEncrytpedData)));
    System.out.println("Decrypted Data Hash:  " + calcDigest(new ByteArrayInputStream(baDecryptedData)));
    System.out.println("     Orig Data Hash:  " + calcDigest(new ByteArrayInputStream(baData)));
    System.out.println();
  }

  private static String calcDigest(InputStream in) throws Exception
  {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");

    byte[] baBuff = new byte[1024];
    int nBytesRead = -1;

    while ((nBytesRead = in.read(baBuff)) != -1)
    {
      digest.update(baBuff, 0, nBytesRead);
    }

    in.close();

    byte[] baDigest = digest.digest();
    StringBuilder sb = new StringBuilder(baDigest.length * 2);
    Formatter formatter = new Formatter(sb);

    for (byte b : baDigest)
    {
      formatter.format("%02x", b);
    }

    formatter.close();
    String strDigest = sb.toString().toLowerCase();

    return strDigest;
  }

  public static void main(String[] args)
  {
    try
    {
      String strKeyAlg = "AES";
      int nKeySize = 128;
      String strCipherAlg = "AES/CBC/PKCS5Padding";

      /* It is your responsibility to come up with a secure manor in which to
       * generate encryption keys!!! */
      byte[] baKey = new byte[nKeySize / 8];
      SecureRandom.getInstance("SHA1PRNG").nextBytes(baKey);
      SecretKey key = new SecretKeySpec(baKey, strKeyAlg);

      /*
       * NEVER reuse initialization vectors (IV)!!!  They should be randomly/securely
       * generated for each separate encryption operation.  The IV should be
       * kept with the encrypted data; it is not "secret" and does not necessarily
       * need to be protected.  You only need to insure new secure IV is generated
       * each time data is encrypted; the same IV is used to decrypt the data.
       */
      byte[] baIv = new byte[128];

      SecureRandom.getInstance("SHA1PRNG").nextBytes(baIv);
      doFileCryptoTest(key, baIv, strCipherAlg);

      SecureRandom.getInstance("SHA1PRNG").nextBytes(baIv);
      doByteCryptoTest(key, baIv, strCipherAlg);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}

PBKDF2 – Pure Java Implementation

Here’s my PBKDF2 implementation. It’s a clean-room implementation straight from RFC 2898. It passes all RFC 6070 test vectors (testing code included).

It’s pure java with no weird requirements or external libraries. It performs reasonably well compared to the stock Java implementation of PBKDF2WithHmacSHA1; sometimes it beats it, sometimes it doesn’t. I’m sure there are ways to optimize performance.

Anyway, enjoy…

/*
 * Copyright (c) 2012
 * Cole Barnes [cryptofreek{at}gmail{dot}com]
 * https://cryptofreek.org/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * -----------------------------------------------------------------------------
 * 
 * This is a clean-room implementation of PBKDF2 using RFC 2898 as a reference.
 * 
 * RFC 2898:
 * http://tools.ietf.org/html/rfc2898#section-5.2
 * 
 * This code passes all RFC 6070 test vectors:
 * http://tools.ietf.org/html/rfc6070
 * 
 * The function "nativeDerive()" is supplied as an example of the native Java 
 * PBKDF2WithHmacSHA1 implementation.  It is used for benchmarking and 
 * comparison only.
 * 
 * The functions "fromHex()" and "toHex()" came from some message board
 * somewhere.  No license was included.
 * 
 */

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.spec.KeySpec;
import java.util.Formatter;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class CPbkdf2
{
  /* START RFC 2898 IMPLEMENTATION */
  public static byte[] derive(String P, String S, int c, int dkLen)
  {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try
    {
      int hLen = 20;

      if (dkLen > ((Math.pow(2, 32)) - 1) * hLen)
      {
        System.out.println("derived key too long");
      }
      else
      {
        int l = (int) Math.ceil((double) dkLen / (double) hLen);
        // int r = dkLen - (l-1)*hLen;

        for (int i = 1; i <= l; i++)
        {
          byte[] T = F(P, S, c, i);
          baos.write(T);
        }
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

    byte[] baDerived = new byte[dkLen];
    System.arraycopy(baos.toByteArray(), 0, baDerived, 0, baDerived.length);

    return baDerived;
  }

  private static byte[] F(String P, String S, int c, int i) throws Exception
  {
    byte[] U_LAST = null;
    byte[] U_XOR = null;

    SecretKeySpec key = new SecretKeySpec(P.getBytes("UTF-8"), "HmacSHA1");
    Mac mac = Mac.getInstance(key.getAlgorithm());
    mac.init(key);

    for (int j = 0; j < c; j++)
    {
      if (j == 0)
      {
        byte[] baS = S.getBytes("UTF-8");
        byte[] baI = INT(i);
        byte[] baU = new byte[baS.length + baI.length];

        System.arraycopy(baS, 0, baU, 0, baS.length);
        System.arraycopy(baI, 0, baU, baS.length, baI.length);

        U_XOR = mac.doFinal(baU);
        U_LAST = U_XOR;
        mac.reset();
      }
      else
      {
        byte[] baU = mac.doFinal(U_LAST);
        mac.reset();

        for (int k = 0; k < U_XOR.length; k++)
        {
          U_XOR[k] = (byte) (U_XOR[k] ^ baU[k]);
        }

        U_LAST = baU;
      }
    }

    return U_XOR;
  }

  private static byte[] INT(int i)
  {
    ByteBuffer bb = ByteBuffer.allocate(4);
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.putInt(i);

    return bb.array();
  }
  /* END RFC 2898 IMPLEMENTATION */

  /* START HELPER FUNCTIONS */
  private static String toHex(byte[] ba)
  {
    String strHex = null;

    if (ba != null)
    {
      StringBuilder sb = new StringBuilder(ba.length * 2);
      Formatter formatter = new Formatter(sb);

      for (byte b : ba)
      {
        formatter.format("%02x", b);
      }

      formatter.close();
      strHex = sb.toString().toLowerCase();
    }

    return strHex;
  }

  private static byte[] nativeDerive(String strPassword, String strSalt, int nIterations, int nKeyLen)
  {
    byte[] baDerived = null;

    try
    {
      SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
      KeySpec ks = new PBEKeySpec(strPassword.toCharArray(), strSalt.getBytes("UTF-8"), nIterations, nKeyLen * 8);
      SecretKey s = f.generateSecret(ks);
      baDerived = s.getEncoded();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

    return baDerived;
  }
  /* END HELPER FUNCTIONS */

  public static void runTestVector(String P, String S, int c, int dkLen, String strExpectedDk)
  {
    System.out.println("Input:");
    System.out.println("  P = \"" + P + "\"");
    System.out.println("  S = \"" + S + "\"");
    System.out.println("  c = " + c);
    System.out.println("  dkLen = " + dkLen);
    System.out.println();

    long nStartDk = System.nanoTime();
    byte[] DK = derive(P, S, c, dkLen);
    long nStopDk = System.nanoTime();
    
    long nStartDkNative = System.nanoTime();
    byte[] DK_NATIVE = nativeDerive(P, S, c, dkLen);
    long nStopDkNative = System.nanoTime();

    System.out.println("Output:");
    System.out.println("  DK          = " + toHex(DK));
    System.out.println("  DK_NATIVE   = " + toHex(DK_NATIVE));
    System.out.println("  DK_EXPECTED = " + strExpectedDk.replaceAll(" ", ""));
    System.out.println();

    System.out.println("Duration [my implementation]:      " + (nStopDk - nStartDk) + " ns" );
    System.out.println("Duration [native implementation]:  " + (nStopDkNative - nStartDkNative) + " ns" );
    
    System.out.println("---------------------------------------------------------------");
    System.out.println();
  }

  public static void RFC6070()
  {
    runTestVector("password", "salt", 1, 20, "0c 60 c8 0f 96 1f 0e 71 f3 a9 b5 24 af 60 12 06 2f e0 37 a6");
    runTestVector("password", "salt", 2, 20, "ea 6c 01 4d c7 2d 6f 8c cd 1e d9 2a ce 1d 41 f0 d8 de 89 57");
    runTestVector("password", "salt", 4096, 20, "4b 00 79 01 b7 65 48 9a be ad 49 d9 26 f7 21 d0 65 a4 29 c1");
    runTestVector("password", "salt", 16777216, 20, "ee fe 3d 61 cd 4d a4 e4 e9 94 5b 3d 6b a2 15 8c 26 34 e9 84");
    runTestVector("passwordPASSWORDpassword", "saltSALTsaltSALTsaltSALTsaltSALTsalt", 4096, 25, "3d 2e ec 4f e4 1c 84 9b 80 c8 d8 36 62 c0 e4 4a 8b 29 1a 96 4c f2 f0 70 38");
    runTestVector("pass\0word", "sa\0lt", 4096, 16, "56 fa 6a a7 55 48 09 9d cc 37 d7 f0 34 25 e0 c3");
  }

  public static void main(String[] args)
  {
    RFC6070();
  }
}

HOWTO: JDBC over an SSH Tunnel

First, credit where credit is due. Most of this code came from here (I just modified it a bit):

http://www.miranet.ch/posts/2008/09/23/howto_jdbc_over_ssh/

You’ll also need JSch (a java implementation of SSH):

http://www.jcraft.com/jsch/

The major function of SSH tunnels are to secure what would otherwise be an unsecure client/server connection. But another awfully handy use of SSH tunnels are accessing remote resources that are not normally exposed. Databases, for example.

I recently needed to access a MySQL database on a remote server from some local Java code. This database is off on a third party hosting server that does not allow outside access to MySQL, but I can login via SSH.

So, here goes:

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class CTestDriver
{
  private static void doSshTunnel( String strSshUser, String strSshPassword, String strSshHost, int nSshPort, String strRemoteHost, int nLocalPort, int nRemotePort ) throws JSchException
  {
    final JSch jsch = new JSch();
    Session session = jsch.getSession( strSshUser, strSshHost, 22 );
    session.setPassword( strSshPassword );
    
    final Properties config = new Properties();
    config.put( "StrictHostKeyChecking", "no" );
    session.setConfig( config );
    
    session.connect();
    session.setPortForwardingL(nLocalPort, strRemoteHost, nRemotePort);
  }
  
  public static void main(String[] args)
  {
    try
    {
      String strSshUser = "ssh_user_name";                  // SSH loging username
      String strSshPassword = "abcd1234";                   // SSH login password
      String strSshHost = "your.ssh.hostname.com";          // hostname or ip or SSH server
      int nSshPort = 22;                                    // remote SSH host port number
      String strRemoteHost = "your.database.hostname.com";  // hostname or ip of your database server
      int nLocalPort = 3366;                                // local port number use to bind SSH tunnel
      int nRemotePort = 3306;                               // remote port number of your database 
      String strDbUser = "db_user_name";                    // database loging username
      String strDbPassword = "4321dcba";                    // database login password
      
      CTestDriver.doSshTunnel(strSshUser, strSshPassword, strSshHost, nSshPort, strRemoteHost, nLocalPort, nRemotePort);
      
      Class.forName("com.mysql.jdbc.Driver");
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:"+nLocalPort, strDbUser, strDbPassword);
      con.close();
    }
    catch( Exception e )
    {
      e.printStackTrace();
    }
    finally
    {
      System.exit(0);
    }
  }
}

So, now I can access the remote database and the traffic is encrypted on top of that!

Same disclaimer as always, this IS NOT production worthy code as is. The exception handling is crap and there’s lots more paranoia to be had. Please follow your own coding best-practices.

Simple File Encrypter v0.3b

Simple File Encrypter version 0.3b is done.  Tested on Windows, OS X, Linux, and Solaris.

http://projects.cryptofreek.org/SimpleFileEncrypter/

 

More Java stuff

Before you read, just keep in mind that I am a Java developer by trade.  I may be a bit biased, but I try to be somewhat objective.

As I mentioned in a previous post, Java will be absent from future versions of OS X.  It’s not surprise, and we’ve known about it for months.  Apple is handing the reigns over to Oracle so that the actual “java people” can do the Java development.

Really, this is a long time coming and it’s absolutely the best move.  The big down side is that it just won’t be there by default (like it has been since the beginning of OS X).  Mac OS X was supposed to be the operating system of choice for Java developers, but I digress.

With all that said, Java is not a totally lost cause on OS X Lion.  While it will not be installed by default, you will be prompted to download the 1.6.0 release from Apple when trying to launch any Java applications.  At present it will install 1.6.0_24, which is the latest release from Oracle.  It seems that applet support is missing though (I would love to be proved wrong).  You may recoil in horror at the thought of applets, but you’d probably be surprised how ofter you encounter them (for legitimate purposes) and never know it.

I’m beyond overjoyed that the OpenJDK project is taking over development on the OS X port, but I am a bit dismayed at the current attitude toward Java in the Apple community.  After all, they have quite a bit of experience with resurrecting obsolete/near-dead programming languages … I’m looking at you Objective-C :)

But hey, maybe I really shouldn’t care.  Nobody uses OS X in the server world; Apple killed Xserver and OS X server may be on the same path.  It’s also not going to be too long before the only people with a need for an OS X computer are iOS application developers.

And as always, I take great comfort in knowing the “cloud” (that makes everyone’s iDevice usable) lives and breathes on linux and java :)

Java SE 7 Developer Preview

It’s probably been 9 months since I’ve played with Java SE 7, and now the Developer Preview window is just about over.

I’ve been on a crazy release schedule since late October, so I haven’t had the time or energy to go play with it.  I’m downloading now though to test out our software.  The GA date is set for mid-summer 2011.

Go get it HERE and try to break it :)

On a related note, it is looking more and more definite that a version 7 release will not be ready for OS X at launch.  We’ve been told as much, but there’s been a bit of wishful thinking on everybody’s part none the less.  It seems a shame, as OS X Lion will not be shipping with Java out of the box (and the version you download from Apple does not include applet support).

Simple Android Speedometer

UPDATE

There’s been quite a bit of activity on this post. Some of the info is old, and people keep asking for the full Eclipse project.

Here’s something even better… Step-by-step tutorial for the simple speedometer:

Nothing but old info below here…

I’ve seen lots of questions concerning the use of the GPS functionality in Android.  It’s super easy, but some of the explanations are needlessly complicated.

The code below is a simple speedometer that uses the GPS chip to show your current speed.  There are optimations that can be made (specifically in the “locationManager.requestLocationUpdates” call), but this is good enough to get you going.  The Android API documentation should fill in the gaps. As always … take it, run, and share with the rest of us.

Check out the interface and class below, and make sure to see the previous post for the “CLocation” class.

import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

public interface IBaseGpsListener extends LocationListener, GpsStatus.Listener
{
  public void onLocationChanged(Location location);

  public void onProviderDisabled(String provider);

  public void onProviderEnabled(String provider);

  public void onStatusChanged(String provider, int status, Bundle extras);

  public void onGpsStatusChanged(int event);
}
import java.util.Formatter;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class CSpeedometer extends Activity implements IBaseGpsListener
{
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.speedometer_main);

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                           0,
                                           0,
                                           this);

    this.updateSpeed(null);

    CheckBox chkUseMetricUnits = (CheckBox)this.findViewById(R.id.chkUseMetricUnits);
    chkUseMetricUnits.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
      {
        CSpeedometer.this.updateSpeed(null);
      }
    });
  }

  public boolean onCreateOptionsMenu(Menu menu)
  {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.speedometer_main, menu);
    return true;
  }

  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId())
    {
      case R.id.menuItemQuit:
        this.finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  public void finish()
  {
    super.finish();
    System.exit(0);
  }

  public void updateSpeed(CLocation location)
  {
    float nCurrentSpeed = 0;

    if( location!=null )
    {
      location.setUseMetricUnits(this.useMetricUnits());
      nCurrentSpeed = location.getSpeed();
    }

    Formatter fmt = new Formatter(new StringBuilder());
    fmt.format(Locale.US, "%5.1f", nCurrentSpeed);
    String strCurrentSpeed = fmt.toString();
    strCurrentSpeed = strCurrentSpeed.replace(' ', '0');

    String strUnits = "miles/hour";
    if (this.useMetricUnits())
    {
      strUnits = "meters/second";
    }

    TextView txtCurrentSpeed = (TextView) this.findViewById(R.id.txtCurrentSpeed);
    txtCurrentSpeed.setText(strCurrentSpeed + " " + strUnits);
  }

  public boolean useMetricUnits()
  {
    CheckBox chkUseMetricUnits = (CheckBox)this.findViewById(R.id.chkUseMetricUnits);
    return chkUseMetricUnits.isChecked();
  }

  public void onLocationChanged(Location location)
  {
    if (location != null)
    {
      CLocation myLocation = new CLocation(location, this.useMetricUnits());
      this.updateSpeed(myLocation);
    }
  }

  public void onProviderDisabled(String provider)
  {
    // TODO: do something one day?
  }

  public void onProviderEnabled(String provider)
  {
    // TODO: do something one day?
  }

  public void onStatusChanged(String provider, int status, Bundle extras)
  {
    // TODO: do something one day?

  }

  public void onGpsStatusChanged(int event)
  {
    // TODO: do something one day?
  }
}

UPDATE

I’ve uploaded the source, layout xml, and manifest xml here:

speedometer_test.zip

 

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 &gt;= 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 &gt;= 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&amp;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.