Friday, October 28, 2016

SecretKeySpec Class

The simplest way to generate a secret key is select a block of bytes from an array of bytes.

However you want to do it, generate an array of bytes at least as long as your intended cipher requires. Most ciphers now require 16, 24, or 64 byte blocks as keys.

SecretKeySpec has two constructors which give you a choice of exactly the right sized array of bytes or some block of bytes selected from a larger byte array.
byte[] keyArray1 = new byte[16];
byte[] keyArray2 = new byte[24];
byte[] keyArray3 = new byte[64];
Here are the two SecretKeySpec constructors :
SecretKeySpec(byte[] key, String algorithm)
SecretKeySpec key1 = new SecretKeySpec(keyArray1,"AES");

SecretKeySpec(byte[] key, int offset, int len, String algorithm)
SecretKeySpec key2 = new SecretKeySpec(keyArray3,13,16,"Serpent");


Here is the link to the official Oracle Documentation




No comments: