Reputation:
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
Upvotes: 3
Views: 8551
Reputation: 83
The following hex2bin for java do exactly what hex2bin in php do:
private static String hex2bin(String hex) {
StringBuilder bin = new StringBuilder("");
for (int i = 0; i < hex.length(); i += 2) {
String str = hex.substring(i, i + 2);
bin.append((char) Integer.parseInt(str, 16));
}
return bin.toString();
}
Upvotes: 0
Reputation: 13679
These are my almost complete optimized functions to do it!
/**
* Decodes a hexadecimally encoded binary string.
* <p>
* Note that this function does <em>NOT</em> convert a hexadecimal number to
* a binary number.
*
* @param hex Hexadecimal representation of data.
* @return The byte[] representation of the given data.
* @throws NumberFormatException If the hexadecimal input string is of odd
* length or invalid hexadecimal string.
*/
public static byte[] hex2bin(String hex) throws NumberFormatException {
if (hex.length() % 2 > 0) {
throw new NumberFormatException("Hexadecimal input string must have an even length.");
}
byte[] r = new byte[hex.length() / 2];
for (int i = hex.length(); i > 0;) {
r[i / 2 - 1] = (byte) (digit(hex.charAt(--i)) | (digit(hex.charAt(--i)) << 4));
}
return r;
}
private static int digit(char ch) {
//TODO Optimize this
int r = Character.digit(ch, 16);
if (r < 0) {
throw new NumberFormatException("Invalid hexadecimal string: " + ch);
}
return r;
}
public static String bin2hex(byte[] in) {
StringBuilder sb = new StringBuilder(in.length * 2);
for (byte b : in) {
sb.append(
forDigit((b & 0xF0) >> 4)
).append(
forDigit(b & 0xF)
);
}
return sb.toString();
}
public static char forDigit(int digit) {
if (digit < 10) {
return (char) ('0' + digit);
}
return (char) ('A' - 10 + digit);
}
Upvotes: 1
Reputation: 2484
None of the previous answers worked for me!
I solved using:
DatatypeConverter.parseHexBinary(hexString);
DatatypeConverter.printHexBinary(byteArray);
from package javax.xml.bind.DatatypeConverter
.
Upvotes: 7
Reputation: 76
Try this...
public String Bin2String(String hex){
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
String str = hex.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
return output.toString();
}
Upvotes: 1
Reputation: 201467
Yes. There is Integer.parseInt(String s, int radix)
and Integer.toString(int i, int radix)
.
Upvotes: 0
Reputation: 9429
first, you should convert from hex to dec. then dec to binary
from hex to bin
String binStr = Integer.toString(Integer.parseInt("ff", 16),2);
from bin to hex
String hexStr = Integer.toString(Integer.parseInt("1111", 2),16);
Upvotes: 1