Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any functions in Java equivalent to bin2hex & hex2bin in PHP?

Tags:

java

php

I'm trying to convert this php code to java:

$pwd = "j7dR2f6Ywi01t+~P"; // This is key
$data = "10203010"; // This is Password

$encrypted = encrypt(pwd, $data);
$encrypted =  bin2hex($encrypted);
echo $encrypted;
echo '<br />';
$decrypted = decrypt($pwd, hex2bin($encrypted));
echo $decrypted;

function encrypt ($pwd, $data)
    {
        $key[] = '';
        $box[] = '';
        $cipher = '';
        $pwd_length = strlen($pwd);
        $data_length = strlen($data);
        for ($i = 0; $i < 256; $i++)
        {
            $key[$i] = ord($pwd[$i % $pwd_length]);
            $box[$i] = $i;
        }
        /***************************************
            for($z=0; $z<256; $z++)
            {
                echo $key[$z].'<br />';
            }
            exit;
        /***************************************/
        for ($j = $i = 0; $i < 256; $i++)
        {
            $j = ($j + $box[$i] + $key[$i]) % 256;
            $tmp = $box[$i];
            $box[$i] = $box[$j];
            $box[$j] = $tmp;
        }
        for ($a = $j = $i = 0; $i < $data_length; $i++)
        {
            $a = ($a + 1) % 256;
            $j = ($j + $box[$a]) % 256;
            $tmp = $box[$a];
            $box[$a] = $box[$j];
            $box[$j] = $tmp;
            $k = $box[(($box[$a] + $box[$j]) % 256)];
            $cipher .= chr(ord($data[$i]) ^ $k);
        }
        return $cipher;
    }

    function decrypt ($pwd, $data)
    {
        return encrypt($pwd, $data);
    }

It's perfectly converted to Java like the following except for bin2hex & hex2bin functions

package pkgfinal;
public class Final { 
    public static   String convertHexToString(String hex){

      StringBuilder sb = new StringBuilder();
      StringBuilder temp = new StringBuilder();

      //49204c6f7665204a617661 split into two characters 49, 20, 4c...
      for( int i=0; i<hex.length()-1; i+=2 ){

          //grab the hex in pairs
          String output = hex.substring(i, (i + 2));
          //convert hex to decimal
          int decimal = Integer.parseInt(output, 16);
          //convert the decimal to character
          sb.append((char)decimal);

          temp.append(decimal);
      }
      System.out.println("Decimal : " + temp.toString());

      return sb.toString();
  }


    public static void RC4(String pwd ,String  data)
 {
   int[] key = new int[256];
   int[] box = new int[256];
   int i ;
   int a ;
   int j;
   int temp ;
   int  k ;
   StringBuilder cipher = new StringBuilder();
   StringBuilder output = new StringBuilder();

   int pwd_length = pwd.length();
   int data_length = data.length();
   for (   i = 0 ; i < 256 ; i++ )
    {
        key[i]= pwd.charAt(i % pwd.length());
        box[i]=i;
    }

  i = 0 ;
  j = 0 ;
  for ( ; i < 256 ; i++ )
  {
   j= (j + box[i]+ key[i] ) % 256    ;
   temp = box[i];
   box[i] = box[j];
   box[j]=temp;
  }
  j = 0;
  i = 0 ;
  a = 0 ;
  for( ; i < data_length ; i++)
  {
      a = (a+1) %256 ;
      j= (j + box[a])%256;
      temp = box[a];
      box[a] = box[j];
      box[j]= temp;
////      k = box[((box[a]+box[j])%256 )] ;              
      cipher.append((char)(data.charAt(i)^k));     
  }
  System.out.println(cipher.toString()); 
  //String o = cipher.toString();
  //byte[] bytes = o.getBytes();
  //System.out.println( Hex.encodeHexString( bytes ) );
 }
    public static void main(String[] args) 
    {
         Final.RC4("j7dR2f6Ywi01t+~P","10203010");        
    }

}

is there any way to convert the final result to hexadecimal using a function equivalent to bin2hex for encryption and q


1 Answers

None of the previous answers worked for me!

I solved using:

DatatypeConverter.parseHexBinary(hexString);

DatatypeConverter.printHexBinary(byteArray);

from package javax.xml.bind.DatatypeConverter.

like image 194
xonya Avatar answered May 15 '26 05:05

xonya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!