Symmetric Key Generation Using DES Algorithm

Description generates the sysmetric key using DES algorithm. Key size assigned here is 64 bits. It works only for the key size of 64 bits. 56 bits is mentioned in the coding remaining 8bits is accessed from inbuilt package

  • Filename:

import javax.swing.*;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Random ;

class DESKeyGeneration {
byte[] skey = new byte[1000];
String skeyString;
static byte[] raw;

public DESKeyGeneration() {
void generateSymmetricKey() {
try {
Random r = new Random();
int num = r.nextInt(10000);
String knum = String.valueOf(num);
byte[] knumb = knum.getBytes();
skeyString = new String(skey);
System.out.println("DES Symmetric key = "+skeyString);
catch(Exception e) {
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("DES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
kgen.init(56, sr);
SecretKey skey = kgen.generateKey();
raw = skey.getEncoded();
return raw;
public byte[] getSessionKey() {
return raw;
public static void main(String args[]) {
DESKeyGeneration aeskey = new DESKeyGeneration();