Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SecretKey from byte[]

807605Jun 27 2007 — edited Jun 27 2007
Is it possible to make a SecretKey using byte[]? I have tried several things, the one that looks most promising is setting the bytes in an InputStream and reading them with ObjectInputStream as a SecretKey. This errors out though with an IOException. Is there another way of doing this? Here is my current code:
InputStream keyStream = new ByteArrayInputStream(new byte[] {70, 76, 87, 95, 99, 71,
    53, -101, 68, -7, -63, 4, -121, 14, -2, -87, 28});
SecretKey key = null;
try{
    ObjectInputStream ois = new ObjectInputStream(keyStream);
    key = (SecretKey) ois.readObject();
    ois.close();
} catch (ClassNotFoundException ex) {
    System.err.println("Error: Class not found.");
} catch (IOException ex) {
    System.err.println("Unable to read stream.");
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 25 2007
Added on Jun 27 2007
2 comments
140 views