The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.crypto  [14 examples] > Symmetric Keys  [3 examples]

e460. Getting the Bytes of a Generated Symmetric Key

    try {
        // Generate a key
        KeyGenerator keyGen = KeyGenerator.getInstance("DESede");
        SecretKey key = keyGen.generateKey();
    
        // Get the bytes of the key
        byte[] keyBytes = key.getEncoded();
        int numBytes = keyBytes.length;
    
        // The bytes can be converted back to a SecretKey
        SecretKey key2 = new SecretKeySpec(keyBytes, "DESede");
        boolean b = key.equals(key2);  // true
    } catch (NoSuchAlgorithmException e) {
    }

 Related Examples
e458. Listing All Available Symmetric Key Generators
e459. Generating a Symmetric Key

See also: Encrypting and Decrypting    Key Agreement    MAC   


© 2002 Addison-Wesley.