I have a String in Java that contains 32 characters:
String tempHash = "123456789ABCDEF123456789ABCDEF12";
Each character in the String above represents a hex value. I need to convert it to another String, that contains 8-bytes calculated by each hex from the string above. So in the example from above, the output string would be:
"00000001 00000010 00000011 000001000 000001001 000001011 ..."
how can I do that?
I tried to do:
byte[] bytes1 = toByteArray(tempHash);
where
public static byte[] toByteArray(String s) {
return DatatypeConverter.parseHexBinary(s);
}
but when I iterate over this array:
for (byte singleByte: bytes1) {
System.out.println(singleByte);
}
for the first character I'm getting 18 instead of 00000001.
I'm a little bit lost over here. Could you please help me with that?
One solution is to use a Stream:
String tempHash = "123456789ABCDEF123456789ABCDEF12";
String binary = tempHash.chars() // Get stream of chars
.map(c -> Character.digit(c, 16)) // Convert to hex digit
.mapToObj(Integer::toBinaryString) // Convert to binary
.map(s -> "0".repeat(8 - s.length()) + s) // Pad left with zeros
.collect(Collectors.joining(" ")); // Collect to String
System.out.println(binary);
Output:
00000001 00000010 00000011 00000100 00000101 ...
As Kevin pointed out in his comment below, a pre-Java 11 solution would be to replace the call to String#repeat:
String binary = tempHash.chars() // Get stream of chars
.map(c -> Character.digit(c, 16)) // Convert to hex digit
.mapToObj(Integer::toBinaryString) // Convert to binary
.map(s -> new String(new char[8 - s.length()]).replace('\0', '0') + s) // Pad left with zeros
.collect(Collectors.joining(" ")); // Collect to String
You can use Long.parseLong(String,16);
Once you have a long value, you can get the bytes by doing
long val = ...;
ByteBuffer buf = new ByteBuffer();
buf.put(0, val);
If your string is too long you will need to use a BigInteger. It's essentially the same thing, but a little more complicated
public byte hexToByte(String hexString) {
int firstDigit = toDigit(hexString.charAt(0));
int secondDigit = toDigit(hexString.charAt(1));
return (byte) ((firstDigit << 4) + secondDigit);
}
private int toDigit(char hexChar) {
int digit = Character.digit(hexChar, 16);
if(digit == -1) {
throw new IllegalArgumentException(
"Invalid Hexadecimal Character: "+ hexChar);
}
return digit;
}
Here is the reference
Related
I have the following code trying to convert between byte and bit arrays, somehow it's not converting correctly, what is wrong and how to correct it ?
String getBitsFromBytes(byte[] Byte_Array) // 129
{
String Bits="";
for (int i=0;i<Byte_Array.length;i++) Bits+=String.format("%8s",Integer.toBinaryString(Byte_Array[i] & 0xFF)).replace(' ','0');
System.out.println(Bits); // 10000001
return Bits;
}
byte[] getBytesFromBits(int[] bits)
{
byte[] results=new byte[(bits.length+7)/8];
int byteValue=0;
int index;
for (index=0;index<bits.length;index++)
{
byteValue=(byteValue<<1)|bits[index];
if (index%8==7) results[index/8]=(byte)byteValue;
}
if (index%8!=0) results[index/8]=(byte)((byte)byteValue<<(8-(index%8)));
System.out.println(Arrays.toString(results));
return results;
}
...
String bit_string=getBitsFromBytes("ab".getBytes()); // 0110000101100010 : 01100001 + 01100010 --> ab
int[] bits=new int[bit_string.length()];
for (int i=0;i<bits.length;i++) bits[i]=Integer.parseInt(bit_string.substring(i,i+1));
getBytesFromBits(bits);
When I ran it, I got the following :
0110000101100010
[97, 98]
I was expecting this :
0110000101100010
[a, b]
You need to convert from byte to char if you plan to display numeric values as their corresponding ASCII character:
char[] chars = new char[results.length];
for (int i = 0; i < results.length; i++) {
chars[i] = (char) results[i];
}
System.out.println(Arrays.toString(chars));
To convert from byte[] to String you should use new String(byte[]) constructor and specify the right charset. Arrays.toString() exists only to print a sequence of elements.
How to do bitwise XOR operation to two strings in java.
You want something like this:
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException;
public class StringXORer {
public String encode(String s, String key) {
return base64Encode(xorWithKey(s.getBytes(), key.getBytes()));
}
public String decode(String s, String key) {
return new String(xorWithKey(base64Decode(s), key.getBytes()));
}
private byte[] xorWithKey(byte[] a, byte[] key) {
byte[] out = new byte[a.length];
for (int i = 0; i < a.length; i++) {
out[i] = (byte) (a[i] ^ key[i%key.length]);
}
return out;
}
private byte[] base64Decode(String s) {
try {
BASE64Decoder d = new BASE64Decoder();
return d.decodeBuffer(s);
} catch (IOException e) {throw new RuntimeException(e);}
}
private String base64Encode(byte[] bytes) {
BASE64Encoder enc = new BASE64Encoder();
return enc.encode(bytes).replaceAll("\\s", "");
}
}
The base64 encoding is done because xor'ing the bytes of a string may not give valid bytes back for a string.
Note: this only works for low characters i.e. below 0x8000, This works for all ASCII characters.
I would do an XOR each charAt() to create a new String. Like
String s, key;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < s.length(); i++)
sb.append((char)(s.charAt(i) ^ key.charAt(i % key.length())));
String result = sb.toString();
In response to #user467257's comment
If your input/output is utf-8 and you xor "a" and "æ", you are left with an invalid utf-8 string consisting of one character (decimal 135, a continuation character).
It is the char values which are being xor'ed, but the byte values and this produces a character whichc an be UTF-8 encoded.
public static void main(String... args) throws UnsupportedEncodingException {
char ch1 = 'a';
char ch2 = 'æ';
char ch3 = (char) (ch1 ^ ch2);
System.out.println((int) ch3 + " UTF-8 encoded is " + Arrays.toString(String.valueOf(ch3).getBytes("UTF-8")));
}
prints
135 UTF-8 encoded is [-62, -121]
Pay attention:
A Java char corresponds to a UTF-16 code unit, and in some cases two consecutive chars (a so-called surrogate pair) are needed for one real Unicode character (codepoint).
XORing two valid UTF-16 sequences (i.e. Java Strings char by char, or byte by byte after encoding to UTF-16) does not necessarily give you another valid UTF-16 string - you may have unpaired surrogates as a result. (It would still be a perfectly usable Java String, just the codepoint-concerning methods could get confused, and the ones that convert to other encodings for output and similar.)
The same is valid if you first convert your Strings to UTF-8 and then XOR these bytes - here you quite probably will end up with a byte sequence which is not valid UTF-8, if your Strings were not already both pure ASCII strings.
Even if you try to do it right and iterate over your two Strings by codepoint and try to XOR the codepoints, you can end up with codepoints outside the valid range (for example, U+FFFFF (plane 15) XOR U+10000 (plane 16) = U+1FFFFF (which would the last character of plane 31), way above the range of existing codepoints. And you could also end up this way with codepoints reserved for surrogates (= not valid ones).
If your strings only contain chars < 128, 256, 512, 1024, 2048, 4096, 8192, 16384, or 32768, then the (char-wise) XORed strings will be in the same range, and thus certainly not contain any surrogates. In the first two cases you could also encode your String as ASCII or Latin-1, respectively, and have the same XOR-result for the bytes. (You still can end up with control chars, which may be a problem for you.)
What I'm finally saying here: don't expect the result of encrypting Strings to be a valid string again - instead, simply store and transmit it as a byte[] (or a stream of bytes). (And yes, convert to UTF-8 before encrypting, and from UTF-8 after decrypting).
This solution is compatible with Android (I've tested and used it myself). Thanks to #user467257 whose solution I adapted this from.
import android.util.Base64;
public class StringXORer {
public String encode(String s, String key) {
return new String(Base64.encode(xorWithKey(s.getBytes(), key.getBytes()), Base64.DEFAULT));
}
public String decode(String s, String key) {
return new String(xorWithKey(base64Decode(s), key.getBytes()));
}
private byte[] xorWithKey(byte[] a, byte[] key) {
byte[] out = new byte[a.length];
for (int i = 0; i < a.length; i++) {
out[i] = (byte) (a[i] ^ key[i%key.length]);
}
return out;
}
private byte[] base64Decode(String s) {
return Base64.decode(s,Base64.DEFAULT);
}
private String base64Encode(byte[] bytes) {
return new String(Base64.encode(bytes,Base64.DEFAULT));
}
}
Assuming (!) the strings are of equal length, why not convert the strings to byte arrays and then XOR the bytes. The resultant byte arrays may be of different lengths too depending on your encoding (e.g. UTF8 will expand to different byte lengths for different characters).
You should be careful to specify the character encoding to ensure consistent/reliable string/byte conversion.
This is the code I'm using:
private static byte[] xor(final byte[] input, final byte[] secret) {
final byte[] output = new byte[input.length];
if (secret.length == 0) {
throw new IllegalArgumentException("empty security key");
}
int spos = 0;
for (int pos = 0; pos < input.length; ++pos) {
output[pos] = (byte) (input[pos] ^ secret[spos]);
++spos;
if (spos >= secret.length) {
spos = 0;
}
}
return output;
}
the abs function is when the Strings are not the same length so the legth of the result will be the same as the min lenght of the two String a and b
public String xor(String a, String b){
StringBuilder sb = new StringBuilder();
for(int k=0; k < a.length(); k++)
sb.append((a.charAt(k) ^ b.charAt(k + (Math.abs(a.length() - b.length()))))) ;
return sb.toString();
}
I'm trying to convert an Integer to a String, and then encrypt the String with a XOR encryption. But when i'm decrypting my Strin again, i get a different answer, that the String i typed before the encryption, and i don't know what i'm doing wrong?
public class Krypte {
public static void main (String [] args) {
int i = 12345;
String k = Integer.toString(i);
String G = secure(k.getBytes());
System.out.println("Encrypted: " + G);
String U = secure(G.getBytes());
System.out.println("Decrypted: " + U);
int X = Integer.parseInt(U);
System.out.println("As an int: " + X);
}
public static String secure(byte[] msg) {
// Variables
int outLength = msg.length;
byte secret = (byte) 0xAC; // same as 10101100b (Key)
// XOR kryptering
for (int i = 0; i < outLength; i++) {
// encrypting each byte with XOR (^)
msg[i] = (byte) (msg[i] ^ secret);
}
return new String(msg);
}
}
There's a subtle (yet very important) difference between char and byte types. Consider this:
class Krypte {
public static void main (String [] args) {
int i = 12345;
String k = Integer.toString(i);
System.out.println("Before: " + k);
String G = secure(k.toCharArray());
System.out.println("Encrypted: " + G);
String U = secure(G.toCharArray());
System.out.println("Decrypted: " + U);
int X = Integer.parseInt(U);
System.out.println("As an int: " + X);
}
public static String secure(char[] msg) {
// Variables
int outLength = msg.length;
byte secret = (byte) 0xAC; // same as 10101100b (Key)
// XOR kryptering
for (int i = 0; i < outLength; i++) {
// encrypting each byte with XOR (^)
System.out.println("Byte before: " + msg[i]);
msg[i] = (char) (msg[i] ^ secret);
System.out.println("Byte after: " + msg[i]);
}
return new String(msg);
}
}
This works (proof), because XORing some character value with a byte will (most probably) give you a valid character.
Not let's see what happens in the original snippet - by adding this debugging output into the main loop of secure method:
System.out.println("Byte before: " + msg[i]);
msg[i] = (byte) (msg[i] ^ secret);
System.out.println("Byte after: " + msg[i]);
And the output would be:
Byte before: 49
Byte after: -99
Byte before: 50
Byte after: -98
Byte before: 51
Byte after: -97
Byte before: 52
Byte after: -104
Byte before: 53
Byte after: -103
It's quite ok: first getBytes function encoded the string given into an array of bytes using the platform's default charset. Character '1' gets encoded into 49 byte value; '2' becomes 50, etc.
Then we're XORing these values with our key - and get this sequence of bytes:
-99 -98 -97 -104 -103
The final step seems easy: we just make (and return) a new String from this sequence of bytes, what can go wrong here? But in fact it's the very step where, well, the fan get hit. )
See, String constructor tries to process this sequence of bytes using the platform's default charset. Indeed, for some charsets these bytes represent a sequence of valid characters just fine - but not for UTF-8!
...You probably already guessed what happens next. For each 'undecodable' sequence of bytes, as described here, the first byte is transformed into so-called Replacement character, and others are retried. In this particular example there would be five of these signs of failure in the string returned by the first secure invokation.
Decoding this string is, well, quite meaningless - as it doesn't store any information (except length) about the target string. That's why the original code ultimately failed.
I have a problem converting hexadecimal to a character when the hexadecimal has 3 digits
I have 2 methods which escape and unescape characters over decimal value 127
test\\b8 is produced when test¸ is escaped
The unescape does the following:
for (int i=0, n=node.length; i<n; i++) {
if(c == "\\"){
char c2 = node[i + 1];
char c3 = node[i + 2];
int i= Integer.parseInt(str,16);
char c = (char)i;
System.out.println("Char is:=" + c);
}
}
output - test¸
As you can see I take the first two characters after the slash and convert them into a char. This all works fine. However there are sometimes characters that have 3 hexadecimal digits (for example test\\2d8. This should unescape as test˘). When this enters into my unescape method is won't use all 3 characters. Only the first 2 and therefore the produce wrong results.
Is there a way to determine when to convert 2 or 3 characters
Here's what I would do:
String raw = new String(node); // might be a better way to get a string from the chars
int slashPos = raw.indexOf('\\');
if(slashPos >= 0) {
String hex = raw.substring(slashPos + 1);
int value = Integer.parseInt(hex,16);
}
In this manner, we're not special casing anything for 2, 3, 4, or 100 digits (although I'm sure 100 digits would throw an exception :-) ). Instead, we're using the protocol as a 'milestone' in the string, and then just accepting that everything after the slash is the hex string.
class HexParse {
private static class HexResult {
final boolean exists;
final int value;
HexResult(boolean e, int v) { exists = e; value = v; }
}
private final String raw;
private final HexResult result;
public HexParse(String raw) {
this.raw = raw;
int slashPos = raw.indexOf('\\');
boolean noSlash = slashPos < 0;
boolean noTextAfterSlash = slashPos > raw.length() - 2;
if(noSlash || noTextAfterSlash) {
result = new HexResult(false,0);
} else {
// throws exception if second part of string contains non-hex chars
result = new HexResult(true,Integer.parseInt(raw.substring(slashPos + 1),16));
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(raw).append(" ");
if(result.exists) {
sb.append("has hex of decimal value ").append(result.value);
} else {
sb.append("has no hex");
}
return sb.toString();
}
public static void main(String...args) {
System.out.println(new HexParse("test`hello")); // none
System.out.println(new HexParse("haha\\abcdef")); // hex
System.out.println(new HexParse("good\\f00d")); // hex
System.out.println(new HexParse("\\84b")); // hex
System.out.println(new HexParse("\\")); // none
System.out.println(new HexParse("abcd\\efgh")); //exception
}
}
c:\files\j>javac HexParse.java
c:\files\j>java HexParse
test`hello has no hex
haha\abcdef has hex of decimal value 11259375
good\f00d has hex of decimal value 61453
\84b has hex of decimal value 2123
\ has no hex
Exception in thread "main" java.lang.NumberFormatException: For input string: "e
fgh"
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:458)
at HexParse.<init>(HexParse.java:21)
at HexParse.main(HexParse.java:
I am probably overlooking something silly, but I've never had to deal with binary in code and thought it'd be a good idea to practice it in an encryption program, for kicks.
Long story short, I'm able to convert a string into binary (in the form of a string), but can't figure out how to do the reverse.
Right now, I have something like this:
public static String bytesToString(String bytes){
int i = bytes.length()/8;
int pos = 0;
String result = "";
for(int j=0; j<i; j++){
String temp = bytes.substring(pos,pos+8);
byte b = (byte) Integer.parseInt(temp);
result = result + Byte.toString(b);
pos++;
}
System.out.println("Result: " + result);
return result;
}
I think the bytes are being parsed as literal numbers. What am I missing?
Edit: To clarify, I will previously have parsed a string of text into bits and written them to a string. I want to split this string into bytes and parse them back into letters. It would take "011010000110010101111001" and return "hey".
How about using Integer.parseInt(text, 2)? As in,
public static int binaryToInt(String binary)
{
return Integer.parseInt(binary, 2);
}
I'm not sure why your binaryToString method both takes and returns a string.
Integer.parseInt(temp) will attempt to read temp as a number and return the corresponding int. For example, Integer.parseInt("123") returns 123
EDIT: Be aware that the binary value of a character or text depends on the encoding you are using. For example "hi" is 0110100001101001 in ASCII but it may not in UTF-16 or UTF-32. And Java encodes characters into UTF-16 characters: see http://download.oracle.com/javase/6/docs/api/java/lang/String.html
(for this reason Java chars are 16-bit unsigned integers).
So your bytesToString method must treat input differently depending on the encoding of the input. Or you may write it specifically for ASCII characters, and maybe rename it to, say, asciiBytesToString
You'd better see:
constructor String(byte[])
http://download.oracle.com/javase/6/docs/api/java/lang/String.html
Integer.parseInt(String s, int radix) http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html
public class BinaryStringToChars {
public static void main(String[] args) {
String bin = "011010000110010101111001";
StringBuilder b = new StringBuilder();
int len = bin.length();
int i = 0;
while (i + 8 <= len) {
char c = convert(bin.substring(i, i+8));
i+=8;
b.append(c);
}
System.out.println(b.toString());
}
private static char convert(String bs) {
return (char)Integer.parseInt(bs, 2);
}
}
You need to advance 8 digits at a time, not digit by digit. Otherwise you are reusing bits. Also, you need to tell Integer.parseInt() what radix you want to use, since parseInt(String val) cannot really detect binary (you want Integer.parseInt(String val, int radix). You also need to choose a character encoding to convert bytes into characters (they are not the same thing!). Assuming ISO-8859-1 is ok:
public static String bytesToString(String bytes){
int i = bytes.length()/8;
int pos = 0;
String result = "";
byte[] buffer = new byte[i];
for(int j=0; j<i; j++){
String temp = bytes.substring(pos,pos+8);
buffer[j] = (byte) Integer.parseInt(temp, 2);
pos+=8;
}
result = new String(buffer, "ISO-8859-1");
System.out.println("Result: " + result);
return result;
}