Converting SecretKeySpec to String and vice-versa:
you can use getEncoded()
method in SecretKeySpec
which will give byteArray
, from that you can use encodeToString()
to get string
value of SecretKeySpec
in Base64
object.
While converting SecretKeySpec
to String
: use decode()
in Base64
will give byteArray
, from that you can create instance for SecretKeySpec
with the params as the byteArray
to reproduce your SecretKeySpec
.
String mAesKey_string;
SecretKeySpec mAesKey= new SecretKeySpec(secretKey.getEncoded(), "AES");
//SecretKeySpec to String
byte[] byteaes=mAesKey.getEncoded();
mAesKey_string=Base64.encodeToString(byteaes,Base64.NO_WRAP);
//String to SecretKeySpec
byte[] aesByte = Base64.decode(mAesKey_string, Base64.NO_WRAP);
mAesKey= new SecretKeySpec(aesByte, "AES");