Once you have generated a SecretKey you can use it’s getEncoded() method to generate a byte array that can be used to recreate the key.
// Use the following to initially create a random key KeyGenerator keyGen = KeyGenerator.getInstance("AES"); kgen.init(256); SecretKey key = keyGen.generateKey(); // Then use the following to get the byte encoded = key.getEncoded(); // byte array could now be saved for later use to recreate key
To then recreate your key using the byte array, you can use the following:
SecretKey key = new SecretKeySpec(encoded, "AES");
One Response to “How to save your SecretKey?”
Leave a Reply
You must be logged in to post a comment.