I encode a string, get bytes, convert to a string, and save it with SharedPreferences . In the future, decode the line does not work, how to get an array of bytes?
private void main(String[] args) throws LoginException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { String s = "ab"; Cipher cipher = Cipher.getInstance("AES"); KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); final SecretKey key = kgen.generateKey(); //SecretKeySpec key = new SecretKeySpec("bar12345Bar12345".getBytes(),"AES"); time.setText(String.valueOf(key)); cipher.init(Cipher.ENCRYPT_MODE,key); final byte[] bytes = cipher.doFinal(s.getBytes("UTF-8")); String s=new String(bytes, "UTF-8"); for (byte b : bytes) kkk+=(char) b; //time.setText(s); byte[] byteArray = kkk.getBytes(); // String str = new String(bytes, StandardCharsets.UTF_8); Cipher decript = Cipher.getInstance("AES"); decript.init(Cipher.DECRYPT_MODE,key); byte[] decriptedBytes = decript.doFinal(bytes); for (byte b : decriptedBytes) kk+=(char) b; time.setText(kk); reg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { sPref = getSharedPreferences("save",MODE_PRIVATE); SharedPreferences.Editor ed = sPref.edit(); ed.putString(SAVED_L, s); ed.commit(); Toast.makeText(RegActivity.this, kk+String.valueOf(key), Toast.LENGTH_SHORT).show(); } }); }