Curious Techie
Curious Techie

Reputation: 195

Key size for AES vs DES in KeyGenerator

I am using javax.crypto.KeyGenerator to generate Keys.

Below is my code for AES :

Key key = null;
        SecureRandom rand = new SecureRandom();
        KeyGenerator generator;
        try {                       
            generator = KeyGenerator.getInstance("AES");
            generator.init(rand);
            generator.init(128);
            key = generator.generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        System.out.println("AES key is : ");
        System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));

Code for DES :

Key key = null;
        SecureRandom rand = new SecureRandom();
        KeyGenerator generator;
        try {                       
            generator = KeyGenerator.getInstance("DES");
            generator.init(rand);
            generator.init(56);
            key = generator.generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        System.out.println("DES key is : ");
        System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));

As you can see when I use DES, i have to pass 56 to init() method, as opposed to 128 in AES.

Can someone please explain why i can't use 128 bit and which encryption is preferable from this two types?

Upvotes: 0

Views: 2010

Answers (1)

Dogukan Zengin
Dogukan Zengin

Reputation: 574

AES is an advanced version of DES with bigger key size. So if you need more secure encryption, using AES should be your option.

Data Encryption Standard (DES) : DES is an implementation of a Feistel Cipher. It uses 16 round Feistel structure. The block size is 64-bit. Though, key length is 64-bit, DES has an effective key length of 56 bits, since 8 of the 64 bits of the key are not used by the encryption algorithm (function as check bits only).

Advanced Encryption Standard(AES) :The more popular and widely adopted symmetric encryption algorithm likely to be encountered nowadays is the Advanced Encryption Standard (AES). It is found at least six time faster than triple DES. A replacement for DES was needed as its key size was too small. With increasing computing power, it was considered vulnerable against exhaustive key search attack. Triple DES was designed to overcome this drawback but it was found slow

Upvotes: 2

Related Questions