Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 670
Yayayaya Avatar asked Oct 24 '25 02:10

Yayayaya


1 Answers

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)
}
like image 145
Kishore Jethava Avatar answered Oct 26 '25 16:10

Kishore Jethava