Yayayaya
Yayayaya

Reputation: 246

How to use base 64 for devices with api lower than 26?

I am developing an application that uses a an authorization key to connect users to the apps server using volley. In order for the authorization key to be recognized it must be decoded with both the authorization key itself and the action the user is trying to initiate from the server I have this first line of code that encodes the authorization key below, as well as other areas where the encoding is used:

  String authkey="xxxgafjeusjsj" ;
  String action ="pay" ;
  String auth=authkey+action
  String Authkey=Base64.getEncoder().encodeToString(auth_.getBytes());

 ..... 
  return Base64.getEncoder().encodeToString(signature);
  ...... 
  PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyContent));

The above code works fine, however the last 3 lines can only be used for devices with api 26 and above. Is there an alternative code I can use for the last 3 lines of code? I was advised to use 'import android.util.Base64;' as opposed to 'Java.util.Base64' but it returns the error cannot resolve method getEncoder() Please help

Upvotes: 3

Views: 2590

Answers (1)

Kishore Jethava
Kishore Jethava

Reputation: 6834

I was advised to use 'import android.util.Base64;' as opposed to 'Java.util.Base64' but it returns the error cannot resolve method getEncoder()

android.util.Base64 has encodeToString as equivalent of getEncoder().encode(args)

You can use like

String Authkey= "";

if (VERSION.SDK_INT >= 26) {
       Authkey = Base64.getEncoder().encode(auth_.getBytes()).toString();
} else {
      Authkey = android.util.Base64.encodeToString(auth_.getBytes(), 0)
}

Upvotes: 6

Related Questions