0

I have a byte array , I also have 2 methods which one is HexEncode ( converts a byte array to hex String ) the other one is HexDecode ( converts a hex string to byte array )

at first I print the byte array , converted string and hexEncode then I encode it , then again decode this , when print it , both of strings and hexEncodes are equal but byte arrays are not the same. whats the problem ?

how can I encode and decode it in a way which both byte arrays will be same like others ?

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;


public class MainClass {

    public static void main ( String args[]) throws NoSuchAlgorithmException{

        SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
        SecureRandom r = new SecureRandom();
        BigInteger p = new BigInteger(1024 / 2, 100, r);
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        byte[] result =  sha.digest(p.toByteArray());
        //now I have a random byte array :)


        System.out.println(result);
        System.out.println(hexEncode(result));
        String temp1 = new String(result);
        System.out.println(temp1);

        byte[] after = hexStringToByteArray(hexEncode(result));
        System.out.println(after );
        System.out.println(hexEncode(after));
        String temp2 = new String(after);
        System.out.println(temp2);

        if ( Arrays.equals(result, after) ){
            System.out.println("OK");           
        }
        else{
            System.out.println("Problem");
        }



    }
    private static  String hexEncode(byte[] aInput){
        StringBuilder result = new StringBuilder();
        char[] digits = {'0', '1', '2', '3', '4','5','6','7','8','9','a','b','c','d','e','f'};
        for (int idx = 0; idx < aInput.length; ++idx) {
            byte b = aInput[idx];
            result.append(digits[ (b&0xf0) >> 4 ]);
            result.append(digits[ b&0x0f]);
        }
        return result.toString();
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
}

sample output is :

[B@29453f44
66384c6457e27cbfed4a45c080235f52e50e88a5
f8LdWâ|¿íJEÀ€#_R別
[B@5cad8086
66384c6457e27cbfed4a45c080235f52e50e88a5
f8LdWâ|¿íJEÀ€#_R別
OK
Amir Hossein
  • 249
  • 2
  • 4
  • 14
  • 1
    You should see this: [equals vs Arrays.equals in Java](http://stackoverflow.com/questions/8777257/equals-vs-arrays-equals-in-java) – Obicere Aug 19 '14 at 17:55
  • thanks...its ok now , but byte prints are still different , why ? - now i change the code and result to show you – Amir Hossein Aug 19 '14 at 18:00
  • 1
    You are printing the array references. IF You want to print thw content of the arrays use Arrays.toString() – Gustav Grusell Aug 19 '14 at 18:13

1 Answers1

0

Use Arrays.equals(byte[] b1, byte[] b2) instead of equals:

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;


public class MainClass {

    public static void main ( String args[]) throws NoSuchAlgorithmException{

        SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
        SecureRandom r = new SecureRandom();
        BigInteger p = new BigInteger(1024 / 2, 100, r);
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        byte[] result =  sha.digest(p.toByteArray());
        //now I have a random byte array :)


        System.out.println(result);
        System.out.println(hexEncode(result));
        String temp1 = new String(result);
        System.out.println(temp1);

        byte[] after = hexStringToByteArray(hexEncode(result));
        System.out.println(after );
        System.out.println(hexEncode(after));
        String temp2 = new String(after);
        System.out.println(temp2);

        if ( Arrays.equals(after, result)){
            System.out.println("OK");           
        }
        else{
            System.out.println("Problem");
        }



    }
    private static  String hexEncode(byte[] aInput){
        StringBuilder result = new StringBuilder();
        char[] digits = {'0', '1', '2', '3', '4','5','6','7','8','9','a','b','c','d','e','f'};
        for (int idx = 0; idx < aInput.length; ++idx) {
            byte b = aInput[idx];
            result.append(digits[ (b&0xf0) >> 4 ]);
            result.append(digits[ b&0x0f]);
        }
        return result.toString();
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
}
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49