Generating a Symmetric Key - Java Security

Java examples for Security:Key

Description

Generating a Symmetric Key

Demo Code

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class Main {
  public static void main(String[] args) throws Exception {
    // Generate a DES key
    KeyGenerator keyGen = KeyGenerator.getInstance("DES");
    SecretKey key = keyGen.generateKey();

    // Generate a Blowfish key
    keyGen = KeyGenerator.getInstance("Blowfish");
    key = keyGen.generateKey();/*from  w w w . j a  v a  2 s. c om*/

    // Generate a triple DES key
    keyGen = KeyGenerator.getInstance("DESede");
    key = keyGen.generateKey();
  }

}

Related Tutorials