How to Encode a HexString to Base64 RFC 1421 in Java - java

I need to do a conversion from Hexadecimal String to Base64 in RFC 1421 format. So far I have been doing it with:
org.apache.commons.codec.binary.Base64
But reading the documentation it says the following: Provides Base64 encoding and decoding as defined by RFC 2045.
Therefore it doesn't work for me, I have tried to look for examples to convert a Hex String to Base64 RFC 1421 in Java, but I can't find anything.
Can you give me a hand?
Thanks in advance.
A greeting.

Have you tried using the java.util.Base64 class (available since java 8)?
It has a getMimeEncoder(int, byte[]) method which you can use with a lineLength of 64 and the resulting Encoder should be RFC1421 compliant:
Encoder rfc1421 = Base64.getMimeEncoder(64, new byte[] {'\r', '\n'});
Note: there may be other specificities that I don't know of.

Related

What is the equivalent of Android's Base64.encodeToString(data, Base64.NO_WRAP) in Java

I have looked for this information on stackoverflow but I can't find the exact answer I want.
If we use Java's version of Base64 in java.util, what is Java's equivalent of Android's Base64.encodeToString(data, Base64.NO_WRAP) in Java?
You've identified correctly that you need java.util.Base64. If you read its documentation, you'd see that it supports three types of Base 64 en/decoding. Since the Android code you are trying to translate says NO_WRAP, you should use either the basic encoder or the URL encoder, both of which do not wrap lines. The MIME decoder does wrap lines, which is not what you want.
Base64.Encoder encoder = Base64.getEncoder(); // for the basic encoder, or:
// Base64.Encoder encoder = Base64.getUrlEncoder(); for the URL encoder
On Base64.Encoder, you'll see a method with exactly the same name as the android method - encodeToString.
String encoded = encoder.encodeToString(data);

Character Encoding Conversion In Groovy From UTF-8 to EUC-JP

We require character encoding conversion for one of our service, our requirement is to fetch characters in UTF-8 encoded format and should convert to EUC-JP then prepare some hashing on (Groovy based on) jdk8.
In php, similar solution works fine for us and coded as,
$encodedToEucJp = mb_convert_encoding($inputStringWithUtf8, “EUC-JP”);
Print_r(md5($encodedToEucJp));
We have tried many ways for the solution, e.g.,
Java.security.MessageDigest.getInstance(‘MD5’)
.digest(New String(inputStringWithUtf8.getBytes(“UTF-8”), “EUC-JP”)
.getBytes(“EUC-JP”))
.encodeHex()
.toString();
But, this solution failed for some of the characters that produces different digest then from our php coded solution. Here few characters are mentioned ―, ĭ, ? etc. That’s the reason why we couldn't product same digest with same input both in php and java system.
Thanks, in advance.
The error is in this part of the code:
New String(inputStringWithUtf8.getBytes(“UTF-8”), “EUC-JP”)
Basically, you try to interpret an UTF-8 byte array as if it were encoded in EUC-JP, which is a non-sense.
The following code should do the job
Java.security.MessageDigest.getInstance(‘MD5’)
.digest(inputStringWithUtf8.getBytes(“EUC-JP”))
.encodeHex()
.toString();

How to get the same MD5 string in Java as in C#

I have code in C# which produces MD5 encoded byte[] from String and then this byte[] is converted to String. The C# code is
byte[] valueBytes = (new UnicodeEncoding()).GetBytes(value);
byte[] newHash = (new MD5CryptoServiceProvider()).ComputeHash(valueBytes);
I need to get the same result in Java. I'm trying to do this
Charset utf16 = Charset.forName("UTF-16");
return new String(DigestUtils.md5(value.getBytes(utf16)), utf16);
The code is using Apache Commons Codec library for MD5 calculations. I'm using UTF16 charset because I've read in other SO questions that C#'s UnicodeEncoding uses it by default.
So the code snippets look like they do the same thing, but when I'm passing the string byndyusoft2014, C# gives me hV7u6mQYRgBXXF9jOWWYJg== and Java gives me ﹡둛뭶魙ꇥ늺ꢑ. I've tried UTF16LE and UTF16BE as charsets with no luck.
Has anyone idea about what I'm doing wrong?
I think because of the java decode string to byte[] with utf-8,but the C# is not.So the java and C# encode the different byte array,and then get the different result.You can decode the string to byte[] at c# with utf-8,and see the result.Like following code:
UTF8Encoding utf8 = new UTF8Encoding();
byte[] bytes=utf8.GetBytes("byndyusoft2014");
byte[] en=(new MD5CryptoServiceProvider()).ComputeHash(bytes);
Console.WriteLine(Convert.ToBase64String(en));
and the java code:
byte[] en = DigestUtils.md5Digest("byndyusoft2014".getBytes());
byte[] base64 = Base64Utils.encode(en);
System.out.println(new String(base64));
Of course,in your description,the result of C# like be encoded with base64,so the java should encode the byte array with base64.
The result of them is same as swPvmbGDI1GbPKQwL9knjQ==
The DigestUtils and Base64Utils is some implementation of MD5 and BAS64 in spring library
As it turned out, the main difference was not presented in my original code snippet - it was convertation from MD5 encoded byte[] to String. You need to use Base64 to get final result. This is the working code snippet in Java
Charset utf16 = Charset.forName("UTF-16LE");
return new String(Base64.encodeBase64(DigestUtils.md5(value.getBytes(utf16))));
With this code I get the same result as with C#. Thank you all for good hints!

How to implement rawurldecode in Java?

I'd like to convert PHP code to Java, that is to decode a string stored as an encoded URI format.
That is, change
This%20is%20a%20%2Burl%2B%21
into
This is a +url+!
I've looked at java.net.URI, but there are no suitable examples, and it seems that anything to be decoded by it needs to be in a proper URI format. I'd like to convert a string that isn't in proper format, but contains HTML encoding.
java.net.URLDecoder.decode("This%20is%20a%20%2Burl%2B%21", "UTF-8");
UTF-8 is of course just an example. Use whatever your input encoding is.
You could use URLDecoder (doc here). It just decodes an x-www-form-urlencoded String.
String decodedString = URLDecoder.decode("This%20is%20a%20%2Burl%2B%21");
System.out.println(decodedString);

NSData to Java String

I've been writing a Web Application recently that interacts with iPhones. The iPhone iphone will actually send information to the server in the form of a plist. So it's not uncommon to see something like...
<key>RandomData</key>
<data>UW31vrxbUTl07PaDRDEln3EWTLojFFmsm7YuRAscirI=</data>
Now I know this data is hashed/encrypted in some fashion. When I open up the plist with an editor (Property List Editor), it shows me a more "human readable" format. For example, the data above would be converted into something like...
<346df5da 3c5b5259 74ecf683 4431249f 711630ba 232c54ac 9bf2ee44 0r1c8ab2>
Any idea what the method of converting it is? Mainly I'm looking to get this into a Java String.
Thanks!
According to our friends at wikipedia, the <data> tag contains Base64 encoded data. So, use your favorite Java "Base64" class to decode (see also this question).
ps. technically, this is neither "hashed" nor "encrypted", simply "encoded". "Hashed" implies a one-way transformation where multiple input values can yield the same output value. "Encrypted" implies the need for a (usually secret) "key" to reverse the encryption. Base64 encoding is simply a way of representing arbitrary binary data using only printable characters.
After base64 decoding it you need to hex encode it. This is what PL Editor is showing you.
So...
<key>SomeData</key>
<data>UW31ejxbelle7PaeRAEen3EWMLojbFmsm7LuRAscirI=</data?
Can be represented with...
byte[] bytes = Base64.decode("UW31ejxbelle7PaeRAEen3EWMLojbFmsm7LuRAscirI=");
BigInteger bigInt = new BigInteger(bytes);
String hexString = bigInt.toString(16);
System.out.println(hexString);
To get...
<516df5aa 3c5b5259 74ecf683 4401259f 711630ba 236c59ac 9bb2ee44 0b1c8ab2>

Categories