I want to encrypt the user ID and store it in the terminal.

Error message

Although encryption with KeyStore is successful,
In the first place, I noticed that the conversion from byte to string was not successful.

Applicable source code
import android.util.Base64;
    public static final String USER_ID_DEFAULT = "noname";
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        Log.d ("first_user_id", Base64.encodeToString (Base64.decode (USER_ID_DEFAULT, Base64.NO_WRAP), Base64.NO_WRAP)

→ first_user_id: nonamQ == will be returned. (Expected result is noname)

The hashed character string that is originally returned from the homebrew API is entered in the USER_ID_DEFAULT part.
Therefore, we believe that there is no problem with Base64.NO_WRAP.
But why does the last character look like this?

I hope you can borrow your help. Thanking you in advance.
If necessary information is added.

  • Answer # 1

    In accordance with the method described on the above site, we confirmed that the expected results were returned.

    String encode = Base64.encodeToString (USER_ID_DEFAULT.getBytes (), Base64.DEFAULT);
    Log.d ("encode", encode);
    String decode = new String (Base64.decode (encode, Base64.DEFAULT));
    Log.d ("decode", decode);

Related articles