Java and Net: Base64 conversion confusion

I'm converting text to Java (Android) and Net (Visual Basic) ASCII characters in simple (readable) form are converted well But when it comes to special characters (characters with codes greater than 128), they can cause me trouble

For example, I try to convert a character code with an ASCII value of 65 (character "a")

My java code is:

char a = 65;
String c = String.valueOf(a); 
byte bt[] = c.getBytes();               
String result = Base64.encodeToString(bt,Base64.DEFAULT);

my Net code is:

Dim c As String = Chr(65)
Dim result as String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(c))

Both return the same result: "QQ = =" That's OK But when I try to convert special characters, such as character code 153 Then it returns different results

char a = 153;
String c = String.valueOf(a);               
byte bt[] = c.getBytes();               
String result = Base64.encodeToString(bt,Base64.DEFAULT);

This returns "WPK ="

The same as me Net code:

Dim c As String = Chr(153) 
Dim result as String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(c))

Return to "4osi"

That's strange What's wrong here I use the native Base64 library on both platforms Is there a problem with my code?

Solution

Since the data you encode is encrypted data - random data, in which any byte can be 0 to 255, and in the encrypted state, there is no character or text meaning, you need to call it as - lets - real binary data Java and Net fully supports real binary data through their own byte array primitives

As you know, Base64 encoding is the process of converting real binary data (ranging from 0 to 255) into a slightly larger array of binary data (where each byte is guaranteed to have the same value as ASCII printable characters between 32 bits. And 126) We call this encoded binary The encoded binary can then be safely converted to text because almost every known character set is consistent with the printable ASCII character set (32 to 126)

So Java and VB The main problem with net fragments is that you try to use text primitives in Java - char and string; VB. Net to store real binary data Once you do, it's too late There is no reliable way to convert it back to a byte array because text primitives are not designed to safely store and retrieve binary data For more information on why, read the absolute minimum every software developer absolutely, actively must know about Unicode and character sets (no exceptions!)

Fortunately, the repair is simple For Java, do not use char and string to store binary data Put the data directly into the byte array Try the following:

byte [] bt = new byte[1];
  bt[0] = (byte) 153;
  String result = Base64.encodeToString(bt,Base64.DEFAULT);

I got MQ==

In VB Net, repair is conceptually the same Do not use string Use byte arrays

Dim bytes() As Byte = New Byte() {153}
    Dim result As String = Convert.ToBase64String(bytes)

Again – the answer is MQ==

Finally, after encoding, there is no problem using strings at all Your character is in the ASCII subset. Any conversion between string and byte array will not damage the data, because all character sets are consistent with the ASCII subset

Remember, you will have the same problems in reverse order - decoding You will decode into a byte array, and you will return the real binary number From this point on, data must never be stored as a string - until you finish it - ex. decrypt it back to the original plaintext

I hope this will help

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>