0

How is it possible to convert a Array of bytes which are can not in the range of readable characters to a definable set of characters in java?

Here a example:

byte[] bytes = new byte[] { 60, 109, -73, 0, 0, 
                            0, 0, -128, -1, -1, 
                            -1, -121, 19, 21, 14, 0 };

String base64 = "PG23AAAAAID///+HExUOAA==";

It should be configurable which characters are available for the output. The possible characters should be: a-z and/or A-Z and/or 0-9 and/or <>|,;.:-_!"$%&/()=?*'+#~}][{

The Special characters should be able to choose single.

It there some kind of API that have this already done? And when not how should be this implemented.

The must have: the same byte[] should be the same converted string but it must not be possible to change back.

Dan
  • 10,282
  • 2
  • 37
  • 64
Serverfrog
  • 179
  • 2
  • 17
  • Could you please give some example. I dont understand the question and the examples. 60 map to P ? – Mani Apr 10 '14 at 19:29
  • The Byte array in the example has been encoded to the Base64 String. The problem is i don't know how i should convert the binary date i have (the byte array) that is the result of the SHA-3 algorithm converted to a set of characters. When i choose to have a-z and A-Z so the complete byte Array above should have only the characters a-z and A-Z and no others. When i choose to have numbers in there to it should have a-z and A-Z and 0-9. The Result of the String that comes out in the end result which characters should be available and what binary data has been putted in. I Hope that can help. – Serverfrog Apr 10 '14 at 19:34
  • I'm confused... characters have values that are defined in various character sets, bytes are the most primitive data type in java, so translating them you literally just have to map numbers to characters in your case, what exactly do you need to accomplish? – TTT Apr 10 '14 at 19:40
  • I use the SHA-3 (Keccak) checksum Algorithm to generate a checksum of a String. This checksum result to a array of bytes that are sometimes out of the readable range that can reached with different charsets (-x as a example, where x can some number in the byte range). So i would like to convert these byte array into a readable String where the Characters should be available to define. So the converted byte array above and the chooses set of characters of a-z should result a String that only have the characters a-z and represent the byte array (like a in the ASCII the byte value 65 is a "A") – Serverfrog Apr 10 '14 at 19:49
  • Because i realize i can not really explain this in English (i'm from Germany but language in general is not the best i can ;) ) i prepared some examples in the GUI Builder. But remember: this are only random inserted values. The converted string is Just randomly entered that follow the rule i define with the Checkboxes. http://imgur.com/IAcZ7Wz,WIixuvA,agrASTD,94PSHr3,2ln90u4 – Serverfrog Apr 10 '14 at 20:01
  • Correct me if i am wrong. You have used SHA-3 to convert the String/message into byte[] . now the byte[] may contain some value which cant be readable in specific charset ( for example -2 not in ASCII , so while seeing in ASCI editor it would show ? ). For those bytes you want to map to configurable charsets. is that right ? – Mani Apr 10 '14 at 20:17
  • Mostly ;) but very near. Here a example case. I turned A-Z off -> the bytes can be converted into A-Z but shouldn't. Thats the tricky part. Not only the bytes that can't be Readable in ASCII, all bytes that are not Part of the set of characters that are defined should be converted to those bytes that are define as available. – Serverfrog Apr 10 '14 at 20:24

1 Answers1

0

I am not sure i understand your question correctly.

if you want to encode decode using base64

then please refer this SO answer

if you want to convert the bytes into String with base 64 you could use BigInteger.

Like this

new BigInteger(1, bytes).toString(64)

To me , you are looking for the way to decode base64 hash function.

In both approach your input doesnt return what have mentioned :)

Community
  • 1
  • 1
Mani
  • 3,274
  • 2
  • 17
  • 27
  • the Base64 Encoded String is only to decode it and see, in readable form, which data the String contains. The problem is, i would like to convert the bytes into a definable range. That means when i like only to have a-z,A-Z and 0-9 the base64 would be the best use but when i like to have )=?*'+ in this String to, it would be maybe the base91 encoding, which have to much characters in it (more than )=?*') – Serverfrog Apr 10 '14 at 19:52