JAVA: 128-bit key for String problem and back

I created a SecretkeySpec object that contains a 128-bit key. I would like to have this key in String (this string should be inserted back into the original key later), so I use Base64 encoding.

This is how my key looks in raw format from an array of bytes to characters:

*P??? ?ukL|?~

So, I take the bytes and encode them like this.

byte[] okay = Base64.encode(eF.getSpec().getEncoded());

Now, when I translate it into characters, I get:

S2xEa3Ara0o5blVGYTB3WkRIeUZmZz09DQo=

Now I want to return my key back to its original format from a base64 encoded array.

String dkey = "S2xEa3Ara0o5blVGYTB3WkRIeUZmZz09DQo=";

byte[] key = null;
key = dKey.getBytes();
key = Base64.decode(key);

Now, when I check the result, I get:

DKlDkp+kJ9nUFa0wZHyFfg==

instead:

*P??? ?ukL|?~ 

, , . , , , . , - 128- , , , .

, .

+3
2
S2xEa3Ara0o5blVGYTB3WkRIeUZmZz09DQo=

KlDkp+kJ9nUFa0wZDHyFfg==.

D ?

KlDkp+kJ9nUFa0wZDHyFfg== 

, base64, . , .

, ,

? base64 ?

+2

(, ). . , ( Apache Commons Codecs, , org.apache.commons.codec.binary.Base64).

package edu.sasik.test.encoding;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

public class TestBase64 {

  public static void main(String[] args) {
    String base64Str = Base64.encode("Hello World!".getBytes());
    System.out.println(base64Str);
    byte[] bytes = Base64.decode(base64Str);
    System.out.println(new String(bytes));
  }

}

:

SGVsbG8gV29ybGQh
Hello World!

, .

0

Source: https://habr.com/ru/post/1778498/


All Articles