My input hex is
C30A010000003602000F73B32F9ECA00E9F2F2E9
I need to convert it to the following base 64 encoded String:
wwoBAAAANgIAD3OzL57KAOny8uk=
I can simulate this transformation on this site: http://www.asciitohex.com/ but I cant seem to get this transformation working in Java using the various base64 encoder Utils that are suggested on this site and other places on the web. For example,
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
public class Test {
public static void main(final String args[]) throws DecoderException {
String hexString = "C30A010000003602000F73B32F9ECA00E9F2F2E9";
String output = new String(Base64.encodeBase64String(hexString.getBytes()));
System.out.println(output);
}
However the output for this is something different:
QzMwQTAxMDAwMDAwMzYwMjAwMEY3M0IzMkY5RUNBMDBFOUYyRjJFOQ==
Can anyone suggest how to get this transformation working successfully?
Thanks
Basically, hexString.getBytes() doesn't do what you expect it to. It's just encoding the string as a byte sequence in your platform default encoding - it's got nothing to do with hex.
You need to decode from hex to byte[] to start with. Additionally, you don't need to call the String constructor with another string. As you're already using Apache Commons Codec, it makes sense to use the Hex class for the decoding. I would also separate out the steps for clarity:
String hexString = "C30A010000003602000F73B32F9ECA00E9F2F2E9";
byte[] rawData = Hex.decodeHex(hexString.toCharArray());
String output = Base64.encodeBase64String(rawData);
Related
Here i have a code which accept a file content in byte array, i want to check whether its in base64 format or not,before converting it to base64 and returning.. can anyone help me out here
import sun.misc.BASE64Encoder;
public static String encodeInByteArray(byte[] b)
{
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(b);
}
Below is the code which i tried to check for base64 format:
import sun.misc.BASE64Encoder;
import java.util.regex.Pattern;
public class Encoder
{
public static String encodeInByteArray(byte[] b)
{
String regex =
"([A-Za-z0-9+/]{4})*"+
"([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)";
Pattern patron = Pattern.compile(regex);
String s=b.toString();
if (!patron.matcher(s).matches()){
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(b);
}
else
return s;
}
public static void main(String [] args) throws FileNotFoundException
{
FileInputStream fs= new FileInputStream("Sample.pdf");
String s= fs.toString();
byte[] b = s.getBytes();
encodeInByteArray(b);
}
}
Calling b.toString() doesn't do what you might expect - the resulting string will be something like [B#106d69c, because arrays don't override toString. (In a similar vein, calling fs.toString() won't give you the contents of the file as a string).
To get a String from a byte[], use the constructor:
new String(b)
But you probably want to specify a particular charset, e.g.:
new String(b, StandardCharsets.ISO_8859_1)
otherwise you may get different results, depending upon your JVM's configuration.
First solution you could parse the file, or parse the file part way (to save resources) and determine if a line is base64 encoded. See this answer for the String base64 encoding check.
How to check whether the string is base64 encoded or not
A second solution would be is that if you have complete control over the file saving and encoding, you could place a byte at the head or tail of the file indicated if its base64 encoded or not, which should be faster then the above solution.
You can use Base64.isBase64(byte[] arrayOctet) from apache's commons-codec.
Be aware that whitespaces are valid at the moment as stated in the documentation.
A string-"gACA" encoded in PHP using base64. Now I'm trying to decode in java using base64. But getting absurd value after decoding. I have tried like this:
public class DecodeString{
{
public static void main(String args[]){
String strEncode = "gACA"; //gACA is encoded string in PHP
byte byteEncode[] = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.decode(strEncode );
System.out.println("Decoded String" + new String(k, "UTF-8"));
}
}
Output:
??
Please help me out
Java has built-in Base64 encoder-decoder, no need extra libraries to decode it:
byte[] data = javax.xml.bind.DatatypeConverter.parseBase64Binary("gACA");
for (byte b : data)
System.out.printf("%02x ", b);
Output:
80 00 80
It's 3 bytes with hexadecimal codes: 80 00 80
public static void main(String args[]) {
String strEncode = "gACA"; //gACA is encoded string in PHP
byte byteEncode[] = Base64.decode(strEncode);
String result = new String(byteEncode, "UTF-8");
char[] resultChar = result.toCharArray();
for(int i =0; i < resultChar.length; i++)
{
System.out.println((int)resultChar[i]);
}
System.out.println("Decoded String: " + result);
}
I suspect it's an encoding problem. Issue about 65533 � in C# text file reading this post suggest the first and last character are \“. In the middle there is a char 0. Your result is probably "" or "0", but with wrong encoding.
Try this, it worked fine for me (However I was decoding files):
Base64.decodeBase64(IOUtils.toByteArray(strEncode));
So it would look like this:
public class DecodeString{
{
public static void main(String args[]){
String strEncode = "gACA"; //gACA is encoded string in PHP
byte[] byteEncode = Base64.decodeBase64(IOUtils.toByteArray(strEncode));
System.out.println("Decoded String" + new String(k, "UTF-8));
}
}
Note that you will need extra libraries:
Commons Codec
Commons FileUpload
Commons IO
First things first, the code you use should not compile, it's missing a closing quote after "UTF-8.
And yeah, "gACA" is a valid base64 string as the format goes, but it doesn't decode to any meaningful UTF-8 text. I suppose you're using the wrong encoding, or messed up the string somehow...
RFC 4648 defines two alphabets.
PHP uses Base 64 Encoding
Java uses Base 64 Encoding with URL and Filename Safe Alphabet.
They are very close but not the exact same. In PHP:
const REPLACE_PAIRS = [
'-' => '+',
'_' => '/'
];
public static function base64FromUrlSafeToPHP($base64_url_encoded) {
return strtr($base64_url_encoded, self::REPLACE_PAIRS);
}
public static function base64FromPHPToUrlSafe($base64_encoded) {
return strtr($base64_encoded, array_flip(self::REPLACE_PAIRS));
}
I'm new to Java and I'm no sure how to do the following:
A Scala application somewhere converts a String into bytes:
ByteBuffer.wrap(str.getBytes)
I collect this byte array as a Java String, and I wish to do the inverse of what the Scala code above did, hence get the original String (object str above).
Getting the ByteBuffer as a String to begin with is the only option I have, as I'm reading it from an AWS Kinesis stream (or is it?). The Scala code shouldn't change either.
Example string:
String str = "AAAAAAAAAAGZ7dFR0XmV23BRuufU+eCekJe6TGGUBBu5WSLIse4ERy9............";
How can this be achieved in Java?
EDIT
Okay, so I'll try to elaborate a little more about the process:
A 3rd party Scala application produces CSV rows which I need to consume
Before storing those rows in an AWS Kinesis stream, the application does the following to each row:
ByteBuffer.wrap(output.getBytes);
I read the data from the stream as a string, and the string could look like the following one:
String str = "AAAAAAAAAAGZ7dFR0XmV23BRuufU+eCekJe6TGGUBBu5WSLIse4ERy9............";
I need to restore the contents of the string above into its original, readable, form;
I hope I've made it clearer now, sorry for puzzling you all to begin with.
If you want to go from byte[] to String, try new String(yourBytes).
Both getBytes and the String(byte[]) uses the default character encoding.
From Amazon Kinesis Service API Reference:
The data blob to put into the record, which is Base64-encoded when the blob is serialized.
You need to base64 decode the string. Using Java 8 it would look like:
byte[] bytes = Base64.getDecoder().decode("AAAAAAAAAAGZ7dFR0XmV23BR........");
str = new String(bytes, "utf-8"));
Other options: Base64 Encoding in Java
I m not sure if I understand the question exactly but do you mean this?
String decoded = new String(bytes);
public static void main(String[] args){
String decoded = new String(bytesData);
String actualString;
try{
actualString = new String(bytesData,"UTF-8");
System.out.printLn("String is" + actualString);
}catch(UnsupportedEncodingException e){
e.printstacktrace();
}
}
Sorry,wrong answer.
Again,ByteBuffer is a java class. SO they may work the same way
You need java version..
From kafka ApiUtils:
def writeShortString(buffer:ByteBuffer,string:String){
if(String == null){
buffer.putShort(-1)
}
else{
val encodedString = string.getBytes(“utf-8”)
if(encodedString.length > Short.MaxValue){
throw YourException(Your Message)
else{
buffer.putShort(encodedString.length.asInstanceOf[Short])
buffer.put(encodedString)
}
}
}
For Kinesis data blobs:
private CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.decode(record.getData()).toString();
I realise this is probably more of a general java question, but since it's running in Notes\ Domino environment, thought I'd check that community first.
Summary:
I don't seem to be able to decode the string: dABlAHMAdAA= using lotus.domino.axis.encoding.Base64 or sun.misc.BASE64Decoder
I know the original text is: test
I confirmed by decoding at http://www5.rptea.com/base64/ it appears it is UTF-16.
As simple test, using either of below:
String s_base64 = "dABlAHMAdAA=";
byte[] byte_base64 = null;
String s_decoded = "";
byte_base64 = new sun.misc.BASE64Decoder().decodeBuffer(s_base64);
s_decoded = new String(byte_base64, "UTF-16");
System.out.println("Test1: " + s_decoded);
byte_base64 = lotus.domino.axis.encoding.Base64.decode(s_base64);
s_decoded = new String(byte_base64, "UTF-16");
System.out.println("Test2: " + s_decoded);
System.out.println("========= FINISH.");
I get the output:
Test1: ????
Test2: ????
If I create String as UTF-8
s_decoded = new String(byte_base64, "UTF-8");
it outputs:
t
no error is thrown, but it doesn't complete the code, doesn't get to the "FINISH".
Detail
I'm accessing an asmx web service, in the SOAP response, some nodes contain base64 encoded data. At this point in time, there is no way to get the service changed, so I am having to XPath and decode myself. Encoded data is either text or html. If I pass the encoded data thru http://www5.rptea.com/base64/ and select UTF-16, it decodes correctly, so I must be doing something incorrectly.
As side note, I encoded "test":
s_base64 = lotus.domino.axis.encoding.Base64.encode(s_text.getBytes());
System.out.println("test1 encodes to: " + s_base64);
s_base64 = new sun.misc.BASE64Encoder().encode(s_text.getBytes());
System.out.println("test2 encodes to: " + s_base64);
they both encode to:
dGVzdA==
...which if you then feed into 2 decoders above, as expected, decodes correctly.
If I go to site above, and encode "test" as UTF-16, I get: dABlAHMAdAA= so that confirms that data is in UTF-16.
It's like the data is genuine base64 data, but the decoder doesn't recognise it as such. I'm slightly stumped at the moment.
Any pointers or comments would be gratefully received.
The string has been encoded in UTF-16LE (little-endian), where the least significant byte is stored first. Java defaults to big-endian. You need to use:
s_decoded = new String(byte_base64, "UTF-16LE");
i have used your sample "dABlAHMAdAA=" on my base64 decode online tool and it seems like you are missing the Apache base64 jar files
Click the link below.
http://www.hosting4free.info/Base64Decode/Base64-Decode.jsp
The code behind the website is
import org.apache.commons.codec.binary.Base64;
public class base64decode
{
public static void main(String[] args) throws UnsupportedEncodingException
{
byte[] decoded = Base64.decodeBase64("YWJjZGVmZw==".getBytes());
System.out.println(new String(decoded) + "\n");
}
}
I'm using apache library for encoding to base64. But this time problem is very typical. I've a b64 encoded string.
MIIHSjCCBjKgAwIBAgIQQuw1emUfNRlPD/euDuzBjDANBgkqhkiG9w0BAQUFADCB"+
"5TELMAkGA1UEBhMCRVMxIDAeBgkqhkiG9w0BCQEWEWFjQGFjYWJvZ2FjaWEub3Jn
Its the part of certificate (.CER) file. I am just decoding it and again encoding it but result is little bit different. Resultant string is,
"MIIHSjCCBjKgAwIBAgIQQuw1emUfNRlPD/euDuzBjDANBgkqhkiG9w0BAQUFADA"+ "/5TELMAkGA1UEBhMCRVMxIDAeBgkqhkiG9w0BCQEWEWFjQGFjYWJvZ2FjaWEub3Jn"
The difference is at the end of the first line and starting of the second line. CB are replaced by A/.
This change invalidates my certificate. Where the problem can be ?
The problem is in your intermediate string conversion. If you use only byte array, everything is fine.
public static void main(String args[]) {
String partOfCer = "MIIHSjCCBjKgAwIBAgIQQuw1emUfNRlPD/euDuzBjDANBgkqhkiG9w0BAQUFADCB" + "5TELMAkGA1UEBhMCRVMxIDAeBgkqhkiG9w0BCQEWEWFjQGFjYWJvZ2FjaWEub3Jn";
byte[] dec1_byte = Base64.decodeBase64(partOfCer.getBytes());
// String dec1 = new String(dec1_byte);
byte[] newBytes = Base64.encodeBase64(dec1_byte);
String newStr = new String(newBytes);
System.out.println(partOfCer);
System.out.println(newStr);
System.out.println(partOfCer.equals(newStr));
}