2

I have been given a set of steps to obtain a hash of data. I have trying to comprehend the steps for quite long now, but the hash that I am getting doesn't seem to match expected hash.

The steps that was mentioned is as follows:

  1. Use the FIN message input as a binary value (unsigned char in C language, byte in Java). The FIN message input must be coded in the ASCII character set.
  2. Combine the left LAU key and the right LAU key as one string. The merged LAU key must be used as a binary value (unsigned char in C language, byte in Java). The merged LAU key must be coded in the ASCII character set.
  3. Call a HMAC256 routine to compute the hash value. The hash value must also be treated as a binary value (unsigned char in C language, byte in Java). The hmac size is 32 bytes.
  4. Convert the hmac binary values to uppercase hexadecimal printable characters (for example, a onebyte binary value of 0000 1010 has a hexadecimal value of 0x0A and will be translated to the twobyte characters “0A”).

This is what I have tried.

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Hex;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;


public class ABCHash {
    public static void main(String[] args) throws Exception {
        try {
            int character;
            StringBuffer buffer = new StringBuffer("");
            FileInputStream inputStream = new FileInputStream(new File("C:/EncPDF.txt"));

            while( (character = inputStream.read()) != -1)
                buffer.append((char) character);

            inputStream.close();
            System.out.println("Fetching data from the file"+buffer);

            StringBuffer sbuf = new StringBuffer(buffer);
           
            String str=sbuf.toString();
           
            System.out.println( "Data = "+ str);
            if(str!=null) {
                String key = "0123456789ABCDEF0123456789ABCDEF"; // Assuming the key as 0123456789ABCDEF
               
                byte[] hexvalue= stringToHexByte(str);
                byte[] hexkey=stringToHexByte(key);
                byte[] byHMAC = encode(hexkey, hexvalue);
                String stEncryptedData = Hex.encodeHexString(byHMAC).toUpperCase();
               
                System.out.println("Encrypted data =\n "+stEncryptedData);
            }
        } catch(Exception e) {
            System.out.println("Exception in the file reading"+e);
        }
    }

    private static byte[] encode(byte[] hexkey, byte[] hexvalue) {
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(hexkey, "HmacSHA256");
            sha256_HMAC.init(secret_key);

            return sha256_HMAC.doFinal(hexvalue);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

        return null;
    }
   
    public static String stringToHex(String base) throws UnsupportedEncodingException {
        return String.format("%040x", new BigInteger(1, base.getBytes(StandardCharsets.US_ASCII)));
    }
   
    public static byte[] stringToHexByte(String base) throws UnsupportedEncodingException {
        System.out.println(stringToHex(base).toUpperCase());
        return DatatypeConverter.parseHexBinary(stringToHex(base).toUpperCase());
    }

}

It will be really helpful if someone can point whats wrong with the implementation.

4
  • According to your description the message input must be coded in the ASCII but you encode it in hex: hexvalue= stringToHexByte(str);.
    – Robert
    Sep 30, 2016 at 14:33
  • Sandesh, did you solve the problem which you asked? I am face to face with this problem now.
    – kamal
    May 10, 2018 at 6:30
  • @kamal Yes. I completed it, but I don't remember the solution. I don't have access to code as well. sorry mate! May 10, 2018 at 6:32
  • @SandeshGupta ok thanks
    – kamal
    May 10, 2018 at 8:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.