Leon Anavi
IT Tips && Tricks

Java ME

Created: 03.08.2009 23:08 Last Modified: 04.08.2009 11:35 Views: 10217
Keywords: Cryptography, Digest, Hash, Hex, MD2, MD5, Message, MessageDigest, SHA-1, String

Cryptographic Hash Functions

Introduction

The subject of this article is the study of JavaME (j2me) cryptographic hash function: MD2, MD5 and SHA1. Hash function is an one-way mathematical algorithm that returns a fixed-size unique output string. The input data is called message and the hash value is called message digest. There are two types of hash functions:
  • Non-secure Hash Functions - CRC16, CRC32, etc.
  • Secure Hash Functions - MD2, MD5, SHA1, etc.

Secure Hash Function

Secure hash functions are intended to be impossible to decrypt. The most popular cryptographic hash functions are:
  • MD2 - First published by Ron Riverst in 1989. The message digest size is 128b. Since 2004 MD2 lost popularity due to vulnerability and is deprecated.
  • MD5 - Developed by Ron Riverst and first published in 1992. The message digest size is 128b. Until recently was considered to be the most popular hash function but collisions were detected lately.
  • SHA-1 - The best established hash function is developed by National Security Agency (NSA) and published by the National Institute of Standards and Technology (NIST) in 1995. The message digest is 160b.

JavaME Implementation

To use any of the cryptographic hash functions import java.security.MessageDigest, java.security.DigestException, java.security.NoSuchAlgorithmException. Class MessageDigest provides methods to encrypt a message. Exception will be thrown on error. To represent message digest as human readable text bytes should be converted to string which contains hexadecimal characters(0-9,a-f).

MD2

private String md2(String sInput) throws NoSuchAlgorithmException,
                                                DigestException
{
        //MD2 digest length is 128b (32 characters)
        int nDigestLen = 16;
        byte[] PlainText = sInput.getBytes();
        //Allocate memory for the encrypted text
        byte[] EncryptedText = new byte[nDigestLen];
        MessageDigest Cipher;
        Cipher = MessageDigest.getInstance("MD2");
        Cipher.update(PlainText, 0, PlainText.length);
        Cipher.digest(EncryptedText, 0, nDigestLen);
        //Convert result to hexidecimal
        return toHex(EncryptedText);
}

MD5

private String md5(String sInput) throws NoSuchAlgorithmException,
                                         DigestException
{
        //MD5 digest length is 128b (32 characters)
        int nDigestLen = 16;
        byte[] PlainText = sInput.getBytes();
        //Allocate memory for the encrypted text
        byte[] EncryptedText = new byte[nDigestLen];
        MessageDigest Cipher;
        Cipher = MessageDigest.getInstance("MD5");
        Cipher.update(PlainText, 0, PlainText.length);
        Cipher.digest(EncryptedText, 0, nDigestLen);
        //Convert result to hexidecimal
        return toHex(EncryptedText);
}

SHA-1

private String sha1(String sInput) throws NoSuchAlgorithmException, 
                                          DigestException
{
	//SHA1 digest length is 160b (40 characters)
	int nDigestLen = 20;
	byte[] PlainText = sInput.getBytes();
	//Allocate memory for the encrypted text
	byte[] EncryptedText = new byte[nDigestLen];
	MessageDigest Cipher;
	Cipher = MessageDigest.getInstance("SHA-1");
	Cipher.update(PlainText, 0, PlainText.length);
	Cipher.digest(EncryptedText, 0, nDigestLen);
	//Convert result to hexidecimal
	return toHex(EncryptedText);
}

Convert Bytes to Hex

private String toHex(byte[] bsData)
{
        int nDataLen = bsData.length;
        String sHex = "";
        for (int nIter = 0; nIter < nDataLen; nIter++)
        {
            int nValue = (bsData[nIter] + 256) % 256;
            int nIndex1 = nValue >> 4;
            sHex += Integer.toHexString(nIndex1);
            int nIndex2 = nValue & 0x0f;
            sHex += Integer.toHexString(nIndex2);
        }
        return sHex;
}

Testing

The published examples have been successfully tested on Nokia E51 and NetBeans IDE 6.7 J2ME Emulator. The results have been compared to PHP and MySQL functions for SHA-1 and MD5. The test on Nokia 5300 failed due to No Class Def Found Error.

Class Reference

Class String
Class MessageDigest
Class NoSuchAlgorithmException
DigestException
Class Integer


  Home | About | Contact | Disclaimer | Sitemap © 2009-2022 Leon Anavi. All rights reserved.