5

I have some code to login to a 3rd party solution, that requires me to encode a password. They have provided me with a sample in Java, but I also need to develop it in C# (and PHP, later).

I have the code in a C# Windows app, and a Java Android app, and with the same output, they seem to work well up until the point where I try and convert the cypher bytes to a base 64 encoded string.

Here is the Java code:

enc = Base64.encodeBase64(ciphertext);
return enc.toString();

And here is the C# code:

return System.Convert.ToBase64String(cipherBytes);

I can see that the ciphertext bytes in the Java app are the same as in the C# app, except that they are signed, and that the base64 encoded bytes in Java, if converted to ASCII values, would give the string that I am seeing in the C# app, but the enc.ToString() in Java is not returning that same string.

More info

Java

ciphertext = 66, 67, -69, 24, -48, -23, 84, -5
encodeded64 = 81, 107, 79, 55, 71, 78, 68, 112, 86, 80, 115, 61
to string = [B@41771ea8

C#

cipherBytes = 66, 67, 187, 24, 208, 233, 84, 251
result = QkO7GNDpVPs=

Many thanks for your help. (This is my first post here, so please feel free to let me know if I broke any rules)

1 Answer 1

11

The Java code you're using is calling toString() on a byte array, which will always give you a string of the form [B@....

You could use:

return new String(enc, "ASCII");

... but I would suggest changing to use an API where encoding a byte array gives you back a string to start with. I like this public domain implementation.

return Base64.encodeBytes(cipherText);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.