1// create new key
2SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
3// get base64 encoded version of the key
4String encodedKey = Base64.getEncoder().encodeToString(secretKey.getEncoded());
5
1// decode the base64 encoded string
2byte[] decodedKey = Base64.getDecoder().decode(encodedKey);
3// rebuild key using SecretKeySpec
4SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
5