This section provides a quick tutorial example on how to write a sample program to generate a secret key for Blowfish, DES, or HmacMD5 encryption, save the secret key to a file, and read it back.
The following sample program shows you how to generate a secret key,
write it a file, and read it back.
Since I am not specifying the provider name,
the implementation of the Blowfish algorithm provided in the default security package
was selected. Of course, Sun is the provider of the default security package.
The Blowfish key is only 7 bytes when "encoded" in RAW format.
When importing the blowfish key back from the 7 raw bytes, SecretKeyScep class
is used instead of SecretKeyFactory class.
In the second test, I wants to try DES algorithm:
java -cp . JceSecretKeyTest 56 key2 DES
KeyGenerator Object Info:
Algorithm = DES
Provider = SunJCE version 1.42
Key Size = 56
toString = javax.crypto.KeyGenerator@998b08
SecretKey Object Info:
Algorithm = DES
Saved File = key2.key
Size = 8
Format = RAW
toString = com.sun.crypto.provider.DESKey@fffe7985
KeySpec Object Info:
Saved File = key2.key
Length = 8
toString = javax.crypto.spec.DESKeySpec@106082
SecretKey Object Info:
Algorithm = DES
toString = com.sun.crypto.provider.DESKey@fffe7985
Of course, you can continue testing with DESede and HmacMD5.